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

ClassCastException: cannot cast jdk.nashorn.internal.scripts.JO*

XMLWordPrintable

    • b08
    • x86_64
    • windows_7

        FULL PRODUCT VERSION :
        java version "1.8.0_20"
        Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
        Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)


        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [version 6.1.7601]

        A DESCRIPTION OF THE PROBLEM :
        Here's an upstream bug report for:
        - https://josm.openstreetmap.de/ticket/10423
        - https://github.com/ypid/opening_hours.js/issues/52

        where some javascript works fine with Java 7u67 but fails with Java 8u20 with this stracktrace:

        java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.scripts.JO12 to jdk.nashorn.internal.scripts.JO16
        at sun.invoke.util.ValueConversions.newClassCastException(Unknown Source)
        at sun.invoke.util.ValueConversions.castReference(Unknown Source)

        We have isolated the problem to the handling of "name" property on objects but can't figure exactly what the problem is.

        Here's attached a small program demonstrating the problem.

        REGRESSION. Last worked in version 7u67

        ADDITIONAL REGRESSION INFORMATION:
        java version "1.7.0_67"
        Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
        Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run attached program.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        "Success" is displayed on standard output.
        ACTUAL -
        ClassCastException

        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        Exception in thread "main" java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.scripts.JO4 to jdk.nashorn.internal.scripts.JO8
        at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:312)
        at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:307)
        at jdk.nashorn.internal.runtime.AccessorProperty.getObjectValue(AccessorProperty.java:359)
        at jdk.nashorn.internal.runtime.FindProperty.getObjectValue(FindProperty.java:162)
        at jdk.nashorn.internal.runtime.ScriptObject.get(ScriptObject.java:2690)
        at jdk.nashorn.internal.runtime.ScriptObject.get(ScriptObject.java:2707)
        at jdk.nashorn.internal.scripts.Script$\^eval\_.opening_hours(<eval>:14)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:555)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
        at jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:185)
        at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:505)
        at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:227)
        at Test.main(Test.java:35)


        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import javax.script.Invocable;
        import javax.script.ScriptEngine;
        import javax.script.ScriptEngineManager;
        import javax.script.ScriptException;

        public class Test {

            public static void main(String[] args) {
                String js = "function opening_hours() {\n"+
                    "var other_object = { \n" +
                    // Try to move the name member a
                    " 'name': 'test name', \n"+
                    " '2005': [], \n"+
                    " '2007': [], \n"+
                    " '2006': [], \n"+
                    "}; \n"+
                
                    "var object = { \n"+
                    " 'name': 'hello', \n"+
                    " // '2005': [], \n"+
                    " // '2007': [], \n"+
                    " // '2006': [], \n"+
                    "}; \n"+
                
                    // Comment out both -> ClassCastException disappears.
                    "object['name']; \n"+
                    "object.name; \n"+
                "}\n";
                
                try {
                    ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
                    engine.eval(js);
                    ((Invocable) engine).invokeFunction("opening_hours");
                    System.out.println("Success !");
                } catch (ScriptException | NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Switch back to Java 7.

              hannesw Hannes Wallnoefer
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: