JDK-8056978 : ClassCastException: cannot cast jdk.nashorn.internal.scripts.JO*
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 8u20
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2014-08-29
  • Updated: 2015-10-12
  • Resolved: 2014-09-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u40 b08Fixed 9Fixed
Related Reports
Duplicate :  
Description
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.


Comments
Looks like there is some unwanted map sharing occurs and we end up using "wrong" script object "format" - JO8 vs JO4. I reproduced the ClassCastException as described on jdk9-dev tip as well.
02-09-2014