Interface Introduction

This section describes a few of the interfaces that you will definitely need when using Jenesis.

The Jenesis API is "strict" in the sense that it views the source code file as fine-grained composition of fundamental language elements. As a consequence, programming with the API can be cumbersome at times. Things that take just a few seconds to express in Java at the keyboard in an editor can be surprisingly complex to implement using objects (complex though not difficult). Though this is generally not too big a problem, the user can disable the object-oriented nature of syntax composition using the Freeform interface. This interface is a simple way to write Java code directly.

VirtualMachine

The VirtualMachine is the mother of all objects (in Jenesis) since all object construction is derived directly or indirectly from it. Therefore, the first step in any program that uses Jenesis will be to get an instance of a VirtualMachine.

To do this, you can call the static method VirtualMachine.getVirtualMachine() to grab a reference to the default implementation.

The VirtualMachine interface (actually an abstract class) contains many important factory methods including all Type, Literal, and Expression factory methods. You will refer often to the VirtualMachine to create such objects. For example, if you want to create a String literal, you would call the VirtualMachine.newString(String) method. Similary, if you need a variable named i, you would call the VirtualMachine.newVar("i"). The same goes for types and expressions. To get an int[][] type, you would call VirtualMachine.newArray(Type.INT, 2).

CompilationUnit

A single CompilationUnit object and a single .java file have a one-to-one correspondence. It contains an optional package declaration, optional import declarations, and type declarations / definitions. It is the direct container for the top-level class (modeled by the PackageClass interface).

You can create a new CompilationUnit using the VirtualMachine.newCompilationUnit(String) method. The argument to the factory method takes a String whose value should be the path where the .java file should be written. For example, if the value you choose here is "/tmp", the package name of the CompilationUnit is "com.example.project", and the name of the top-level-class (or interface) is "HelloWorld", the file that will be written is "/tmp/com/example/project/HelloWorld.java".

Once you have completed building the classes and/or interfaces within the CompilationUnit, the source file can be written using the CompilationUnit.encode() method, or encoded and compiled using the CompilationUnit.compile() method.