简体   繁体   English

Java到JavaScript使用GWT编译器

[英]Java to JavaScript using GWT compiler

I have some Java code written that I'd like to convert to JavaScript. 我编写了一些Java代码,我想将其转换为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. 我想知道是否可以使用GWT编译器将提到的Java代码编译成JavaScript代码, 保留方法,变量和参数的所有名称 I tried to compile it with code optimizations turned off using -draftCompile but the method names are mangled. 我尝试使用-draftCompile关闭代码优化来编译它,但是方法名称被破坏了。 If GWT compiler can't do this, can some other tool? 如果GWT编译器不能这样做,可以使用其他一些工具吗?

Update 更新

The Java code would have dependencies only to GWT emulated classes so the GWT compiler would definitely be able to process it. Java代码只依赖于GWT模拟类,因此GWT编译器肯定能够处理它。

Update 2 更新2

This Java method : 这个Java方法:

public String method()

got translated to this JavaScript funciton : 翻译成这个JavaScript函数:

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). 比如说,您有一个用Java编写的排序算法(或其他一些简单的Maths实用程序)。 The method sort() takes an array of integers. 方法sort()采用整数数组。 and returns these integers in an array sorted. 并在排序的数组中返回这些整数。 Say now, I have both Java and JavaScript applications. 现在说,我有Java和JavaScript应用程序。 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(). 我想用Java编写这个方法一次,通过GWT编译器运行它,并保持方法名称相同,或者以可预测的方式更改它,这样我就可以检测它并知道如何将其更改回排序( )。 I can then put that code in my JavaScript application and use it. 然后我可以将该代码放在我的JavaScript应用程序中并使用它。 I can also automatically re-generate it if the Java version changes. 如果Java版本发生变化,我也可以自动重新生成它。 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. 我在技术上有一个非常好的理由,我在高层次上理解GWT的概念,我只是在寻找这一点的答案。

Conclusion 结论

The answer to the main question is NO . 主要问题的答案是否定的 While method name can be somewhat preserved, its 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. 其中的方法调用分散在整个生成的文件中,因此,它们不能在JavaScript库中使用,这是本主题的重点。

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. 虽然您可以将编译器设置为输出“漂亮”代码,但我建议您为要从GWT项目外部调用的类编写导出函数。 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. 我相信GWT文档中的某个地方详细说明了如何做到这一点,但我找不到它,所以我刚刚创建了一个例子。

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. 不必在JavaScript中重写Java类中的任何内容。 You only write the references in JavaScript, so you can do this: 您只能在JavaScript中编写引用,因此您可以这样做:

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. 当然,您必须从GWT应用程序中的某个位置(最有可能在您的EntryPoint中)调用静态导出方法才能使其工作。

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

Maybe I can answer your second question: "If GWT compiler can't do this, can some other tool?" 也许我可以回答你的第二个问题:“如果GWT编译器不能这样做,可以使用其他工具吗?”

I am using Java2Script for quite a while now, also on quite large projects. 我现在使用Java2Script已经有一段时间了,也是在相当大的项目上。 Integration with native JavaScript is fine, names are preserved, and after some time one can even match the generated JavaScript (in the browser debugger) with the original Java code with little effort. 与本机JavaScript集成很好,名称被保留,并且在一段时间之后,甚至可以轻松地将生成的JavaScript(在浏览器调试器中)与原始Java代码匹配。

Udo 你做

No - this isn't possible with the GWT compiler, since the GWT compiler is build to generate optimized and very performant JavaScript out of Java. 否 - 这是GWT编译器无法实现的,因为构建GWT编译器是为了从Java生成优化且性能非常好的JavaScript。

The big advantage is, that you can maintain your projekt in Java and compile it with GWT to JavaScript. 最大的优点是,您可以在Java中维护您的项目并使用GWT将其编译为JavaScript。 So there is no need to prevent the variable-names and method-names in the JavaScript result, since all changes and work is done in the JAVA-sources. 因此,无需阻止JavaScript结果中的变量名和方法名,因为所有更改和工作都在JAVA源中完成。

Working in the JavaScript-output of GWT just isn't that easy and is really a lot of work! 在GWT的JavaScript输出中工作并不是那么容易,而且真的很多工作!

Update : 更新

By a hint of David, I found the Compiler-Option " -style ". 通过David的一丝暗示,我找到了Compiler-Option“ -style ”。 You can have a try with the following options: 您可以尝试使用以下选项:

-style=PRETTY -optimize=0

I have no idea if this will really generate "human readable" code. 我不知道这是否真的会产生“人类可读”的代码。 I think it won't, since the GWT framework will still be part of the resulting JavaScript and so it will be difficult to make changes to the JavaScript-result. 我认为它不会,因为GWT框架仍然是生成的JavaScript的一部分,因此很难对JavaScript结果进行更改。 Have a try and let us know ... 试试让我们知道......

You can "export" your function by writing inline JavaScript that calls it, and there is a tool gwt-exporter that does this automatically when you annotate classes and methods with @Export and similar. 您可以通过编写调用它的内联JavaScript来“导出”您的函数,并且有一个工具gwt-exporter在您使用@Export和类似注释类和方法时自动执行此操作。 More information: https://code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en 更多信息: https//code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM