Debugging

how turn on debuggin?

what happens when debug is on?

JavaScript ClassLoader and Hotspot

If you are familiar with Java debugging, you must know there is a technology called "Hotspot". One feature of Hotspot is to replace old classes bytecodes with a new ones that are generated by dynamic compilers. This feature helps developers a lot in debugging. That is to say, when a developer load a very complex application in debugging mode, he want to modify the sources a little, he can just do it, the compiler will compile those related classes and notify classloader to load those affected classes bytecodes. So the changes are loaded in an application being executed without the need of reloading it. It saves lots of time by avoiding closing, reopening and waiting big applications again and again. This is very convenient when comparing to those static compiled applications written by C or C++ languages.

Let's try it with an example. The following java test, creates an html button with a click event handle. Each time the user clicks the button, the instance method doCLickAction() is called.


package org.sgx.j2s.js;

import org.sgx.j2s.html.HTMLUtils;

public class HotSpotTest1 {
  int counter = 0;  
  
  public static void main(String[] args) {
    new HotSpotTest1().test();
  }
  
  public void test() {
    
    //creates an html button element in document.body, with label "click me" and 
    //with an event handler that will call doCLickAction() instance method
    
    HTMLUtils.createButton(JsUtils.document().body, "click me", new Runnable() {
      public void run() {
        counter++;
        doClickAction();
      }
    });   
  }

  protected void doClickAction() {
    System.out.println(counter+" say hello!");
  }

}

Now we will use J2S Hot Spot for debugging the application making changes to sources that will be reflected in the html document without having to reload the application. For debugging a J2S application with HotSpot, we use Debug As... instead of Run As... in the context menu:

Figure 4-1. Debug As... Java2Script application context menu

This will execute our application that will show a button labeled "click me". If we click the button once a message is printed:

Figure 4-2. HotSpot debugging the application - first screenshot

Now the interesting part. Change the message printed in the method doClickAction(), for example:


protected void doClickAction() {
  System.out.println(counter+" say hello CRUEL WORLD!");
}

Save the changes and keep looking at your application at the J2S console. After a few seconds, you should see a red label "Application loaded":

Figure 4-3. HotSpot debugging the application - application change notification

That means the J2S HotSpot mechanish has detected changes in classes of current application, and the application has loaded the new changes. So, we can click the button again, and will see the new message printed:

Figure 4-4. HotSpot debugging the application - changes were applied

W just were able of modify the application and reflect those changes all without restarting our application, or in other words, we have just make a "hot change" in our application.

Note: Just like in java, only changes to instance variable and instance methods will be supported by HotSpot. Changes to static methods, or changes in class signatures (like adding/removing/renaming) methods are not supported by HotSpot.

How is this accomplished?

Java2Script also implements JavaScript's Hotspot technology. It is not complicate, because JavaScript is already a very robust and convenient language to do so. All Java2Script implementation is to clean those classes' declaration inside JavaScript class inheritance system simulator. And then reload the *.js using Java2Script's classloader. In the implementation, classloader does not change class or object's prototype when a reloaded class is redefined, it's possible to keep all classes relationships without breaking those already instantiated instances.

To trigger Java2Script class simulator to Hotspot swapping, there is a thread at the plugin side, trying to load an updating JavaScript classes list from Java2Script compiler in Eclipse. Its work is simple, just trying to load http://127.0.0.1:1725/<session>.js. And the server listening on default port 1725 is started by Java2Script compiler, called "Java2Script Inner Hotspot Server (J2SIHS)". When a compiling occurs, compiler will notify this server that a class is updated, and the server will add the information to the list. Once a JavaScript request arrives, it will send out the list according to the request session id. And when the JavaScript client thread gets the updated classes list, it will try to unload related classes and reload them. That is the rough procedure of Java2Script Hotspot technology.

By using Hotspot, I think it is much more convenient for me to develop JavaScript RIA in Java codes than before, especially in developing SWT applications.