Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

Spice up your Sling Models With Lombok | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

Spice up your Sling Models With Lombok by Initialyze

Abstract

Project Lombok is a java library that can be used to greatly simplify the process of extending Sling Models from WCM Core.

The Digital Foundation Blueprint provided by Adobe greatly streamlines AEM implementation methodology. WCM Core Components, an integral part of this blueprint provide key building blocks components for some of the most common use cases. To extend these core components, one of the recommended implementation pattern is the Sling Model Delegation Pattern.

In our experience, using this delegation pattern requires writing a lot of boiler plate code for e.g:

We have to implement every method defined by the interface and delegate it.
If new methods are introduced (for e.g when we upgrade to newer version of WCM Core Components), we need to implement those methods and delegate those also else, we may run into runtime or compile errors in some cases.
Lombok to the rescue!
Project Lombok is a java library that reduces boilerplate code. The library replaces boilerplate code with Lombok annotations and keeps your class simple.

How does it work?
Here’s a typical example of a sling model we extend using the Sling Delegation Pattern

@Model(adaptables = SlingHttpServletRequest.class,
adapters = {Title.class, ComponentExporter.class},
resourceType = {TitleImpl.RESOURCE_TYPE_V1})
public class TitleImpl implements Title {

private static final Logger LOG = LoggerFactory.getLogger(TitleImpl.class);

protected static final String RESOURCE_TYPE_V1 = "aem-initialyzer/components/general/title/v1/title";

@Self
private SlingHttpServletRequest request;

@Self
@Via(type = ResourceSuperType.class)
private com.adobe.cq.wcm.core.components.models.Title title;

@PostConstruct
private void initModel() {

}

@Override
public String getText() {
return title.getText();
}

@Override
public String getType() {
return title.getType();
}

@Override
public String getLinkURL() {
return title.getLinkURL();
}

@Override
public boolean isLinkDisabled() {
return title.isLinkDisabled();
}

@Override
public @Nullable String getId() {
return title.getId();
}
}
The same example above, with Lombok can be written as:

@Model(adaptables = SlingHttpServletRequest.class,
adapters = {Title.class, ComponentExporter.class},
resourceType = {TitleImpl.RESOURCE_TYPE_V1})
public class TitleImpl implements Title {

private static final Logger LOG = LoggerFactory.getLogger(TitleImpl.class);

protected static final String RESOURCE_TYPE_V1 = "aem-initialyzer/components/general/title/v1/title";


@Delegate(types = com.adobe.cq.wcm.core.components.models.Title.class)
@Self @Via(type = ResourceSuperType.class)
private com.adobe.cq.wcm.core.components.models.Title delegate;


}

Read Full Blog

Spice up your Sling Models With Lombok

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

0 Replies