Take the following test:
public static class Scripted {
public void setThing(List<?> list) {
System.out.println("Set via list");
}
public void setThing(Object[] array) {
System.out.println("Set array");
}
}
@Test
public void testScripting() throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
engine.getContext().setAttribute("s", new Scripted(), ScriptContext.ENGINE_SCOPE);
engine.eval("s.thing = Array(1, 2, 3);");
}
With the version of Rhino shipping with Java 7, if you run this, you will get an exception like this:
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: The choice of Java constructor setThing matching JavaScript argument types (object) is ambiguous; candidate constructors are:
void setThing(java.util.List)
void setThing(java.lang.Object[]) (<Unknown source>#1) in <Unknown source> at line number 1
The Object[]
overload existence in the first place is because the previous version of Rhino would not automatically convert arrays to List
, but it would convert them to Object[]
.
If this were a personal project this is where I would just delete the Object[]
overload. The problem is that this is a public API and there could be someone calling that method right now. I would still like to upgrade to Java 7 but I would like to avoid upsetting either the JavaScript users or the people using the array version of the method.
Is there a way to hide the Object[]
overloaded methods from Rhino while others would still be able to call them?