diff options
Diffstat (limited to 'src/jdk/nashorn/api/scripting/NashornException.java')
-rw-r--r-- | src/jdk/nashorn/api/scripting/NashornException.java | 45 |
1 files changed, 26 insertions, 19 deletions
diff --git a/src/jdk/nashorn/api/scripting/NashornException.java b/src/jdk/nashorn/api/scripting/NashornException.java index 098f8508..1509a7df 100644 --- a/src/jdk/nashorn/api/scripting/NashornException.java +++ b/src/jdk/nashorn/api/scripting/NashornException.java @@ -41,13 +41,18 @@ import jdk.nashorn.internal.runtime.ScriptObject; * script object or Java access to script object properties via java.util.Map * interface. In these cases, user code will get an instance of this or * implementation defined subclass. + * + * @since 1.8u40 */ +@jdk.Exported @SuppressWarnings("serial") public abstract class NashornException extends RuntimeException { // script file name private String fileName; // script line number private int line; + // are the line and fileName unknown? + private boolean lineAndFileNameUnknown; // script column number private int column; // underlying ECMA error object - lazily initialized @@ -89,27 +94,10 @@ public abstract class NashornException extends RuntimeException { */ protected NashornException(final String msg, final Throwable cause) { super(msg, cause == null ? null : cause); - // This is not so pretty - but it gets the job done. Note that the stack - // trace has been already filled by "fillInStackTrace" call from - // Throwable - // constructor and so we don't pay additional cost for it. - // Hard luck - no column number info this.column = -1; - - // Find the first JavaScript frame by walking and set file, line from it - // Usually, we should be able to find it in just few frames depth. - for (final StackTraceElement ste : getStackTrace()) { - if (ECMAErrors.isScriptFrame(ste)) { - // Whatever here is compiled from JavaScript code - this.fileName = ste.getFileName(); - this.line = ste.getLineNumber(); - return; - } - } - - this.fileName = null; - this.line = 0; + // We can retrieve the line number and file name from the stack trace if needed + this.lineAndFileNameUnknown = true; } /** @@ -118,6 +106,7 @@ public abstract class NashornException extends RuntimeException { * @return the file name */ public final String getFileName() { + ensureLineAndFileName(); return fileName; } @@ -128,6 +117,7 @@ public abstract class NashornException extends RuntimeException { */ public final void setFileName(final String fileName) { this.fileName = fileName; + lineAndFileNameUnknown = false; } /** @@ -136,6 +126,7 @@ public abstract class NashornException extends RuntimeException { * @return the line number */ public final int getLineNumber() { + ensureLineAndFileName(); return line; } @@ -145,6 +136,7 @@ public abstract class NashornException extends RuntimeException { * @param line the line number */ public final void setLineNumber(final int line) { + lineAndFileNameUnknown = false; this.line = line; } @@ -271,4 +263,19 @@ public abstract class NashornException extends RuntimeException { public void setEcmaError(final Object ecmaError) { this.ecmaError = ecmaError; } + + private void ensureLineAndFileName() { + if (lineAndFileNameUnknown) { + for (final StackTraceElement ste : getStackTrace()) { + if (ECMAErrors.isScriptFrame(ste)) { + // Whatever here is compiled from JavaScript code + fileName = ste.getFileName(); + line = ste.getLineNumber(); + return; + } + } + + lineAndFileNameUnknown = false; + } + } } |