Tutorial of J2S in Eclipse (8): How to Extend Java2Script Compiler

Soheil Hassas Yeganeh
Oct 29, 2006

One of the newest features that the java2script compiler will have in the M4 release is extension mechanism for java2script compiler. In this article the new extension mechanism of j2s compiler is demonstrated.

Prerequisite

If you are not familiar with PDE and JDT, please read the PDE guide and JDT programmers guide.

Please read previous tutorials: 

Setup Environment

First create the environment described in "Java2Script Getting Started" and complete the tutorial and be sure that the javascript sources are created.

Create an eclipse plugin project, without any preconfigured templates and samples. 

Import the project "net.sf.j2s.core" into the workspace. It would be better if you checkout the head version from the j2s SVN site

Extending J2S Compiler

Edit your plugin.xml with PDE's plugin editor. Goto the "Dependencies" tab and add "org.eclipse.jdt.core" and "net.sf.j2s.core" as the dependencies. Then goto the "Extensions" tab and add an extension for the "net.sf.j2s.core.extendedASTScriptVisitor" extension point.  Right click on the extension and add a new "extendedASTScriptVisitor" instance and select an ID and an implementation class for it.

The implementation class for the extension point must be an implementation of IExtenedVisitor interface. Create an extended visitor class for your plugin :

public class ExtendedASTScriptVisitor implements IExtendedVisitor {

public ASTScriptVisitor getScriptVisitor() {
...
}

public DependencyASTVisitor getDependencyVisitor() {
...
}

}


The Java2Script compiler uses two visitors for converting java source codes to javascript. An ASTScriptVisitor is responsible for converting basic java elements (e.g. ClassDefinition,  MethodInvocation, ... ). An DependencyASTVisitor is responsible for creating a class dependency trees, in other words an import list for each java file. J2S core provides some default implementation for these visitors (SWTScriptVisitor, ASTScriptVisitor, SWTDependencyASTVisitor, DependencyASTVisitor) that each of them are used according to the situation. ASTScriptVisitor is the base class for converting java source codes. SWTScriptVisitor is a subclass of ASTScriptVisitor that is responsible to convert SWT specific java constructs. And also DependencyASTVisitor is the basic import tree builder and SWTDependencyASTVisitor is a direct subclass of DependencyASTVisitor that is responsible of building SWT import tree.  Any IExtendedVisitor should return an instance of ASTScriptVisitor and DependencyASTVisitor that are used for converting java to javascript. 

After installing the extended compiler plugin, we have to tell the j2s to use our compiler instead of the default. For this purpose, you must place a property named "j2s.compiler.visitor" in the .j2s file and set the value to the id of your extension.

The Sample -- Adding comments to generated javascript

Let's suppose that we want to add some comments to the generated javascript when a method is invoked. For example write "a hello world!" there. 

We need a class that extends ASTScriptVisitors needs to be implemented. And for the basic dependency visitor we can used the default SWTDependencyVisitor or DependencyASTVistor. In j2s visitors have access to a shared sequential buffer for generating javascript file contents. Suppose we have set id of this extension to "net.sf.j2s.tutorial.extendedCompiler.extendedASTScriptVisitor".

For the ASTScriptVisitor we need we can extend one of the default visitors (SWTScriptVisitor, ASTScriptVisitor)

public class TutorialExtendedVisitor extends SWTScriptVisitor {

public boolean visit(MethodInvocation node) {
buffer.append("/* This comment is added with extended compiler: Hello World! */");
return super.visit(node);
}

}

public class ExtendedASTScriptVisitor implements IExtendedVisitor {

public ASTScriptVisitor getScriptVisitor() {
return new TutorialExtendedVisitor();
}

public DependencyASTVisitor getDependencyVisitor() {
return new SWTDependencyASTVisitor();
}

}

Now we have to export our plugin and install it.  Do not forget to change the .j2s file of your project to the id of the extended visitor extension. You should add a line to your .j2s file that :

	j2s.compiler.visitor=net.sf.j2s.tutorial.extendedCompiler.extendedASTScriptVisitor

Now clean the project, and then look at the new generated js files.

Resources

Download the extendedcompiler project files (zip) (9 k)

Blog discussion

Have problems while reading through these articles? Or find some errors between the words? Or have some ideas on the Java2Script technology? Please make comments or discussions here in the blog.

About the author

Soheil Hassas Yeganeh: Developer of Java2Script.