How to invoke Rhino compiled JavaScript methods (class files) in the Java program?

女生的网名这么多〃 提交于 2019-11-28 11:30:00

Using javap, I believe that the JavaScript type test does not mean that the resultant Java type is this class. The generated Java type invokes the script code in its constructor; this will not result in exposing 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'd say you need to map to Java types to do what you want. From the jsc doc:

-implements java-intf-name

Specifies that a java class implementing the Java interface java-intf-name should be generated from the incoming JavaScript source file. Each global function in the source file is made a method of the generated class, implementing any methods in the interface by the same name.

Define this interface:

//Adder.java
public interface Adder {
  public int addNumbers(int a, int b);
}

Write this implementation:

//AdderImpl.js
function addNumbers(a, b) {
  return a+b;
}

Compile the JavaScript with the arguments -implements Adder AdderImpl.js. Invoke the method like so:

Adder adder = new AdderImpl();
int n = adder.addNumbers(1, 2);
System.out.println(n);

I'd hazard a guess that it was probably necessary to do it this way because of differences in the languages' type systems.

I used Rhino 1.7R2. For the sake of brevity, I've avoided using packages, etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!