In this example, we will create a very simple Lombok transformation that adds a helloWorld method to any class annotated as This is a trivial and useless transformation but serves as a simple starting point. The basic classes you'll need to write are: Here's an overview of the compilation process and how Project Lombok fits in: After the annotation processing stage, the compiler will generate byte code from the modified AST. The Lombok annotation handler is free to modify the AST by injecting new nodes such as methods, fields and expressions. Project Lombok uses a Java Annotation Processor and Java annotations to add boilerplate code to existing classes. When delegating to a handler, the Lombok annotation processor provides an object representing the Abstract Syntax Tree (AST) of the annotated node (e.g. Lombok annotation handlers declare the specific annotation that they handle. Handlers are discovered via a framework called SPI. The annotation processor acts as a dispatcher that delegates to Lombok annotation handlers (this is what we're going to create). Project Lombok runs as an annotation processor. I started by reading Nicolas Frankel's blog and a post in the Project Lombok discussion group but mostly it came down to groking the Project Lombok source code. At the moment, there are scarce few resources out there on this subject. What I present here is an approach that worked for me. In this blog, I will walk you through the process of extending Project Lombok to do a simple Hello World transformer. Project Lombok comes with a decent set of transformations, but you may also want to create your own custom Lombok tranformations. Now, you can simply use the class the same way as if you had written those getters, setters and constructors yourself.Project Lombok aims to reduce Java boiler-plate via annotations that perform class transformations at compile time. The arguments of the constructor have the same order as the order with which the instance variables appear in the class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |