Set them free: Using Lombok in z2

As you have undoubtedly learned by now, z2 compiles on its own, on-demand. It uses the Eclipse Compiler for Java (ECJ) for that. It used to use the build it Javac from the JDK more on that in some other post.

Lombok is a rather cool (although maybe slightly disturbing) approach to add getters and setters and more to pretty plain Java objects w/o typing them. A minor feature in a way – and maybe not really important – but definitely a noise remover. And a cool example to show off how well z2 holds things together even when enhancing the compilation step.

Using Lombok, you can use POJOs w/o the need to declare setters and getters before you actually have a good reason to do so:

package test;

import lombok.Getter;
import lombok.Setter;

public class MyBean {

    @Getter
    @Setter
    private String text;
}

Lombok, under the hood makes sure that methods setText, getText are available.

Compiling Java source code to Java byte code is  just one of the things that need to be happening before code can be executed – in some cases. To give extensions a chance to be able to enhance what happens during compilation, z2 offers a compiler API.

Both use-cases apply:

Modifying source code before compilation: In some cases it is most handy to prepare source code before compilation already – like a preprocessor. This is what we use for Lombok below. If you know Lombok, that may come as a surprise. Read on.

Modifying byte code after compilation: Other tools understand byte code already and better than source code. So you can do that as well. This method was used in the z2@Spring distribution supporting weaving of the AspectJ aspect of the spring framework. No idea what that means? Read on here: Spring features in Z2, Spring AspectJ.

But back to Lombok: Here is a short descriptions how it’s done. More links below.

The compiler API is very simple. There is only one interface: ICompiler, that has one method:

boolean compile(String compName, File[] src, File dest, ClassLoader cl);

Whenever called, the job of a compiler is to transform source files in the folders passed in src or compilation results of other compiler invocations in the folder passed as dest as desired.

The set of compilers to be used is a Java component specific configuration. I.e. on a Java component descriptor (the z.properties file) you specify the set of compilers to apply as well as their invocation order. E.g. like this:

java.compile.order=lombok,java

The default setting is “java.compile.order=java”.

Now to the construction of the actual extension:

A compiler extensions is declared as a z2 component (of course). In its component descriptor it declares its type, its unique id as a compiler, as well as its implementation class:

com.zfabrik.component.type=com.zfabrik.compiler
compiler.id=lombok
component.className=com.zfabrik.impl.LombokCompiler

The implementation class uses the Delombok source converter tool as follows:

  1. Create a temp folder at <component root>/gen/lombok
  2. Compute a class path based on the class path class loader (see above). Delombok requires a class path.
  3. Process all source files in the src folders using Delombok
  4. Copy the processed source files back to where they were found originally

View the source code at: LombokCompiler.java (use z2_base/z2_base)

If you have a running z2 setup (see Check it out), you can play around with this by simply checking out the projects at

http://www.z2-environment.net/svn/z2_base/trunk/offline/lombok

(again: use z2_base/z2_base when asked)

into your eclipse workspace and sync’ing your runtime. The simple web app at /lombok demos a simple Lombok processed Bean in a JSP.

There is one pre-requisite however: You need to have the tools.jar from your JDK on your classpath. The simplest way to do so is to copy tools.jar from JAVA_HOME/lib to JAVA_HOME/jre/lib/ext.

Why is this cool?

  1. We reduced the application of lombok to your Java code to a mere one-liner
  2. From the outside, e.g. when using the Eclipsoid plugin to fullfill project dependencies, it’s as if the Java Beans were indeed normal Java Beans in the first Place.
  3. On the downside you need a JDK rather just a JRE and need to provide the tools.jar (that contains the Javac that Delombok requires).

Links:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s