Java to JavaScript using GWT compiler

17,453

Solution 1

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() {[email protected]::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

Solution 2

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.

The big advantage is, that you can maintain your projekt in Java and compile it with GWT to 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.

Working in the JavaScript-output of GWT just isn't that easy and is really a lot of work!

Update:

By a hint of David, I found the 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. Have a try and let us know ...

Solution 3

Maybe I can answer your second question: "If GWT compiler can't do this, can some other tool?"

I am using Java2Script for quite a while now, also on quite large projects. 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.

Udo

Solution 4

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. More information: https://code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en

Share:
17,453

Related videos on Youtube

Boris Jockov
Author by

Boris Jockov

A freelance software developer.

Updated on June 14, 2022

Comments

  • Boris Jockov
    Boris Jockov almost 2 years

    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, 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.

    • Erik
      Erik
      hav a test with -style PRETTY instead of DETAILED - perhaps this will do it.
    • Frodo Baggins
      Frodo Baggins
      Is the part from the "$" onwards some predictable string, up to the next underline?
  • Boris Jockov
    Boris Jockov over 12 years
    This approach would work assuming I am to keep the Java code. I just need a tool to convert Java method to JavaScript function. To save me the trouble of reimplementing the same algorithm in JavaScript.
  • Riley Lark
    Riley Lark over 12 years
    But you could use this method to identify the relevant JavaScript function that the compiler creates. You asked for a predictable naming scheme, and this is it! window.YourClass.yourMethod will point you to the new obfuscated name in JavaScript.