J2JS
Home > Docs > Compiler Usage

1. Compiler Usage

The J2JS cross-compiler translates Java class files into JavaScript code and assembles reachable code into an assembly. To boost performance, J2JS is incremental by performing dependency checking based on existence/modification times.

It is important to understand that the assembler will ignore all unreachable code. The decision on reachability is based on a static code analysis and a set of entry points: Only those methods are reachable which can be traced back to a method declared as an entry point.

2. Configuration

The J2JS cross-compiler is configured with an xml string. This string can have the following attributes.
AttributeDescriptionRequired
baseDir Semicolon- or whitespace-separated list of path entries on which Java class files may be located. A path entry must not contain whitespaces. No; default is the current directory.
webPage The path to the web page. The assembler will generate a script include into the web page. The include will be placed between the magic pair of comments <!-- j2js Script --><!-- j2js Script -->. Yes
targetDir The directory to which the enhanced web page and the assemblies will be generated. Yes
classPath Semicolon- or whitespace-separated list of path entries on which Java class files may be located. A path entry must not contain whitespaces. Yes
cacheFile The assembler will maintain a cache of pre-crosscompiled classes. This is the path to the cache.

Hint: For troubleshooting, it may sometimes be useful to delete the cache or to use the noCache option.
Yes
onLoad The signature of a static method which is called after the document has loaded. The method must have no argument or an argument of type 'java.lang.String[]'. In the latter case, the values of the URL query string are passed to the method.

Note: The method is automatically added to the set of entry points, see below.
Yes
junkSize Assemblies are written in junks of the specified size. Must be of the form [0-9]+B or [0-9]+kB. Yes
compression Assemblies are kept as small as possibly. For example, no comments are generated. For maximal compression, set generateLineNumbers to false. Compressed assemblies are hard to read by humans. This fact can add an extra layer of obfuscation to your code.

Note: Only semantic compression is used. Currently, no file compression algorithm (for example zip) is employed.
No; default is false
generateLineNumbers If true, then line number information is generated for debugging purposes, for example in stack traces. No; default is true
entryPoints Semicolon- or whitespace-separated list of entry points, see the discussion on reachability above. Each item must either be the signature of a method, of a class, or of a package. A class signature equals specifying all method signatures of that class. A package signature equals specifying all classes in that package (but not in sub-packages). The static onLoad method (see above) is automatically added to the list of entry points. Also added to this list are all pre-reachable signatures.

Note: Normally you will only add those entrypoints which the static code analyser cannot detect, for example methods called through reflection (this is the case in the TestRunner target).
No; defaults to the empty list.
noCache If true, no dependency checking based on existence/modification times takes place, and required sources will always be recompile. No; default is false.
failonerror Indicates whether the build will continue even if there are compilation errors. No; default is true.
logFilePattern The path and pattern for the log file. Supported are all patterns as defined by class java.util.logging.FileHandler. No; default is to log to standard output.
logLevel One of SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST. No; default is INFO

3. Standalone Invokation

The cross-compiler can be invoked directly on the command line, for example

set applCLASSPATH=lib/j2js.jar;lib/bcel.jar
java -cp %applCLASSPATH% j2js.compiler.Compiler conf/helloworld1.xml


Extracted from /projects/j2js-Demos/compile.bat
The single argument must be the path to a file containing the xml-configuration.

4. Ant Invokation

1. Task Declaration

The j2js compiler can be executed as an Ant task. See j2j2-Demos/build.xml how to declare this task.

2. Build Example

    <target name="HelloWorld1">
        <j2js 
            baseDir="${basedir}"
            targetDir="build/site"
            classPath="build/classes; ${j2js-Runtime.dir}/build/classes"
            webPage="src/site/HelloWorld1.html"
            onLoad="j2js.demo.HelloWorld1#main(java.lang.String[])"
            junkSize="30kB"
            compression="true"
            generateLineNumbers="false"
            cacheFile="build/j2js.cache" />
    </target>

Extracted from /projects/j2js-Demos/build.xml
compiles all required classes and creates in the build directory an assembly containing all methods reachable through the specified entry point.

5. About Signatures

Signatures play an important role for the configuration of J2JS.

1. Method Signatures

A method signature is a string of the form

packageName.className#methodName(type1, ...)

Examples are

foo.bar#fooBar(int)
    foo.bar#fooBar()
    foo.bar#fooBar(java.lang.String, int)

2. Class Signatures

A class signature is the name of a class, for example

java.lang.String

3. Package Signatures

A package signature is the name of a package followed by the wildcard '.*', for example

java.lang.*

4. Pre-Reachable Signatures

There are three ways to specify reachable signatues: Using the onLoad property, using the entryPoints property, or specifying signatures in the file j2js.properties.

The signatures specified in j2js.properties will always be loaded by the static code analyser, and are listed in this chapter.

1. Unconditional Reachability

Unconditionally reachable signatures are typically used by the JavaScript JVM, and thus must be recognized by the static code analyser.

j2js.preTaintedSignatures = \
    java.lang.NullPointerException#<init>(java.lang.String); \
    java.lang.RuntimeException#<init>(java.lang.String); \
    java.lang.Throwable#printStackTrace(); \
    java.lang.Throwable#fillInStackTrace(); \
    java.lang.Throwable#toString(); \
    java.lang.String#init(); \
    java.lang.System#invoke(java.lang.String,java.lang.String); \
    java.lang.Class$AnnotationInvocationHandler#invoke(\
    		java.lang.Object,\
    		java.lang.reflect.Method,java.lang.Object[]); \
    java.lang.reflect.Proxy#invoke(\
    		java.lang.reflect.Proxy,\
    		java.lang.String,java.lang.Object[]); \
    j2js.client.Engine#getEngine();

Extracted from file:/D:/workspace/j2js-Compiler/src/resources/j2js.properties

2. Instancing Based Reachability

Reachability based on instancing means that a signature becomes automatically reachable if the code analyser discovers that an object of the associated class is instantiated.

j2js.taintIfInstantiated = \
    *#handleEvent(*); \
    *#compare(java.lang.Object,java.lang.Object); \
    *#invoke(*)

Extracted from file:/D:/workspace/j2js-Compiler/src/resources/j2js.properties

Last build on Sat Jan 03 11:06:48 CET 2009