Using javap , I believe that the JavaScript test type does not mean that the resulting Java type is this class. The generated Java type calls script code in its constructor; this will not expose addNumbers as a Java method.
>javap -classpath . test public class test extends org.mozilla.javascript.NativeFunction implements org.m ozilla.javascript.Script{ public test(org.mozilla.javascript.Scriptable, org.mozilla.javascript.Contex t, int); public test(); public static void main(java.lang.String[]); public final java.lang.Object exec(org.mozilla.javascript.Context, org.mozil la.javascript.Scriptable); public final java.lang.Object call(org.mozilla.javascript.Context, org.mozil la.javascript.Scriptable, org.mozilla.javascript.Scriptable, java.lang.Object[]) ; public int getLanguageVersion(); public java.lang.String getFunctionName(); public int getParamCount(); public int getParamAndVarCount(); public java.lang.String getParamOrVarName(int); public java.lang.String getEncodedSource(); public boolean getParamOrVarConst(int); }
Reading between the lines, I would say you need to map Java types to do what you want. From jsc doc:
-implements java-intf-name
Indicates that the java class implementation of the Java interface java-intf-name should be generated from an incoming JavaScript file source. Each global function in the source file is made by a generated class method that implements any methods in the interface with the same name.
Define this interface:
//Adder.java public interface Adder { public int addNumbers(int a, int b); }
Write this implementation:
Compile JavaScript with the -implements Adder AdderImpl.js . Call the method as follows:
Adder adder = new AdderImpl(); int n = adder.addNumbers(1, 2); System.out.println(n);
I would venture to suggest that this is probably necessary due to differences in language type systems.
I used Rhino 1.7R2. For brevity, I avoided using packages, etc.
source share