Chapter 8. Code Translation

 

"The question of whether computers can think is like the question of whether submarines can swim."

 Edsger W. Dijkstra
Table of Contents
Generated JavaScript files
Doing java in HTML documents
Types
Exceptions

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.

Figure 8-1. Concepts from both languages and what J2S takes from both worlds

Generated JavaScript files

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:


Clazz.declarePackage ("my.first.project");                (1)
c$ = Clazz.declareType (my.first.project, "HelloWorld");  (2)
Clazz.defineMethod (c$, "getName", function () {          (3)
  return this.getClass().getName() + this.hashCode();
});
c$.main = Clazz.defineMethod (c$, "main", function (args) {(4)
  var obj =  new my.first.project.HelloWorld ();
  System.out.println ("object name " + obj.getName ());
}, "~A");
(1)
this is the translation of the java statement package my.first.project; , declaring the package my.first.project. As with java, it is required before a class definition.
(2)
This is the translation of the java code public class HelloWorld . With Clazz.declareType (my.first.project, "HelloWorld") we declare the new class HelloWorld of package my.first.project an it returns the new created class object that we later can use for adding methods and such things.
(3)
This is the translation of the java code public class HelloWorld . With Clazz.declareType (my.first.project, "HelloWorld") we declare the new class HelloWorld of package my.first.project an it returns the new created class object that we later can use for adding methods and such things.
(4)
this is how the static method main is defined in javascript. Note that we pass the following parameters to Clazz.defineMethod : class object (returned by declareType), method name, method function.

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:

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:


Clazz.declarePackage ("my.first.project");
Clazz.load (["java.util.HashMap"], "my.first.project.MyTable", null, function () {(1)
  c$ = Clazz.declareType (my.first.project, "MyTable", java.util.HashMap);(2)
  Clazz.defineMethod (c$, "print", function () {          (3)
    var s = "";
    var i = this.keySet ().iterator ();
    while (i.hasNext ()) {
      s += i.next () + ",";
    }
    return s;
  });
});
(1)
This is different that our previous example. Since in this case, our class extends java.util.HashMap, we need first of all, that java.util.HashMap to be loaded and only then declare our new type. The function Clazz.load help us on this. Note that the actual call to Clazz.declareType is inside the callback function passed to Clazz.load.
(2)
At this point the required class java.util.HashMap is loaded and so we can call Clazz.declareType. Notice the last argument java.util.HashMap referencing the parent class. Note that in javascript we can reference java Class objects just like in java.
(3)
Notice that calling this.keySet() is done just like in java, with the exception that in javascript the this keyword is not optional.