Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6957117

javax script - calls from script to Java can lose Exceptions

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4
    • Resolution: Withdrawn
    • Affects Version/s: 6u10
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      FULL PRODUCT VERSION :
      I can reproduce this issue in Java 6.20 and Java 7 ea b95

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP Version 2002 Service Pack 3

      A DESCRIPTION OF THE PROBLEM :
      I thought this was the same as bug id 6474943 - JSR 223 exceptions lack critical diagnostic info from Rhino. But I can report a similar issue with simpler steps.

      When a function in a script is called through Invocable - and that function calls a Java method - and If that Java method throws any runtime exception then the "caused by" part of the stack trace is lost. There appears no way to find the real line number where the error occurs. It seems that the sun.org.mozilla.javascript.internal.WrappedException swallows the original exception information.

      Note I created a bug report for the Mozilla folks as well here:
      https://bugzilla.mozilla.org/show_bug.cgi?id=569046





      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See: Source code for an executable test case:

      Save the script as script.js

      Save the java code as Main.java - Then compile and run Main


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      javax.script.ScriptException: sun.org.mozilla.javascript.internal.WrappedException: Wrapped java.lang.NullPointerException (<Unknown Source>#11) in <Unknown Source> at line number 11
              at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:209)
              at com.sun.script.javascript.RhinoScriptEngine.invokeFunction(RhinoScriptEngine.java:167)
              at testrhino.Main.<init>(Main.java:58)
              at testrhino.Main.main(Main.java:31)
      CAUSED BY
          x.substring(1); at line 60 (in my version of the code)

      We should get the caused by to be able to trace exactly where the exception occurred.
      ACTUAL -
      javax.script.ScriptException: sun.org.mozilla.javascript.internal.WrappedException: Wrapped java.lang.NullPointerException (<Unknown Source>#11) in <Unknown Source> at line number 11
              at com.sun.script.javascript.RhinoScriptEngine.invoke(RhinoScriptEngine.java:209)
              at com.sun.script.javascript.RhinoScriptEngine.invokeFunction(RhinoScriptEngine.java:167)
              at testrhino.Main.<init>(Main.java:58)
              at testrhino.Main.main(Main.java:31)


      No caused by here....


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      // ** Script start
      function start() {
          log.info("calling method in Java....");
          test.callFromRhino();
      }
      // ** Script end

      // ** Main start
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.io.Reader;
      import java.net.URL;
      import java.util.logging.Level;
      import java.util.logging.Logger;

      import javax.script.*;

      /**
       *
       * @author Dan
       */
      public class Main {

          private ScriptEngineManager mgr = new ScriptEngineManager();
          private ScriptEngine engine = mgr.getEngineByName("JavaScript");

          private static final Logger log = Logger.getLogger("Main");


          public static void main(String args[]) {
              try {
                  new Main();
              } catch (Exception ex) {
                  ex.printStackTrace();
                  log.log(Level.SEVERE, ex.getMessage(), ex);
                  ex.getCause().printStackTrace();
              }
          }

          public Main() throws ScriptException, NoSuchMethodException {
              CompiledScript script = null;
              //URL url = getClass().getResource("callback.js");
              InputStream is = getClass().getResourceAsStream("script.js");

              Compilable compilingEngine = (Compilable) engine;

              Reader reader = new InputStreamReader(is);
              script = compilingEngine.compile(reader);

              Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
              bindings.put("log", log);
              bindings.put("test", this); // place this object in the bindings
              script.eval(bindings);


              Invocable invocable = (Invocable) script.getEngine();
              invocable.invokeFunction("start");

          }

          public void callFromRhino() {
              // The start function calls this method.
              // We'll force a null pointer exception
              // I cannot get this line number from the stack trace.
              String x = null;
              x.substring(1);

          }

      }
      // ** Main end

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None.

        Attachments

          Activity

            People

            • Assignee:
              sundar Sundararajan Athijegannathan
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: