如何在Java程序中调用Rhino编译的JavaScript方法(类文件)? [英] How to invoke Rhino compiled JavaScript methods (class files) in the Java program?

查看:211
本文介绍了如何在Java程序中调用Rhino编译的JavaScript方法(类文件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编译了以下JavaScript文件test.js到test.class:

I compiled following JavaScript file, "test.js", into the "test.class" :

var test = (function () {
  var that = {};

  that.addNumbers = function (a, b) {
    return a+b;
  };

  return that;
}());

我想调用编译的JavaScript函数test.addNumbers(1,2)在简单的Java程序run.java中如下:

I would like to call the compiled JavaScript function, "test.addNumbers(1,2)", in the simple Java program "run.java" as follows :

public class run {

  public static void main(String[] args) throws Exception {

    Context cx = Context.enter();

    try {
      Scriptable scope = cx.initStandardObjects();

      // HOW TO CALL THE METHOD, Test.addNumbers(1,2)?  Please help me!

    } finally {
      Context.exit();
    }
  }
}

失败。我阅读Rhino教程并检查了许多文章和示例,但是它们只显示了如何从命令行或源文件test.js调用JavaScript方法。
我需要从编译的test.class文件中调用该方法。

I tried many ways, but failed. I read Rhino tutorial and examined many articles and examples, BUT they only show how to call JavaScript methods from the command line or the source file, "test.js". I need to call the method from the compiled "test.class" file.

非常感谢您的帮助!

推荐答案

使用 javap ,我相信JavaScript类型 test 并不意味着生成的Java类型是这个类。生成的Java类型在其构造函数中调用脚本代码;这不会导致将 addNumbers 暴露为Java方法。

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);
}

在行之间读取,我想说你需要映射到Java类型做你想要的。从 jsc 文档:

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

指定一个java类
实现Java接口
java-intf-name 应该从传入的JavaScript源代码
生成

源文件中的每个全局函数都是
生成类的方法,在接口中通过相同的
名称实现任何
方法。

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.

定义此界面:

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

编写此实现:

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

使用参数编译JavaScript Adder AdderImpl .js 。调用方法如下:

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.

我使用Rhino 1.7R2。为了简洁,我已避免使用包等。

这篇关于如何在Java程序中调用Rhino编译的JavaScript方法(类文件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆