Java back-end

The Java back-end is PIL's most mature back-end.

In order to compile and execute Java code generated using this PIL back-end, the pil-java-runtime.jar that comes with the PIL distribution has to be on the Java CLASSPATH. This library contains reflection classes for built-in types and various utility classes to implement PIL constructs in Java.

Code style

Code generated by the Java back-end looks pretty similar to the original PIL code, except that getters and setters are added and called for properties. Additionally, reflection classes are generated. For each function and global variable a class is generated with a static method/field. A doSomething function will result in a DoSomething class with a doSomething static method. For global variables a similar convention is used.

Classes in Java's default package cannot be imported from other packages, therefore PIL puts PIL classes in the default namespace (i.e. no somens:: prefix on classes and functions) in a application Java package.

If Java keywords are used as identifiers (e.g. variable names, method names, class names), they are postfixed with an "_" in the generated Java code.

Since Java does not allow operators in method names, PIL replaces operators with $something sequences. For example, a || method in PIL will result in a $pipe$pipe method in Java. The translation is as follows:

  • "+" -> "$plus"
  • "-" -> "$min"
  • "*" -> "$times"
  • "/" -> "$div"
  • "%" -> "$mod"
  • "|" -> "$pipe"
  • "&" -> "$amp"
  • "=" -> "$eq"
  • "!" -> "$not"
  • "<" -> "$lt"
  • ">" -> "$gt"

Built-in type mapping

  • List: ArrayList
  • Set: LinkedHashSet
  • Map: HashMap
  • Array: native arrays
  • Exception: RuntimeException

How to use

To generate Java code from a .pil file:

    $ pilc -i file.pil -d out --java

All the Java files are generated to the "out" directory, these files can then be compiled as usual:

    javac -cp out:/path/to/pil-java-runtime.jar -d classes application/*.java

If you compile and run a .pil file with pil-java, the runtime jar file is put on your classpath automatically.