# Spring Lunch - Lombok 🌶 --- ## 🤔Why Lombok? - Java was created in 1995 - Effective patterns emerged over time - Effective Java by Joshua Bloch - Lots of boilerplate and generated code - How to reduce the boilerplate? - Run-time (with e.g. reflection) - Code generation - Traditionally Java changed very slowly (this is changing) --- ## What's with the name? > "The 14th century Majapahit Javanese Nagarakretagama manuscript mentions lombok mirah island which is identified as Lombok island, which is also the area that produced a spice called red lombok. **In today's modern Javanese, the term lombok refers to chili pepper**..." --- ## 🏞 Real world examples [<i class="fa fa-github"></i> Repo - Lombok Examples](https://github.com/ddubson/lombok-examples) - Eliminating boilerplate with `@Data` - IntelliJ generate menu - getters/setters - constructors - `toString` - Java 101: `equals` and `hashcode` - Fluent creation of objects with `@Builder` - Immutable data class with `@Value` - Require fields to explicitly be not null with `@NonNull` - Logging with `@Slf4j` - Shorter declarations with `val` - Safe copying of immutable objects using `@With` --- ## ☕️Usage in a Spring/Java project - Can include this as part of Spring Initilizr - Gradle `annotationProcessor` directive and/or Intellij Lombok plugin - Delombok --- ## ⚠️ Watch out for - Using in conjunction with Java JPA (Spring Data JPA, Hibernate Entities) could lead to unexpected behavior that could be tricky to isolate during debugging. - Using in conjunction with Jackson JSON library can lead to unexpected behavior - Introducing a lot of non-native constructs to eliminate the noise of native constructs (lots of imports, changes in Lombok API (on the rarer side)) - Static import of Lombok `builder` in IntelliJ doesn't work: https://github.com/rzwitserloot/lombok/issues/2044 --- ## Customizing annotation on Lombok-generated methods ```java @AllArgsConstructor(onConstructor = @__(@Autowired)) @Getter(onMethod = @__(@Autowired)) public class LombokManagedClass { // ... } ``` --- ## Immutable Objects with Lombok and Jackson ```java import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @Data @AllArgsConstructor @Builder(builderClassName = "Builder") @JsonDeserialize(builder = ImmutableThing.Builder.class) public class ImmutableThing { private final String cantChangeMe; @JsonPOJOBuilder(withPrefix = "") public static final class Builder { } } ``` --- ## 📚References - [Lombok](https://projectlombok.org/) - [Effective Java (Amazon)](https://www.amazon.com/Effective-Java-Joshua-Bloch/dp/0134685997) - [Effective Java (O'Reilly Books)](https://learning.oreilly.com/library/view/effective-java-3rd/9780134686097/) - [Sambal and Lombok](https://en.wikipedia.org/wiki/Sambal) --- ## 🚘Parking Lot (for future sessions) - Comparison with Kotlin? - [Delombok](https://projectlombok.org/features/delombok) --- Help us improve, give some feedback! https://forms.gle/9wSh34KpbGaWJBgE7
{"metaMigratedAt":"2023-06-15T05:10:11.804Z","metaMigratedFrom":"Content","title":"Spring Lunch - Lombok 🌶","breaks":true,"contributors":"[{\"id\":\"6221bbf6-10a0-4687-9087-8c2420c49c42\",\"add\":2935,\"del\":1260},{\"id\":\"fde7702e-69a3-479d-ab92-f66e20acf544\",\"add\":3016,\"del\":2198},{\"id\":\"0edfcb1a-0af9-465b-a233-debdcd3f8bd4\",\"add\":729,\"del\":47}]"}
    715 views