Lombok
Contents |
Lombok
This page describes the potential support for Project Lombok in NetBeans. The support for Lombok is tracked under umbrella issue 173063.
New: Lombok now supports NetBeans! See this e-mail thread and this screencast by Alexis Moussine-Pouchkine.
Here are some scenarios how Lombok could be used in a practical project with NetBeans.
Simple scenario
- Create a Java application
- Create a new JavaBean (called
MyBean) in this application, using the template available in NetBeans - Add the following code to the
main()method of theMainclass:MyBean bean = new MyBean();bean.setSampleProperty("x");System.out.println(bean.getSampleProperty()); - Run the project - the application prints "x" to the console
This is good, but when looking at the code ofMyBean, we see that it contains boilerplate code (such as getters and setters). So how do we simplify the code using Lombok?
- Add Lombok library to the project
- Disable "Compile on Save" for this project (Compile on Save does not work with Lombok)
- Delete the getter and setter from the
MyBeanclass - Add the
@Dataannotation toMyBean. NetBeans will offer to importlombok.Data- do that. - Run the application again - things work again, with less code!
Advanced scenario
- Create a Java Web application
- Create Java Persistence entity classes using the Entity Classes from Database wizard
- Create JSF pages for these entity classes using the JSF Pages from Entity Classes wizard
- Run the project
We get a simple working application. But when looking at the generated entity classes, we see that these contain boilerplate code (such as getters and setters). So how do we simplify the code using Lombok?
- Add Lombok library to the project
- Disable "Deploy on Save" for this project (Deploy on Save does not work with Lombok)
- Delete the getters and setters from the entity classes
- Add the
@Dataannotation to the entity classes. NetBeans will offer to importlombok.Data- do that. - Redeploy the application - things work again, with less code!
IDE support
The above can be done in NetBeans now, but there are many things that do not work correctly, or that could be improved. Let's track them under umbrella issue 173063.
