mcpField
L
lombok-patterns

lombok-patterns

github.com/emvnuelDevelopment287 installsUpdated 12w ago
Repository

About lombok-patterns

lombok-patterns is a Project Lombok guide for Java developers, covering annotations, integration patterns, and practices. It highlights Lombok uses that may be replaced by modern language features in Java 21+.

Key features

  1. 01Groups common Lombok annotations by purpose
  2. 02Covers JPA and Spring integration patterns
  3. 03Explains when Java Records fit
  4. 04Lists common pitfalls and remedies

Use cases

  1. 01Choose annotations for JPA entities
  2. 02Use constructor injection in Spring services
  3. 03Assess whether a simple DTO fits a Record
  4. 04Review Lombok usage and troubleshoot pitfalls

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/emvnuel/skills --skill lombok-patterns

After installing, type/lombok-patternsin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

cookbook/
builder-annotation.md
configuration-properties.md
constructor-annotations.md
data-annotation.md
dependency-injection.md
entity-patterns.md
equals-hashcode-jpa.md
getter-setter.md
logging-annotations.md
non-null.md
sneaky-throws.md
synchronized-annotation.md
value-annotation.md
with-annotation.md

Details

From the project README

Lombok Patterns

This skill provides guidance on using Project Lombok to reduce boilerplate code in Java applications, covering annotations, best practices, and integration patterns.

Note: This guide targets Java 21+ projects. Some Lombok features are now obsolete due to modern Java features (Records, var, try-with-resources). See Java 21+ Considerations below.

Overview

Lombok is a Java library that automatically generates common code like getters, setters, constructors, builders, and more during compilation. It helps create cleaner, more maintainable code.

Java 21+ Considerations

Some Lombok features are now redundant with modern Java:

Lombok FeatureJava AlternativeRecommendation
val/varvar (Java 10+)Use Java var
@Valuerecord (Java 16+)Prefer records for simple value objects
@Datarecord (Java 16+)Prefer records for simple DTOs
@Cleanuptry-with-resources (Java 7+)Use try-with-resources
@Withrecord wither methodsRecords can define wither methods
When Lombok Still Adds Value
  • JPA Entities: Records don't work with JPA (need no-arg constructor, mutable state)
  • @Builder: Records don't have built-in builder support
  • @Slf4j: No Java equivalent for logger generation
  • @RequiredArgsConstructor: Useful for Spring dependency injection
  • Inheritance: Records can't extend classes

Annotation Categories

Core Annotations (Still Valuable)
AnnotationPurposeUse Case
@Getter/@SetterAccessor methodsJPA entities, mutable classes
@NoArgsConstructorNo-argument constructorJPA entities, serialization
@AllArgsConstructorAll-fields constructorDependency injection
@RequiredArgsConstructorConstructor for final/@NonNull fieldsSpring services
@BuilderBuilder pattern implementationComplex object construction
Consider Java Records Instead
LombokUse Java Record When
@ValueSimple immutable data carriers
@DataSimple DTOs without inheritance
// ✅ Prefer Java Record for simple value objects
public record Money(BigDecimal amount, String currency) {}

// ✅ Prefer Java Record for simple DTOs
public record CustomerDTO(String name, String email) {}

// ✅ Use Lombok for JPA entities (records don't work with JPA)
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Customer {
    @Id private Long id;
    private String name;
}
Logging Annotations
AnnotationLogger Type
@Slf4jSLF4J (recommended)
@Log4j2Log4j 2
@Logjava.util.logging
@CommonsLogApache Commons Logging
Field & Method Annotations
AnnotationPurpose
@NonNullNull checks on parameters/fields
@WithImmutable setters (creates new instance)
@SneakyThrowsThrow checked exceptions without declaration
@SynchronizedThread-safe method synchronization
@ToStringCustomizable toString() generation
@EqualsAndHashCodeCustomizable equals/hashCode

Quick Reference

JPA Entities (Lombok Recommended)
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Product {
    @Id
    @EqualsAndHashCode.Include
    private Long id;

    private String name;
    private BigDecimal price;
}
Spring Services
@Service
@RequiredArgsConstructor
@Slf4j
public class OrderService {
    private final OrderRepository orderRepository;
    private final PaymentService paymentService;

    public Order process(Order order) {
        log.info("Processing order: {}", order.getId());
        return orderRepository.save(order);
    }
}
Builder Pattern (Lombok Adds Value)
// Records don't have builder support - use Lombok
@Builder
public record Email(
    String to,
    String subject,
    String body,
    @Singular List<String> attachments
) {}

// Usage
Email email = Email.builder()
    .to("[email protected]")
    .subject("Hello")
    .body("Content")
    .attachment("file1.pdf")
    .build();

Best Practices

✅ Do
  • Use Java Records for simple value objects and DTOs
  • Use @RequiredArgsConstructor with final fields for dependency injection
  • Use @Slf4j for logging instead of manual logger creation
  • Use @Builder for classes with many optional fields
  • Be explicit with @EqualsAndHashCode on JPA entities
❌ Avoid
  • Using @Data or @Value when a Java Record would suffice
  • Using @Data on JPA entities (use individual annotations)
  • Using @EqualsAndHashCode with lazy-loaded JPA relationships
  • Overusing @SneakyThrows (makes exception handling unclear)

Common Pitfalls

PitfallProblemSolution
@Data on entitiesIncludes mutable setters, problematic equals/hashCodeUse @Getter, @Setter, explicit @EqualsAndHashCode
Missing @NoArgsConstructorJPA/Jackson need no-arg constructorAlways add with @AllArgsConstructor
Circular @ToStringStackOverflow with bidirectional relationshipsUse @ToString.Exclude on one side
Using Lombok for simple DTOsAdds unnecessary dependencyUse Java Records instead

Cookbook Index

Core Annotations
Field & Method Annotations
Integration Patterns

Comments

Markdown supported