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

Nashorn - ClassCastException when using bound method of Java instance

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 8u20
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:

      Description

      FULL PRODUCT VERSION :
      Bug seen on OS X + Linux 64 with both 1.8.0_20-ea-b23 and 1.9.0-ea-b23

      ADDITIONAL OS VERSION INFORMATION :
      Bug seen on OS X (10.9) + various Linux 64 with both 1.8.0_20-ea-b23 and 1.9.0-ea-b23

      A DESCRIPTION OF THE PROBLEM :
      Placing a reference to a bound java method into a javascript variable then calling it is not equivalent to calling it directly

      var ArrayList = Java.type("java.util.ArrayList")
      al = new ArrayList()
      al.size() // WORKS
      var size = al.size
      size() // ClassCastException

      java.lang.ClassCastException: Cannot cast jdk.nashorn.internal.runtime.Undefined to java.util.ArrayList
      at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:312)
      at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:307)
      at jdk.nashorn.internal.scripts.Script$Recompilation$514$\^eval\_.:program(bx<Untitled>:2)
      at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:567)
      at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:222)
      at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:376)
      at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:465)
      at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:429)
      at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:425)
      at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:158)

      Whereas with a pure JavaScript object, it works as expected:

      Foo = function(){}
      Foo.prototype.banana = function(){print("BANANA");}
      foo = new Foo()
      foo.banana() // WORKS
      f = foo.banana
      f() // WORKS

      It seems that there isn't anything that one can actually legally do with a reference to al.size other than toString it. This is unexpected, but if it is to be the case, a better and more descriptive exception than a ClassCastException would be helpful.



      REPRODUCIBILITY :
      This bug can be reproduced always.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: