Skip to content

Latest commit

 

History

History
40 lines (24 loc) · 3.04 KB

lombok.md

File metadata and controls

40 lines (24 loc) · 3.04 KB

Project Lombok DHIS2 guide

As of DHIS2 2.34 we now allow usage of Project Lombok in our code, it does however come with some restrictions.

IDE setup

To have Lombok function properly in IDEs like Intellij and Eclipse you will have to enable APT (annotation processing tool) and also install a plugin which helps with auto-complete etc, a nice guide for this can be found here.

Maven already supports APT (through the lombok depdendency), so for CI servers etc no additional setup is needed.

Occasionally IntelliJ tends to get out of sync with lombok and shows errors related to generated code even if setup is correct and compilation works. In such case use File => Invalidate Caches / Restart....

Allowed annotations

Current we only allow Lombok to be used for simple bean classes (DTOs, parameter classes etc), and we only allow the following annotations.

  1. @Data (and by implication also @ToString and @EqualsAndHashCode )
  2. @Value
  3. @Builder, @Singular
  4. @Getter, @Setter
  5. @NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor
  6. @Accessors(chain=true)
  7. @With

Other annotations

Ask in the backend team meeting if you'd like to use any annotation not on the allowed annotations list.

We discourage using @SneakyThrows even though you might find usages of it in our code.

Fine Tuning

Be careful when using @ToString or @EqualsAndHashCode (and by implication also @Data) as they by default include all fields. This is mostly a problem for domain objects where circular or very deep graphs would be referenced and effectivly considered in toString() and hashCode() generated. For that reason such classes often had manually written implementations where certain fields intentionally were left out. The same can be done using @ToString or @EqualsAndHashCode by annotating the fields with @ToString.Exclude or @EqualsAndHashCode.Exclude.

Examples

  • Simple DAO + builder class used for Artemis audit persistence, please be aware that this pattern should only be used for simple JDBC objects (and not for JPA) Audit.java.

  • DTO class + builder used for Artemis audit message passing, this example also shows how you can integrate @JsonPOJOBuilder (from Jackson) with Lombok builders Audit.java.