ad
ad

Code Remix Weekly | OpenRewrite and Lombok: Peace at last

Science & Technology


Introduction

Welcome everyone to another edition of Code Remix Weekly! I'm your host, Sam Snider. Today, we'll dive into an exciting new development in OpenRewrite, namely its support for the Lombok annotation processor—a feature that many have requested for a long time.

Upcoming Events

Before we jump into today's topic, let's highlight some upcoming events. Although Tim is currently busy giving a presentation, he will soon be speaking at JAL next week. If you are in the area, don’t miss out on the opportunity to attend his talk focused on generative AI and code. There will also be an interactive workshop, so if you're interested in co-developing a recipe or tackling a problem, that’s a fantastic chance to engage.

Additionally, Jonathan will be speaking at the Dallas Java User Group on Wednesday, November 13th. If you're in the Dallas area, I encourage you to attend and say hello to him. The event is free, making it an excellent opportunity for learning and networking.

Looking further ahead, we have the upcoming Code Remix Summit scheduled for May 12-14, 2025, in Miami. Use the VIP code MADVIP for a significant discount. I’ll be speaking there along with many prominent contributors from the open-source community. It’s a wonderful chance to connect and explore what we’re doing to advance automated software refactoring.

Main Topic: Lombok Support in OpenRewrite

Now, let's delve into the newly integrated support for the Lombok annotation processor in OpenRewrite. This change addresses an issue that dates back to 2021, and, in fact, has been a long-standing concern for numerous developers, including those of us at OpenRewrite who utilize Lombok extensively in our codebase.

Understanding Lombok

Lombok is a powerful annotation processor that uses annotations to automate code generation, eliminating boilerplate code in Java development. It can generate methods such as getters, setters, and other common functionalities, which significantly reduces the verbosity of Java code.

Prior to this update, OpenRewrite could parse code utilizing Lombok annotations but lacked the essential type attribution for Lombok-generated methods. Because the Java parser relied on the Java compiler to parse and type-attribute code, it didn't support Lombok out of the box. Consequently, any code utilizing Lombok annotations wouldn't produce the correct types in the resulting Abstract Syntax Tree (AST).

Key Enhancements

With the new Lombok support, we can now seamlessly parse coded that includes Lombok annotations. The integration allows the type attribution for Lombok-generated methods, providing two main benefits:

  1. Correctness: Type attribution for all symbols improves the accuracy of recipes executed on codebases that use Lombok.

  2. Performance: Parsing speed improves significantly when the Java compiler has the necessary type information at hand, reducing processing times for larger projects.

Currently, the initial implementation is only available for Java 17. Future versions supporting Java 8, 11, and 21 are in the works.

This integration required a few critical steps:

  • Configuring the Lombok Annotation Processor: We look for Lombok on the classpath during parsing and configure the Java compiler to use the Lombok processor.
  • Filtering Out Generated Elements: After Lombok generates methods and other elements, we need to ensure these do not appear in the AST printed back to the original source code. Using the @lombok.Generated annotation helps identify these elements to filter them out.

Additionally, we had to override some of the Lombok features to ensure that the transformations made by Lombok do not disrupt the integrity of the source code representation.

Future Recipes

This groundbreaking support opens the door for various Lombok-related recipes, allowing for tasks such as converting Lombok annotations to regular Java method definitions or removing Lombok annotations altogether. Should you have an interest in writing such recipes or have ideas in mind, I encourage you to contribute or share your thoughts.

Conclusion

As we wrap up this incredible milestone in OpenRewrite's evolution, I invite everyone to try the new Lombok support, especially if you're working with Java 17. If you're using older versions, stay tuned! In the coming weeks, we'll also support Java 8, 11, and 21.

This integration not only enhances the performance and correctness of recipes using Lombok but also facilitates ongoing community engagement and contributions. Please feel free to connect with me in the Open Source Community Slack, provide feedback, and share your experiences.

Thank you for joining me this week; I hope to see you at future events and in our community discussions!


Keywords

Lombok, OpenRewrite, annotation processor, type attribution, Java parser, boilerplate code, AST, community engagement, recipes.

FAQ

Q1: What is Lombok?
A1: Lombok is an annotation processor that helps reduce boilerplate code in Java by automatically generating methods like getters and setters.

Q2: What changes are being made to OpenRewrite regarding Lombok?
A2: OpenRewrite has integrated support for the Lombok annotation processor, allowing for accurate type attribution and improved performance when parsing Java code.

Q3: Which versions of Java currently support Lombok in OpenRewrite?
A3: Currently, the Lombok support is implemented for Java 17, with plans to extend support to Java 8, 11, and 21 shortly.

Q4: Can I contribute Lombok-related recipes to OpenRewrite?
A4: Absolutely! Contributions are encouraged, especially for recipes that can automate the process of converting Lombok annotations to traditional code structures.

Q5: Where can I connect with the OpenRewrite community?
A5: You can connect with our community through the Open Source Community Slack, where you can share feedback, ideas, and engage in discussions.