简体   繁体   English

Rhino Javascript编译器

[英]Rhino Javascript Compiler

I found the post about compiling javascript to java using Rhino compiler. 我找到了有关使用Rhino编译器将JavaScript编译为Java的帖子 I was able to get the simple case to work and invoke the methods in java. 我能够得到一个简单的案例来工作并调用java中的方法。 However, I have some questions, and hope I can get them answered here. 但是,我有一些问题,希望我能在这里得到解答。

  1. How do I compile the below code to TestObject.class with method (setTmpValue,getTmpValue,getType) and constructor of 1 arguments? 如何使用方法(setTmpValue,getTmpValue,getType)和1个参数的构造函数将以下代码编译为TestObject.class? Or it is not possible? 还是不可能?

    function TestObject(params) { this.type= params.type; var tmpValue = 0; this.setTmpValue = function ( val ) { tmpValue = val; }; this.getTmpValue = function () { return tmpValue; }; this.getType = function () { return type }; }

  2. Is it possible to refer a class that will be compiled from other js file? 是否可以引用将从其他js文件编译的类?

    Example: Can I invoke B in A? or do new B() in A? A.js -> A.class B.js -> B.class

  3. How does the scope work for these compiled classes? 范围如何作用于这些已编译的类?

  4. Is there other documentation than the one Here ? 除了这里的文档,还有其他文档吗?

Thanks in advance for helping out! 在此先感谢您的帮助!

There's an easier way, without compiling; 有一种更简单的方法,无需编译。 it seems that, given your question and comments, your goal is to have JavaScript objects be callable from Java. 似乎,鉴于您的问题和评论,您的目标是让Java对象可调用JavaScript对象。

Here's one way to do it: 这是一种实现方法:

On Java side: 在Java方面:

package mypackage;
public abstract class TestObject {
    // insert public abstract methods
    public abstract void doIt();
}

On JavaScript side: 在JavaScript方面:

var myInstance = new JavaAdapter(
    Packages.mypackage.TestObject,
    {
        doIt: function() {
            // do something
        }
    }
)

Packages.myapplication.MyApplication.setTestObject(myInstance);

Back on Java side: 回到Java方面:

package myapplication;

public class MyApplication {
    private static TestObject test;

    public static void setTestObject(TestObject o) {
        test = o;
    }

    public void doSomething() {
        test.doIt();
    }
}

Basically, JavaScript objects can implement Java interfaces or extend Java abstract classes. 基本上,JavaScript对象可以实现Java接口或扩展Java抽象类。 If they make themselves available on the Java side, the Java side can call them like Java objects. 如果它们在Java端可用,则Java端可以像Java对象一样调用它们。 As far as I can tell, that's what you want. 据我所知,这就是您想要的。 If not, leave a clarification and we'll go from there. 如果没有,请澄清一下,我们将从那里继续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM