Expand my Community achievements bar.

Using Project Lombok in AEM Sling Models | Write Less Code for Sling Models

Avatar

Community Advisor

9/20/22

Company Name: Rightpoint

Company URL: https://www.linkedin.com/company/rightpointindia/

Your Name: Nikhil Kumar

Your Title: AEM Lead



Most of the time in our projects we end up creating plain Sling models for dialogs value or for some performing some operations.
In some cases we just need a plain Sling Model where we just want to Inject some variables or create some variable and generate getters and setters for the same , also if you have around more than 20+ variables then you have to first declare those variables and then generate corresponding Getters and Setters which increases the size of your Java class by adding around 60+ extra line of code (if 20 variables).

In such scenarios we can make use of Project Lambok Library to get the Getters and Setters generated at runtime.

Add following maven dependency:

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.4</version>
    <scope>provided</scope>
</dependency>


After adding this dependency you can start using this library in your Java classes, Below is the example that I tested out in my AEM Sling Model:

 

2793890D-DDD6-46F1-9C68-1AE2FAA05367.jpeg


Once you add the @Getter annotation it automatically declares the getter method to the variable, you can verify in the outline in Eclipse.

Getter Corresponding to the variable:

F09964C6-EC92-4716-A45A-475C0ADCEE98.jpeg

 

 


Same way we can use @Setter annotation as well to declare the setters at runtime. If you looking for using both, you can use @Data annotation at the class level and it automatically imports all the Getter and Setters at runtime.


Using @Data Annotation:

6E74ED57-1C59-4E17-8500-8711B8B0BF3A.jpeg

 

 

52BC1297-D19B-4E70-A949-4811056F63E8.jpeg

 

 



NOTE : Sometimes after adding the dependency you won't see the reference of Getters or Setters in Outline then in that case your methods won't be generated at runtime. So please make sure it's the part of Outline.
If it's not showing up the go to ..m2\repository\org\projectlombok\lombok\1.18.4repository in you local and run the java -jar lombok-1.18.4.jar to run the Jar and you will see following window opens up, Just provide the path of the IDE that you are using and click on Install/Update
. And restart the IDE to see the affect.

 

CA813B7B-718B-4B7B-88A8-127F0AC5A6FB.jpeg


Project Lombok Library comes with more features which can help you to spice up your Java code and reduces the most of the manual effort.

Came to know from one of friend regarding as he was implementing the same in Spring application. So I thought why not it a try to use it as part current AEM Archetype project.
This is tested on AEMaaCs and AEM 6.5.5.

@Jagpreet_Singh_  @kautuk_sahni  @VeenaVikraman 

Aspire Experience Manager Journey Optimizer