Java to JavaScript使用GWT编译器 [英] Java to JavaScript using GWT compiler

查看:99
本文介绍了Java to JavaScript使用GWT编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些我想转换为JavaScript的Java代码。
我想知道是否可以使用GWT编译器将所提到的Java代码编译为JavaScript代码,以保留方法,变量和参数的所有名称
我尝试使用-draftCompile关闭代码优化来编译它,但方法名称已被破坏。
如果GWT编译器不能这样做,可以使用其他工具吗?
$ b $ p更新

Java代码仅对GWT模拟类具有依赖性,因此GWT编译器肯定能够处理它。



更新2

这个Java方法:

  public String method )

被翻译成此JavaScript功能:

 函数com_client_T_ $ method__Lcom_client_T_2Ljava_lang_String_2()

使用编译器选项:

  -style Detailed 
-optimize 0
-draftCompile

所以名字不能保存。但是有没有办法控制它们被改变的方式?



澄清

比方说,你有一个用Java编写的排序算法(或其他一些简单的Maths工具)。 sort()方法接受一个整数数组。并返回这些整数数组排序。现在说,我有Java和JavaScript应用程序。我想在Java中编写这个方法一次,通过GWT编译器运行它,或者保持方法名称相同,或者以可预测的方式更改方法名称,以便我可以检测并知道如何将其更改回来进行排序( )。然后,我可以将该代码放入JavaScript应用程序中并使用它。如果Java版本更改,我也可以自动重新生成它。我在技术上有一个非常好的理由,我理解高水平的GWT概念,我只是在寻找这个问题的答案。



结论
$ b 主要问题的答案是 NO
虽然方法名可以稍微保留,但它的正文不可用。它内部的方法调用分散在整个生成的文件中,因此它们不能用在JavaScript库中,这是本主题的重点。

虽然你可以设置编译器输出'漂亮'的代码,但我建议你为你想从GWT项目之外调用的类编写导出函数。我相信在GWT文档中有详细说明如何做到这一点,但是我找不到它,所以我在这里创建了一个例子。

  public YourClass(){
...
}

public void yourMethod(){
...


public static YourClass create(){
return new YourClass();


public final static void export()/ * - {
$ wnd.YourClass = function(){
this.instance = new @your。 package.name.YourClass :: create()()
}

var _ = $ wnd.YourClass.prototype;
_.yourMethod = function(){this.instance。@ your.package.name.YourClass :: yourMethod()()}
} - * /;
}

编辑

详细说明,您的代码会像正常一样被混淆,但是由于导出功能,您可以轻松地在外部引用这些函数。您 不得在JavaScript中重写Java类中的任何内容。您只能在JavaScript中编写引用,因此您可以执行此操作:

  var myInstance = new YourClass(); 
myInstance.yourMethod();

当然,您必须从GWT应用程序的某处调用静态导出方法(很可能在您的EntryPoint)来完成这项工作。



有关从JavaScript引用Java方法的更多信息:
http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#methods-fields


I have some Java code written that I'd like to convert to JavaScript. I wonder if it is possible to use the GWT compiler to compile the mentioned Java code into JavaScript code preserving all the names of the methods, variables and parameters. I tried to compile it with code optimizations turned off using -draftCompile but the method names are mangled. If GWT compiler can't do this, can some other tool?

Update

The Java code would have dependencies only to GWT emulated classes so the GWT compiler would definitely be able to process it.

Update 2

This Java method :

public String method()

got translated to this JavaScript funciton :

function com_client_T_$method__Lcom_client_T_2Ljava_lang_String_2()

using the compiler options :

-style DETAILED
-optimize 0
-draftCompile

So names can't be preserved. But is there a way to control how they are changed?

Clarification

Say, for example, you have a sort algorithm written in Java (or some other simple Maths utility). The method sort() takes an array of integers. and returns these integers in an array sorted. Say now, I have both Java and JavaScript applications. I want to write this method once, in Java, run it through the GWT compiler and either keep the method name the same, or have it change in a predictable way, so I can detect it and know how to change it back to sort(). I can then put that code in my JavaScript application and use it. I can also automatically re-generate it if the Java version changes. I have a very good reason technically for this, I understand the concepts of GWT at a high level, I'm just looking for an answer to this point only.

Conclusion

The answer to the main question is NO. While method name can be somewhat preserved, it's body is not usable. Method calls inside it are scattered throughout the generated file and as such, they can't be used in a JavaScript library which was the whole point of this topic.

解决方案

Although you can set the compiler to output 'pretty' code, I suggest you write export functions for the classes you want to call from outside your GWT project. I believe somewhere in the GWT documentation it's detailed how to do this, but I couldn't find it so here an example I just created.

class YourClass {
    public YourClass() {
        ...
    }

    public void yourMethod() {
        ...
    }

    public static YourClass create() {
        return new YourClass();
    }

    public final static native void export() /*-{
          $wnd.YourClass = function() {
              this.instance = new @your.package.name.YourClass::create()()
          }

          var _ = $wnd.YourClass.prototype;
          _.yourMethod = function() {this.instance.@your.package.name.YourClass::yourMethod()()}
    }-*/;
}

EDIT

To elaborate, your code will get obfuscated like normal, but thanks to the export function, you can easily reference those functions externally. You don't have to rewrite anything from your Java class in JavaScript. You only write the references in JavaScript, so you can do this:

var myInstance = new YourClass();
myInstance.yourMethod();

Of course you have to call the static export method from somewhere in your GWT app (most likely in your EntryPoint) to make this work.

More info about referencing Java methods from JavaScript: http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#methods-fields

这篇关于Java to JavaScript使用GWT编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆