Open Source & Free  

TIP: Using Lombok and Other tools

TIP: Using Lombok and Other tools

Header Image

A few weeks back maaartinus asked on stack overflow whether we can get lombok working with Codename One. After a short back and forth it seems that this was as easy as I’d hoped and he was able to get it working without a change.

Personally, I like our approach of property objects more but that’s obviously a matter of taste. Lombok works by processing the bytecode after compilation to convert annotations to terse code. E.g. this Java code using Lombok:

@Getter @Setter @NonNull
private List<Person> members;

Would be similar to this regular Java code:

private List<Person> members;

public Family(final List<Person> members) {
    if (members == null) throw new java.lang.NullPointerException("members");
    this.members = members;
}

public List<Person> getMembers() {
    return members;
}

public void setMembers(final List<Person> members) {
    if (members == null) throw new java.lang.NullPointerException("members");
    this.members = members;
}
I took the code sample above from here

That’s pretty nice and it has some other features that hide some of the verbosity of Java.

As mentioned in the answer adding Lombok to a Codename One application is pretty easy:

  • Just add the lombok.jar to the build path which you can find in the project settings for the native IDE

  • In the build.xml replace compiler="modern" with compiler="extJavac"

  • Add the path to lombok.jar to the classpath in the build.xml (this might not be necessary in NetBeans)

Doing this for Other Things

Lombok is just one of a huge set of bytecode manipulation tools that work statically. I’m sure there are many other tools like that in the wild that would “just work” and we never got around to test them.

If you have a pet tool in your toolbox check it out and see if it works with Codename One. You might be surprised.

2 Comments

  • Martin Grajcar says:

    You’re wrong concerning how Lombok works. It does AST manipulation rather than bytecode manipulation. That’s a black magic necessary as without the stuff generated by Lombok, the code won’t usually compile (and you’d get no bytecode to play with). But this is just a detail, the important thing is that it’s job is done in the compiler.

  • Shai Almog says:

    I should have phrased it differently. It manipulates the bytecode statically which is the point that matters to us. I don’t really care about the underlying implementation and had no interest in it.

Leave a Reply