在 Java 和 (Rhino) Javascript 之间传递通用类型
我不清楚使用 (Mozilla) Rhino 时如何在 Javascript 和 Java 之间转换类型的规则。
文档中有一些关于 String
的细节:
请务必记住 Java 字符串和 JavaScript 字符串不相同 [...]Rhino 提供了一些帮助来减少 两种类型之间的差异。首先,你可以传递一个JavaScript 字符串到需要 Java 字符串的 Java 方法,Rhino 将 执行转换。我们实际上看到了这个功能在 调用前面示例中的 java.lang.String 构造函数。 Rhino 还使 JavaScript 方法可用于 Java 字符串,如果 java.lang.String 类尚未定义它们
,但是其他类呢?如果我将 javascript Number 传递给需要 int
、double
(或 Integer
或 Double
)的 Java 方法,它会转变? long
/Long
怎么样? (它不适合 Double
,因此也不适合 JS 数字?
返回这些值的 Java 方法怎么样?
然后是 Boolean
/boolean JS 常量
true
和 false
是否与适当的 Java 值相互转换
java.lang.Boolean.TRUE.booleanValue()
? 认为不是。
我 Mozilla Rhino 文档,但请指出我是否遗漏了一些明显的内容。
I'm unclear on the rules for how types are converted between Javascript and Java when using (Mozilla) Rhino.
There's some specifics in the documentation about 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
But what about others? If I pass a javascript Number to a Java method expecting int
, double
(or Integer
or Double
) will it get converted? What about long
/Long
? (which won't fit in a Double
and so won't fit in a JS number?
What about Java methods returning these values?
Then there's Boolean
/boolean
. Are the JS constants true
and false
converted to and from the appropriate Java value? I've seen code like
java.lang.Boolean.TRUE.booleanValue()
used from JS, so at least some people think it isn't.
I have looked at the Mozilla Rhino documentation but do point out if I've missed something obvious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是它将 JavaScript 类型转换为 Java 类型的方法: http://www-archive .mozilla.org/js/liveconnect/lc3_method_overloading.html#InitationConversion。
尝试一下:
UPD
不幸的是,我也找不到任何有关 JavaScript-from-Java 类型映射的文档。但是 教程 显示 JavaScript 对象作为 Java < 插入到上下文中并从上下文中检索code>Object 实际上可以是
Double
、Boolean
、Function
(对于 JavaScript 函数;还实现Scriptable
)或Scriptable
(对于对象)。使用此代码片段可以获得JavaScript-Java类型映射参考:
https://gist.github.com/1089320#file_java_script_java_type_mapping.textile
至于 LiveConnect 兼容性。如果您指的是此脚注:
我认为这是关于使用 Java 中的 JavaScript 与 LiveConnect 规范的不同。从 JavaScript 使用 Java 应该是一样的。
Here's how it converts JavaScript types to Java types: http://www-archive.mozilla.org/js/liveconnect/lc3_method_overloading.html#InvocationConversion.
Try it:
UPD
Unfortunately I can't find any docs about JavaScript-from-Java type mapping either. But the tutorial shows that JavaScript objects are inserted into and retrieved from context as Java
Object
s that actually can beDouble
s,Boolean
s,Function
s (for JavaScript functions; also implementsScriptable
) orScriptable
s (for objects).Using this code snippet it's possibly to get JavaScript-Java type mapping reference:
https://gist.github.com/1089320#file_java_script_java_type_mapping.textile
As for LiveConnect compatibility. If you are referring to this footnote:
I think it's about using JavaScript from Java is different from LiveConnect specification. Using Java from JavaScript should be the same.
实际上,即使是“自动”转换,我也遇到了问题,最终我自己进行了转换:
Actually I had a problem even with the "automatic" conversion, ending up converting myself:
这是通过让包装工厂在 string 没有实现 js string 具有的所有插件的情况下为我完成工作来解决的(例如 string.length !== string.length() )
这可以设置为顶层:
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() )
This can be set at the top level with: