From b40416f06052ca158103abb59741a8ea6742cc1c Mon Sep 17 00:00:00 2001 From: Greg Brail Date: Wed, 18 Dec 2024 15:08:05 -0800 Subject: [PATCH] Go back to the pre-Java-9 stack trace API This is compatible with Android and also seems to be faster --- .../java/org/mozilla/javascript/Context.java | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/Context.java b/rhino/src/main/java/org/mozilla/javascript/Context.java index 3f4ac58252..2dfb33c3a6 100644 --- a/rhino/src/main/java/org/mozilla/javascript/Context.java +++ b/rhino/src/main/java/org/mozilla/javascript/Context.java @@ -25,7 +25,6 @@ import java.util.HashSet; import java.util.Locale; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.TimeZone; import java.util.function.Consumer; @@ -2642,24 +2641,20 @@ static String getSourcePositionFromStack(int[] linep) { } /** Returns the current filename in the java stack. */ - @SuppressWarnings("AndroidJdkLibsChecker") - // Android uses interpreter, so we should not get here. static String getSourcePositionFromJavaStack(int[] linep) { - Optional frame = - StackWalker.getInstance() - .walk(stream -> stream.filter(Context::frameMatches).findFirst()); - return frame.map( - f -> { - linep[0] = f.getLineNumber(); - return f.getFileName(); - }) - .orElse(null); - } - - @SuppressWarnings("AndroidJdkLibsChecker") - private static boolean frameMatches(StackWalker.StackFrame frame) { - return (frame.getFileName() == null || !frame.getFileName().endsWith(".java")) - && frame.getLineNumber() > 0; + StackTraceElement[] stack = new Throwable().getStackTrace(); + for (StackTraceElement e : stack) { + if (frameMatches(e)) { + linep[0] = e.getLineNumber(); + return e.getFileName(); + } + } + return null; + } + + private static boolean frameMatches(StackTraceElement e) { + return (e.getFileName() == null || !e.getFileName().endsWith(".java")) + && e.getLineNumber() > 0; } RegExpProxy getRegExpProxy() {