如何在Rhino引擎中使用javascript到新的java内部类?

时间:2022-06-21 22:30:07

Here I write some js code in kettle Modified Java Script Value step (which use Rhino engine), to use java API. Actually, all the code works well. For example:

这里我在kettle Modified Java Script Value步骤(使用Rhino引擎)中编写一些js代码,以使用java API。实际上,所有代码都运行良好。例如:

var fileInputStream = new java.io.FileInputStream(filePath); //new file object

But if I want to use the inner class, how could I new inner class in js environment? Here is the demo:

但是如果我想使用内部类,我怎么能在js环境中新建内部类?这是演示:

//java
public class Out{
   public static class In {
       public String test = "Hello";
   }
}
//use inner class
Out.In in = new Out.In();
System.out.print(in.test); // successful!!


//js
var out = new Out();    // successful
var in = new Out.In();  // error here.

error message

org.mozilla.javascript.Undefined@55516dbb is not a function, it is undefined

We can find that, javascript compiler view In() as a function of class Out, but actually In() is the inner class of Out.

我们可以发现,javascript编译器视图In()作为类Out的函数,但实际上In()是Out的内部类。

From the Internet, I find that Nashorn, a newer javascript engine, supports scripts access java inner access, which grammar is that

从互联网上,我发现Nashorn是一个较新的javascript引擎,支持脚本访问java内部访问,其语法是

var Float = Java.type("java.awt.geom.Arc2D$Float"); // using $ to indicate inner class

My question is that, how can I new a inner class from Rhino??? There must be several methods somewhere.

我的问题是,如何从Rhino中新建一个内部类?某处必须有几种方法。

Any help is greatly needed!!

非常需要任何帮助!

3 个解决方案

#1


I have no experience with kettle, but according to the error message, Rhino JavaScript engine is used there. I guess, the problem is not with inner classes, but with absence of Packages. prefix. By default only java. top-level package is imported to Rhino JavaScript context. Other classes are accessible using Packages. prefix. If your Out class is located in the default package, try to use

我没有使用水壶的经验,但根据错误信息,在那里使用Rhino JavaScript引擎。我想,问题不在于内部类,而在于没有包。字首。默认只有java。*包导入Rhino JavaScript上下文。可以使用Packages访问其他类。字首。如果您的Out类位于默认包中,请尝试使用

var in = new Packages.Out.In();

If not, prepend it with the full package name. For example, if your Java code appears in com.example package, use

如果没有,请在前面加上完整的包名称。例如,如果您的Java代码出现在com.example包中,请使用

var in = new Packages.com.example.Out.In();

For more information see Rhino documentation.

有关更多信息,请参阅Rhino文档。

#2


Just put your Out class into any package, e.g. org.Out.
Then use it from JavaScript like this new org.Out.In().test;

只需将您的Out类放入任何包中,例如org.Out。然后在JavaScript中使用它,就像这个新的org.Out.In()。test;

You are trying to do smth equivalent to Java Out o = new Out(); new o.In(); // compilation error here

你试图做相当于Java Out o = new Out()的smth;新的o.In(); //这里编译错误

And this also will not work in Java. So I think it is not a Rhino issue at all

这也不适用于Java。所以我认为这根本不是犀牛问题

Also you can try to write var field = new org.pentaho.di.trans.steps.mongodboutput.MongoDbOutputMeta.MongoField();

您也可以尝试编写var field = new org.pentaho.di.trans.steps.mongodboutput.MongoDbOutputMeta.MongoField();

#3


Please try this code.

请试试这段代码。

var out = new Out();  
var in = new Out.In(out);

Inner class constructor requires outer class instance.

内部类构造函数需要外部类实例。

#1


I have no experience with kettle, but according to the error message, Rhino JavaScript engine is used there. I guess, the problem is not with inner classes, but with absence of Packages. prefix. By default only java. top-level package is imported to Rhino JavaScript context. Other classes are accessible using Packages. prefix. If your Out class is located in the default package, try to use

我没有使用水壶的经验,但根据错误信息,在那里使用Rhino JavaScript引擎。我想,问题不在于内部类,而在于没有包。字首。默认只有java。*包导入Rhino JavaScript上下文。可以使用Packages访问其他类。字首。如果您的Out类位于默认包中,请尝试使用

var in = new Packages.Out.In();

If not, prepend it with the full package name. For example, if your Java code appears in com.example package, use

如果没有,请在前面加上完整的包名称。例如,如果您的Java代码出现在com.example包中,请使用

var in = new Packages.com.example.Out.In();

For more information see Rhino documentation.

有关更多信息,请参阅Rhino文档。

#2


Just put your Out class into any package, e.g. org.Out.
Then use it from JavaScript like this new org.Out.In().test;

只需将您的Out类放入任何包中,例如org.Out。然后在JavaScript中使用它,就像这个新的org.Out.In()。test;

You are trying to do smth equivalent to Java Out o = new Out(); new o.In(); // compilation error here

你试图做相当于Java Out o = new Out()的smth;新的o.In(); //这里编译错误

And this also will not work in Java. So I think it is not a Rhino issue at all

这也不适用于Java。所以我认为这根本不是犀牛问题

Also you can try to write var field = new org.pentaho.di.trans.steps.mongodboutput.MongoDbOutputMeta.MongoField();

您也可以尝试编写var field = new org.pentaho.di.trans.steps.mongodboutput.MongoDbOutputMeta.MongoField();

#3


Please try this code.

请试试这段代码。

var out = new Out();  
var in = new Out.In(out);

Inner class constructor requires outer class instance.

内部类构造函数需要外部类实例。