Skip to content

Instantly share code, notes, and snippets.

@walterhiggins
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walterhiggins/0d1892bf7d5430527075 to your computer and use it in GitHub Desktop.
Save walterhiggins/0d1892bf7d5430527075 to your computer and use it in GitHub Desktop.
Nashorn class cast exception for vararg method params

Problem

Nashorn has trouble casting JS arrays to native java arrays when invoking vararg methods.

I'm calling a Java method from JS. The java method uses variable arguments...

http://docs.visualillusionsent.net/CanaryLib/1.2.0/src-html/net/canarymod/api/inventory/helper/FireworkHelper.html#line.317

In my JS I construct an array variable and populate the array:

var cmDyeColor = Packages.net.canarymod.api.DyeColor;
var colors = [ cmDyeColor.ORANGE, cmDyeColor.RED ];
...
cmFireWorkHelper.addStarColors(item, colors);

The last statement above generates the following error...

Caused by: java.lang.ClassCastException: Cannot cast jdk.nashorn.api.scripting.ScriptObjectMirror to [Lnet.canarymod.api.DyeColor;
	at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
	at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
	at jdk.nashorn.internal.scripts.Script$Recompilation$342$762A$\^eval\_.L:1$canaryFirework(<eval>:26)

Incidentally, this also seems to be a problem when casting to arrays of primitives e.g. [int] see http://docs.visualillusionsent.net/CanaryLib/1.2.0/src-html/net/canarymod/api/inventory/helper/FireworkHelper.html#line.350

relevant properties

java.runtime.name=Java(TM) SE Runtime Environment
java.vm.version=25.40-b25
user.country.format=IE
java.vm.vendor=Oracle Corporation
java.vendor.url=http://java.oracle.com/
path.separator=:
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg=sun.io
sun.java.launcher=SUN_STANDARD
sun.os.patch.level=unknown
java.vm.specification.name=Java Virtual Machine Specification
java.runtime.version=1.8.0_40-b25
java.awt.graphicsenv=sun.awt.CGraphicsEnvironment
os.arch=x86_64
java.vm.specification.vendor=Oracle Corporation
os.name=Mac OS X
sun.jnu.encoding=UTF-8
java.specification.name=Java Platform API Specification
java.class.version=52.0
sun.management.compiler=HotSpot 64-Bit Tiered Compilers
os.version=10.10.3
java.specification.version=1.8
java.vm.specification.version=1.8
sun.arch.data.model=64
java.specification.vendor=Oracle Corporation
awt.toolkit=sun.lwawt.macosx.LWCToolkit
java.vm.info=mixed mode
java.version=1.8.0_40
java.vendor=Oracle Corporation
java.vendor.url.bug=http://bugreport.sun.com/bugreport/
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeBig
@szegedi
Copy link

szegedi commented Jun 1, 2015

This is a known issue that is fixed, but it unfortunately missed the window to be included in 8u40; apologies for the inconvenience. It should work with any current Early Access build of 8u60.

@szegedi
Copy link

szegedi commented Jun 1, 2015

Using an explicit Java.to invocation should work as a workaround in the meantime:

    var DyeColorArrayType = Java.type("net.canarymod.api.DyeColor[]");
    ...
    cmFireWorkHelper.addStarColors(item, Java.to(colors, DyeColorArrayType));

I know it ain't pretty, but it's a way to make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment