Let's go straight into an example. We'll create a small "Hello, World!" program. The end result shall look as follows:
package com.foo.myapp; import java.lang.String; import java.lang.System; public class Main { public static void main(String pArgs) { System.out.println("Hello, world!"); } }
Okay, no surprises. Now, here 's the generator
public void generateHelloWorldJava(File pTargetDir) { final JSGFactory factory = JSGFactory.create(); final Source s = factory.newSource("com.foo.myapp.Main").makePublic(); final Method mainMethod = s.newMethod("main").makePublic().makeStatic(); mainMethod.parameter(JSGQName.STRING_ARRAY, "pArgs"); mainMethod.body().line(System.class, ".out.println(", q("Hello, world!"), ");"); // The class has been created, now have it written to disk: factory.writeTo(new File("target/generated-sources/myapp"); }
Having seen the example, let's break it down into the relevant parts:
The first line is about creating a factory.
final JSGFactory factory = JSGFactory.create();
The factory is an instance of JSGFactory. It is an object, that you will rarely notice, unless you are generating a real lot of Java source files. The purpose of the factory is basically to collect all the generated sources, so that you may persist them at a later point in one go.
The factory is being used to create Source objects. These objects are basically representations of a Java source file, which is being generated. For every Java source file, that you want to generate, you must create a Source object. Creating a Source object works like this:
final Source s = factory.newSource("com.foo.myapp.Main").makePublic();
The newSource method accepts a single argument, which is the fully qualified class name of the generated Java class. (Fully qualified = Package name, plus class name. In our example, we'll generate a class named Main in the package com.foo.myapp. The Source object is a builder: In other words, it is configured by invoking other methods on it, which return the Source object. In the example, we want the generated class to be public. So, we invoke the method makePublic on it.
The next line uses the source builder to create a new method, named "main". By creating the method, we do in fact create another builder, so the creation looks like this:
final Method mainMethod = s.newMethod("main").makePublic().makeStatic();
The methods makePublic, and makeStatic are being invoked on the method builder, declaring that the method should be "public", and "static".
The method builder is also used to add parameters to the method:
mainMethod.parameter(JSGQName.STRING_ARRAY, "pArgs");
Note the use of the builtin constant JSGQName.STRING_ARRAY to specify the parameter types. Such constants exist for a lot of commonly used types, including java.lang.Integer, java.lang.Long, java.lang.Boolean, java.lang.Object, java.lang.String, java.util.List, java.util.Map, and the primitive types. For other types, see the dedicated document.