Discussions

News: STJS: Javascript code generation from Java source code

  1. The subject is not new (GWT is there for a while already). Strongly –typed languages seem to keep being interesting in a developers’ world very attracted currently by dynamic, weakly typed languages. Dart is also another example. We believe that for bigger Javascript projects (let’s say more than 2000 lines of code), the Javascript becomes very hard to maintain, almost impossible to refactor. And even though the IDEs support for Javascript is increasing, it’s still way behind the one for Java. Try renaming a function in a Javascript project (one that has the same name as other function that you don’t want to rename)!

    GWT seemed to be the ideal solution, but as with any development project, it’s very hard to satisfy everybody!  We don’t believe in big frameworks, all-in-one approach. We prefer smaller library focused on a single thing, that integrates nicely with other libraries that excel in their domain.

    Our open source project “Strongly-typed Javascript” (STJS) focuses only on that part – generate the Javascript code from a Java code. We took Java as the input language as our server-side is written also in Java. So it becomes easier to share objects between client and server (via AJAX for example).

    The project is provided as a Maven plugin that is executed after the Java sources of your project are compiled. Using the Maven-Eclipse integration you can also have the Javascript generated right after the input Java source file is saved (no need to re-build the project). This makes the developing roundtrip very short and it allows you to see the changes with a simple refresh of the page.

    Our main concern when we built the library was to stay as close as possible to the initial code. Very few things are modified between the Java source and its Javascript counterpart. When debugging in a browser it’s cool to see a meaningful code! Here is a short example:

    Java:

    form.find(".ok").click(new EventHandler() {

     public boolean onEvent(Event evt, Element THIS) {

        $(that.row(stockData)).appendTo("table tbody");

                    return true;

      }

    });

    JavaScript:

    form.find(".ok").click(function(evt) {

      $(that.row(stockData)).appendTo("table tbody");

      return true;

    });

     

    As you could figure out from the example, we don’t propose our GUI components - JQuery and the community around it do already a great job. We only built a bridge that allows you to use JQuery and JQuery UI plugins directly in the Java code.

    If you’re interested in our project, feel free to have a look on the project’s web site http://st-js.sourceforge.net, give it a try and don’t hesitate to send us your feedback.

     

    Threaded Messages (3)

  2. Applets[ Go to top ]

    Seems people start to understand that Java is the better language. The next step after compiling Java to Javascript, is to omit the Javascript part and execute the original Java code. The old among us will remember: these were called applets.

  3. Applets[ Go to top ]

    IMHO the applets had a major flaw: they proposed a new way of building interface instead of interacting more with the web page, HTML being a better way to express interfaces than AWT/Swing way. Now, seeing the direction taken by Google with Dart, who knows - maybe Oracle will partner with Microsoft (?) or Mozilla and will make Java to be directly executed by the browser (as a replacement of Javascript). But they should do that when the closures are in Java, so that the migration would be smoother.

     

  4. STJS experience[ Go to top ]

    We're working with STJS in one of our current projects that has an HTML rich client interface with the help of JQuery. STJS has helped us improve the quality of the code by permitting us to have a compile time verification of the code. Given the quantity of Javascript and the number of developers, working directly in Javascript was quite painful. Bugs could be introduced really easily and the quantity of JS dead code was annoying.

    Definitely recommended for teams working on HTML Rich Client Interface app that don't want to use a web framework like GWT or Vaadin.