Java code generation with JAnnocessor
In this article I will show you how to generate code with JAnnocessor framework created by Nikolche Mihajlovski. First time I met JAnnocessor on GeeCON 2012 conference during Nikolche’s speech: “Innovative and Pragmatic Java Source Code Generation” (slides). Afterwards I used it successfully in one of my projects. There are almost no resources about this framework so I hope my article will be useful for those who are interested in using it or just are looking for brand new toy for their project.
Every Java developer uses some sort of code generation tool on a daily basis. Setters, getters, trivial constructors, toString – all of these is just a boilerplate code. Usually we generate it with our favorite IDE’s help. I can’t really imagine coding it manually and because Java is a static language we will never be able to skip this process.
Those trivial examples of code generation provided by all modern IDEs are not the only situations when code generation is useful. There are many modern frameworks generate some code to help us to write more reliable code and do it faster. I think the most well known examples are QueryDSL and JPA2 Metamodel Generator that creates objects used to perform type-safe database queries.
There are also other situations – not so well supported by IDE – where we could use code generation. Usually it might be helpful in generating:
- DTOs and mappers from domain objects to DTOs
These are only examples. For some projects there might be something project specific where we can’t use any existing code generation tool and we have to write our own. How to do that? With Java APT – Annotation Processing Tool.
Few words about Java APT
Annotation Processing Tool was introduced in Java 1.5 and provides low level API for processing annotated classes. It is a base for most (maybe all?) existing code generation frameworks and because of really low level API I don’t recommend using plain Java APT if you just want to generate some classes. How creepy is code generation with APT look at example on apt-jelly (another code generation tool) page.
Instead of using plain Java APT – you can use one of existing frameworks. Recently I’ve attended to conference talk about interesting JAnnocessor that seems to do its job pretty well.
JAnnocessor is a quite new framework made by Nikolche Mihajlovski. How is it different from APT? Here is how author what author says:
JAnnocessor is built on top of Java APT, encapsulating the Java source code model in a rich and convenient high-level domain model that serves as a good target for expressive matching and transformation.
JAnnocessor comes with couple of built in processors: builder, dto, facade and mapper. If you need custom functionality you can easily write it by your own.
There is one big drawback – very very poor documentation. Actually there is almost no documentation at all. In wiki you won’t find too much and what is even worse – you won’t find Javadocs in framework classes neither. Although there is Getting Started Guide written by author I miss there some points so I will guide you through basics step by step.
We will use JAnnocessor only during compile phase so there is no need to add it into our application package – we set scope to provided
1 2 3 4 5 6
Next part is annotation processing plugin. Although Getting Started Guide recommends to use jannocessor-maven-plugin I was forced to do it “old fashioned way” with maven-processor-plugin because I missed some configuration options.
Into build/plugins section we add:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
- processors – tells maven-processor-plugin which class performs annotation processing – don’t change it
- configuration/logback.configurationFile – that’s optional. JAnnocessor uses inside logback for logging and if you do so as well in your project and you have logback.xml in classpath it is going to be used by JAnnocessor. I recommend to write separate logging configuration for JAnnocessor to avoid possible problems (for example if you use janino conditionals). jannocessor-logback.xml can be as simple as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
- options/templates.path – probably you will use custom JAnnocessor templates. That line can be removed if you won’t
- defaultOutputDirectory – that’s important – by default generated classes are added to src/main/java – which is in my opinon very bad idea. Remember that all generated classes are recreated every Maven build and all modifications made by hand are lost. That’s why generated classes should be places in
In this example I will use built-in generator to generate builder class for my simple POJO.
First of all we need new annotation to mark classes for whom we want to generate builder. It can be as simple as:
1 2 3 4
Our sample POJO:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Next important part is to tell JAnnocessor what has to be generated. In order to do that we need to create specific class in specific package:
org.jannocessor.config.Processors. I couldn’t find a way to make it configurable so that configuration could be in our project’s package.
In configuration we specify where created classes should be placed:
pl.maciejwalkowiak.jannocessor.domain.builder and where are base bean classes:
pl.maciejwalkowiak.jannocessor.domain. Last parameter means if we want to use debug mode – for now it isn’t important.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Let’s run it
In order to run it we just need to execute
mvn compile. In
target/generated-sources/pl/maciejwalkowiak/jannocessor/domain/builder/ we will find class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
Thanks to builder class we have fluent interface for creating
Creating our own generator
For creating custom generators JAnnocessor provides rich API and couple of examples. Unfortunately there is no guide or tutorial available. For this article I wanted to write quickly generator for FEST Assert 2.x but after some time digging in JAnnocessor source code I gave up. Instead I will just show concept.
In order to code custom generator you just need to create class that inherits from
1 2 3 4 5 6 7 8 9 10 11
PowerList<AbstractJavaClass> models represents collection of all classes annotated with custom annotation. Then you have access to all class fields, methods, implemented interfaces etc. The only thing that I missed is rich access to classes superclass. In order to get superclass’ fields I had to use Java Reflection API.
If you want to write custom generator I encourage you to look into examples like BuilderGenerator. It’s not much but it will be definitely helpful.
In this post I’ve shown how to setup and use JAnnocessor. Although I think its good and possibly very useful library, lack of documentation makes it impossible to use it in real serious project. I hope Nikolche will write good docs or build community around project that will do it. I also hope that project will move to github. It somehow become a standard so if author want’s to build community around it – I think it is the only right move. Nevertheless I asked him about that and at least for now he doesn’t have any plans to do so.
Do you have good or bad experience with JAnnocessor or maybe you use other code generation frameworks? Let me know in comments.