Details
-
Bug
-
Resolution: Fixed
-
P3
-
8
-
None
-
b102
-
generic
-
generic
Description
import javax.script.*;
public class Main {
public static void main(String[] args) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByName("nashorn");
Object obj = e.eval("obj = { run: function() { print('hello') } }");
ScriptContext ctxt = new SimpleScriptContext();
ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
e.setContext(ctxt);
Runnable r = ((Invocable)e).getInterface(obj, Runnable.class);
r.run();
}
}
Results in ClassCastException:
Exception in thread "main" java.lang.ClassCastException: jdk.nashorn.api.scripting.ScriptObjectMirror cannot be cast to jdk.nashorn.internal.runtime.ScriptObject
at jdk.nashorn.api.scripting.NashornScriptEngine.getInterfaceInner(NashornScriptEngine.java:187)
at jdk.nashorn.api.scripting.NashornScriptEngine.getInterface(NashornScriptEngine.java:224)
at Main.main(Main.java:14)
This issue is similar toJDK-8021252 - similar issue with Invocable.invokeMethod implementation.
public class Main {
public static void main(String[] args) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByName("nashorn");
Object obj = e.eval("obj = { run: function() { print('hello') } }");
ScriptContext ctxt = new SimpleScriptContext();
ctxt.setBindings(e.createBindings(), ScriptContext.ENGINE_SCOPE);
e.setContext(ctxt);
Runnable r = ((Invocable)e).getInterface(obj, Runnable.class);
r.run();
}
}
Results in ClassCastException:
Exception in thread "main" java.lang.ClassCastException: jdk.nashorn.api.scripting.ScriptObjectMirror cannot be cast to jdk.nashorn.internal.runtime.ScriptObject
at jdk.nashorn.api.scripting.NashornScriptEngine.getInterfaceInner(NashornScriptEngine.java:187)
at jdk.nashorn.api.scripting.NashornScriptEngine.getInterface(NashornScriptEngine.java:224)
at Main.main(Main.java:14)
This issue is similar to