在 Java 和 (Rhino) Javascript 之间传递通用类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6730062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:39:42  来源:igfitidea点击:

Passing common types between Java and (Rhino) Javascript

javajavascripttype-conversionrhino

提问by The Archetypal Paul

I'm unclear on the rules for how types are converted between Javascript and Java when using (Mozilla) Rhino.

我不清楚在使用(Mozilla)Rhino 时如何在 Javascript 和 Java 之间转换类型的规则。

There's some specifics in the documentation about String:

文档中有一些关于 的细节String

It's important to keep in mind that Java strings and JavaScript strings are not the same […]Rhino provides some help in reducing the differences between the two types. First, you can pass a JavaScript string to a Java method that requires a Java string and Rhino will perform the conversion. We actually saw this feature in action on the call to the java.lang.String constructor in the preceding example. Rhino also makes the JavaScript methods available to Java strings if the java.lang.String class doesn't already define them

重要的是要记住 Java 字符串和 JavaScript 字符串是不一样的 […]Rhino 提供了一些帮助来减少这两种类型之间的差异。首先,您可以将 JavaScript 字符串传递给需要 Java 字符串的 Java 方法,Rhino 将执行转换。在前面的示例中,我们实际上在调用 java.lang.String 构造函数时看到了这个特性。如果 java.lang.String 类还没有定义它们,Rhino 也会使 JavaScript 方法可用于 Java 字符串

But what about others? If I pass a javascript Number to a Java method expecting int, double(or Integeror Double) will it get converted? What about long/Long? (which won't fit in a Doubleand so won't fit in a JS number?

但是其他人呢?如果我将 javascript Number 传递给期望int, double(or Integeror Double)的 Java 方法,它会被转换吗?怎么样long/ Long?(哪个不适合 a Double,所以也不适合 JS 编号?

What about Java methods returning these values?

返回这些值的 Java 方法呢?

Then there's Boolean/boolean. Are the JS constants trueand falseconverted to and from the appropriate Java value? I've seen code like

然后是Boolean/ boolean。是JS常量truefalse转换,并从相应的Java值?我见过这样的代码

java.lang.Boolean.TRUE.booleanValue()

used from JS, so at least some people think it isn't.

从 JS 使用,所以至少有些人认为它不是。

I have looked at the Mozilla Rhino documentation but do point out if I've missed something obvious.

我已经查看了 Mozilla Rhino 文档,但请指出我是否遗漏了一些明显的内容。

采纳答案by pingw33n

Here's how it converts JavaScript types to Java types: http://www-archive.mozilla.org/js/liveconnect/lc3_method_overloading.html#InvocationConversion.

以下是它如何将 JavaScript 类型转换为 Java 类型:http: //www-archive.mozilla.org/js/liveconnect/lc3_method_overloading.html#InvocationConversion

Try it:

试试看:

$ java -cp js.jar org.mozilla.javascript.tools.shell.Main

js> new java.lang.Integer(12345)
12345
js> new java.lang.Integer(12345) == 12345
true

js> new java.lang.Double(12345.12345)
12345.12345

js> new java.lang.Long(9223372036854775807)                 
js: Cannot convert 9223372036854776000 to java.lang.Long
js> 9223372036854775807
9223372036854776000
js> new java.lang.Long("9223372036854775807")
9223372036854775807
js> new java.lang.Long("-9223372036854775808")
-9223372036854775808

js> new java.lang.Boolean(true)
true
js> new java.lang.Boolean(true) == true
true
js> new java.lang.Boolean(true) == false
false
js> java.lang.Boolean.TRUE.booleanValue() == true
true
js> java.lang.Boolean.FALSE.booleanValue() == false
true

UPD

UPD

Unfortunately I can't find any docs about JavaScript-from-Java type mapping either. But the tutorialshows that JavaScript objects are inserted into and retrieved from context as Java Objects that actually can be Doubles, Booleans, Functions (for JavaScript functions; also implements Scriptable) or Scriptables (for objects).

不幸的是,我也找不到任何关于 JavaScript-from-Java 类型映射的文档。但是本教程显示 JavaScript 对象作为 Java Objects插入到上下文中并从上下文中检索,Java s 实际上可以是Doubles, Booleans, Functions (对于 JavaScript 函数;也可以实现Scriptable)或Scriptables (对于对象)。

Using this code snippetit's possibly to get JavaScript-Java type mapping reference:

使用此代码片段可能会获得 JavaScript-Java 类型映射参考:

https://gist.github.com/1089320#file_java_script_java_type_mapping.textile

https://gist.github.com/1089320#file_java_script_java_type_mapping.textile

As for LiveConnect compatibility. If you are referring to this footnote:

至于 LiveConnect 兼容性。如果您指的是此脚注:

The ability to call Java from JavaScript was first implemented as part of a Netscape browser technology called LiveConnect. However, since that technology also encompassed communication with browser plugins, and since the way of calling JavaScript from Java in Rhino is entirely different, that term won't be used in this paper.

从 JavaScript 调用 Java 的能力最初是作为 Netscape 浏览器技术 LiveConnect 的一部分实现的。但是,由于该技术还包含与浏览器插件的通信,并且由于在 Rhino 中从 Java 调用 JavaScript 的方式完全不同,因此本文将不使用该术语。

I think it's about using JavaScript from Java is different from LiveConnect specification. Using Java from JavaScript should be the same.

我认为这是关于使用 Java 中的 JavaScript 与 LiveConnect 规范不同。从 JavaScript 使用 Java 应该是一样的。

回答by lama12345

Actually I had a problem even with the "automatic" conversion, ending up converting myself:

实际上,即使使用“自动”转换,我也遇到了问题,最终转换了自己:

function javaToJavaScript(str)
{
    len = str.length();
    tmp = "";
    for (var i=0; i<len; i++)
        tmp += String.fromCharCode(str.charAt(i));
    return tmp;
}

回答by Phil.Ng

This was solved by having the wrapper factory do the work for me in the situations where string doesn't implement all of the addons that js string has (like how string.length !== string.length() )

在 string 没有实现 js string 具有的所有插件的情况下,通过让包装工厂为我完成工作来解决这个问题(例如 string.length !== string.length() )

This can be set at the top level with:

这可以在顶层设置:

context = Context.enter();

//This tells Rhino to convert String, Number, Boolean and Character into their JavaScript equivalents when returning from a Java function
context.getWrapFactory().setJavaPrimitiveWrap(false);

context.initStandardObjects();