-
-
Save walterhiggins/8de7a93e8b57e62aa16d to your computer and use it in GitHub Desktop.
var cmRecipe = Packages.net.canarymod.api.recipes.CraftingRecipe; | |
var cmRecipeRow = Packages.net.canarymod.api.recipes.RecipeRow; | |
// see https://ci.visualillusionsent.net/job/CanaryLib/javadoc/net/canarymod/api/inventory/recipes/CraftingRecipe.html | |
var rows = [ | |
new cmRecipeRow("ESW", [ enderPearl, stick, string]), | |
new cmRecipeRow("SEW", [ stick, enderPearl, string]), | |
new cmRecipeRow("ESW", [ enderPearl, stick, string]) | |
]; | |
var signature = "(net.canarymod.api.inventory.Item, net.canarymod.api.inventory.recipes.RecipeRow[])"; | |
var enderBowRecipe = new cmRecipe[signature]( enderBow, rows); |
Does this mean that if a Java method has two overloads each of which have the same number of parameters and both methods have variable argument parameters, then explicit overload resolution will fail?
public class A {
public A( B... ) {
}
public A( C... ) {
}
}
var a = new A( [ new B(), new B() ] ) ;// fails
var a = new A( [ new C(), new C() ] ); // fails
var a = new A["(B[])"]( [ new B(), new B() ]) ; // also fails as it can't be resolved!
You have several options (sorry, I'll be typing these off the top of my head, not trying them out myself, so minor errors are possible).
1. Explicit constructor overload selection
This is something that will only work in Java 8u40 (it's already available in the early access builds). However, you'd use the explicit signature as part of the type name on the package, and not as a property of the constructor object itself:
var cmRecipeCtor = Packages.net.canarymod.api.recipes["CraftingRecipe(Item, RecipeRow[])"];
(Notice, by the way, that you can use unqualified type names for the signature as Nashorn is smart enough to know that you're selecting among a finite number of overloads and can figure out what you meant. You would only need to use qualified type names in case two overloads could only be distinguished by parameter types in the same position that have the same unqualified name but different packages. Which is practically (hopefully) never.)
2. Coerce the array argument into explicit Java array type
This solution is available since Nashorn was first released in Java 8; you just use Java.to
with a specific Java array type:
var cmRecipeRowArray = Java.type("net.canarymod.api.recipes.RecipeRow[]");
var enderBowRecipe = new cmRecipe(enderBow, Java.to(rows, cmRecipeRowArray));
Hope this helps,
Attila.
When this code is executed I get the following error:
If I execute without an explicit signature...
I get this error...
java.lang.NoSuchMethodException: Can't unambiguously select between fixed arity signatures [(net.canarymod.api.inventory.Item, net.canarymod.api.inventory.Item[]), (net.canarymod.api.inventory.Item, net.canarymod.api.inventory.recipes.RecipeRow[])] of the method net.canarymod.api.inventory.recipes.CraftingRecipe. for argument types [net.canarymod.api.inventory.CanaryItem, jdk.nashorn.internal.objects.NativeArray]