有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java和(Rhino)Javascript之间传递公共类型

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

文档中有一些关于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

但其他人呢?如果我将一个javascript数字传递给一个Java方法,该方法需要intdouble(或IntegerDouble),它会被转换吗?那long/Long呢?(哪个不适合Double所以不适合JS编号

返回这些值的Java方法呢

然后是Boolean/boolean。JS常量truefalse是否转换为适当的Java值?我见过这样的代码

java.lang.Boolean.TRUE.booleanValue()

来自JS,所以至少有些人认为不是

我已经看过Mozilla Rhino文档,但是如果我遗漏了一些明显的东西,一定要指出


共 (3) 个答案

  1. # 1 楼答案

    实际上,即使是“自动”转换,我也有一个问题,最终我转换了自己:

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

    在string没有实现js string拥有的所有插件的情况下(比如string.length!==string.length()),让包装器工厂为我做这项工作就解决了这个问题

    这可以通过以下方式在顶层设置:

    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();
    
  3. # 3 楼答案

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

    试试看:

    $ 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

    不幸的是,我也无法从Java类型映射中找到任何关于JavaScript的文档。但是tutorial显示JavaScript对象作为Java Object插入到上下文中并从上下文中检索,实际上可以是DoubleBooleanFunction(对于JavaScript函数;还实现了Scriptable)或Scriptable(对于对象)

    使用this code snippet可能会获得JavaScript Java类型映射引用:

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

    至于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.

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