"The question of whether computers can think is like the question of whether submarines can swim." | |
Edsger W. Dijkstra |
Now that the user knows the basics of using the Java2Script plugin, int this chapter we will examine the generated code and how Java2Script translate java sources to javascript and how we can use this code in our html documents for lowding our translated java applications.
Note that you won't normally need to understand how the compiler javascript code work, since one of the main idea of Java2Script plugin is to abstract the java user from javascript code at all. Nevertheless, it is generally a good idea to be familiar on how J2S generated code works, but of course you can choose to jump to next section.
A java to javascript code translator like J2S must translate each possible java statement to its javascript equivalent. There are some constructions that are very similar in both languages like, for, if, while, do, and other expressions. But there are Java language constructions like class, method, interface, that are not present in javascript. So the compiler has also to add artificial support for these.
In this section we will examine the generated JavaScript files and also the generated HTML document for a basic understanding of how java code is translated to JavaScript and how that JavaScript can be loaded and executed from an HTML document.
Consider the following simple java class that contains one static method and one instance method:
package my.first.project; public class HelloWorld { public String getName() { return this.getClass().getName() + this.hashCode(); } public static void main(String[] args) { HelloWorld obj = new HelloWorld(); System.out.println("object name " + obj.getName()); } } |
And the generated javascript, with comments, is the following:
also notice that the only difference between declaring a static and instance methods is that in the case of static method declaration we assign the returned method to a class object property, i.e., $c.main = ...
As we can see, the Clazz object contains functions that emulate the java language. We will document all this functions in the Section called Java Language Emulation API in Appendix A.
Other thing to notice is that the code translation is linear: for a java statement there is a single javascript statement. Also, it is noticeable that, for the point of view of the translator, there are two main types of Java statements:
Supported by javascript. Most java statements, like method calling, for, while, if, etc. expressions, are supported directly by the javascript language and do not need almost any translation. In the previous example, the java statement System.out.println ("hello world"); remains the same both in java and in javascript.
Containing java concepts not supported by javascript. . Doing Object Oriented programming stuf like cľasses, methods definition, etc. Here is when we use the object Clazz: the J2S java language emulator for javascript. So, for example, if we ewant to define a new class in javascript, we use Clazz.declareType.
Now let's finnish examining a more advance java example and its transjaltion to javascript:
package my.first.project; import java.util.HashMap; import java.util.Iterator; public class MyTable extends HashMap<String, Integer>{ public String print() { String s = ""; Iterator<String> i = keySet().iterator(); while (i.hasNext()) { s += i.next()+","; } return s; } } |
that is translated to the following commented javascript code: