JDK-8146151 : Indexed access of Java methods on JS primitive types does not work
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Priority: P4
  • Status: Resolved
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2015-12-24
  • Updated: 2017-11-17
  • Resolved: 2017-11-17
Related Reports
Duplicate :  
Description
This issue was reported by Axel Faust via https://twitter.com/ReluctantBird83/status/679600616595918848 (see also JI-9027774).

Test case is here -> http://fpaste.org/304235/50864308/

Test case copied here for convenience:

    var nr = Packages.java.lang.Double.valueOf(2.5);
    var prop = 'int';
    print(nr[prop + 'Value']); // yields undefined instead of method handle for intValue()



Comments
This is too costly to fix. The issue here is that calling Java methods on JS primitive objects with computed property. If we don't find a named property in JS primitive wrapper (like Number, String), we delegate to Beans linker from PrimitiveLookup for named property access. For indexed properties, we form the NativeNumber, NativeString etc. and follow the usual linking based on ScriptObject linking. If we have to support indexed access to Java methods on primitives, we have to override index handling for NativeNumber/String etc. - for such a corner case! Besides there are two easy workarounds for this: * Avoid computed property indexed access for Java methods on JS primitives - use hard-coded index property or normal property access - both are handled as expected. * It is possible to define Java methods as methods on Number.prototype, String.prototype etc. There are not too methods and a useful subset can be defined by user's initialization script(s). // install java.lang.Double.isNaN as Number.prototype extension property Number.prototype.isNaN = function() { var func = Function.prototype.bind.call(java.lang.Double.isNaN, this); return func(this) } // normal invocation works. print(344.2.isNaN()); // computed property / indexed access works as expected. var x = "NaN" print(445.44["is" + x]()); print(NaN["is" + x]());
17-11-2017

There is workaround - namely to use avoid "computed property" for Java methods.
18-05-2016

Note that this issue is different from JDK-8146147 in that this issue occurs because of indexed getter issue with JS primitive linkers (and not Java linker of dynalink).
24-12-2015