Automatic generation of getter and setter methods with configurable access levels, lazy evaluation support, and annotation forwarding capabilities.
Generates getter methods for fields with configurable access levels and lazy evaluation support.
/**
* Put on any field to make lombok build a standard getter.
* This annotation can also be applied to a class, in which case it'll be as if all non-static fields
* that don't already have a @Getter annotation have the annotation.
*/
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
public @interface Getter {
/**
* If you want your getter to be non-public, you can specify an alternate access level here.
*
* @return The getter method will be generated with this access modifier.
*/
AccessLevel value() default AccessLevel.PUBLIC;
/**
* Any annotations listed here are put on the generated method.
* The syntax for this feature depends on JDK version.
* up to JDK7: @Getter(onMethod=@__({@AnnotationsGoHere}))
* from JDK8: @Getter(onMethod_={@AnnotationsGohere})
*
* @return List of annotations to apply to the generated getter method.
*/
AnyAnnotation[] onMethod() default {};
/**
* If true, the generated getter will calculate the value once, the first time the getter is called,
* and cache the result for subsequent calls.
*
* @return Whether to generate a lazy getter.
*/
boolean lazy() default false;
}Usage Examples:
import lombok.Getter;
import lombok.AccessLevel;
public class Example {
@Getter
private String name;
@Getter(AccessLevel.PROTECTED)
private int age;
@Getter(lazy = true)
private final String expensiveValue = computeExpensiveValue();
private String computeExpensiveValue() {
// Expensive computation
return "expensive result";
}
}
// Generated methods:
// public String getName() { return this.name; }
// protected int getAge() { return this.age; }
// public String getExpensiveValue() { /* lazy computation */ }Class-Level Usage:
@Getter
public class Person {
private String firstName;
private String lastName;
@Getter(AccessLevel.PRIVATE)
private String ssn; // Override class-level setting
}
// Generated:
// public String getFirstName() { return this.firstName; }
// public String getLastName() { return this.lastName; }
// private String getSsn() { return this.ssn; }Generates setter methods for fields with configurable access levels and annotation forwarding.
/**
* Put on any field to make lombok build a standard setter.
* This annotation can also be applied to a class, in which case it'll be as if all non-static,
* non-final fields that don't already have a @Setter annotation have the annotation.
*/
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
public @interface Setter {
/**
* If you want your setter to be non-public, you can specify an alternate access level here.
*
* @return The setter method will be generated with this access modifier.
*/
AccessLevel value() default AccessLevel.PUBLIC;
/**
* Any annotations listed here are put on the generated method.
* The syntax for this feature depends on JDK version.
* up to JDK7: @Setter(onMethod=@__({@AnnotationsGoHere}))
* from JDK8: @Setter(onMethod_={@AnnotationsGohere})
*
* @return List of annotations to apply to the generated setter method.
*/
AnyAnnotation[] onMethod() default {};
/**
* Any annotations listed here are put on the generated method's parameter.
* The syntax for this feature depends on JDK version.
* up to JDK7: @Setter(onParam=@__({@AnnotationsGoHere}))
* from JDK8: @Setter(onParam_={@AnnotationsGohere})
*
* @return List of annotations to apply to the generated setter method's parameter.
*/
AnyAnnotation[] onParam() default {};
}Usage Examples:
import lombok.Setter;
import lombok.AccessLevel;
import lombok.NonNull;
public class Example {
@Setter
private String name;
@Setter(AccessLevel.PACKAGE)
private int age;
@Setter
@NonNull
private String email; // Will generate null check in setter
}
// Generated methods:
// public void setName(String name) { this.name = name; }
// void setAge(int age) { this.age = age; } // package-private
// public void setEmail(@NonNull String email) {
// if (email == null) throw new NullPointerException("email is marked non-null but is null");
// this.email = email;
// }Class-Level Usage:
@Setter
public class Configuration {
private String host;
private int port;
private final String version = "1.0"; // No setter generated (final field)
@Setter(AccessLevel.PRIVATE)
private String apiKey; // Override class-level setting
}Generates wither methods that return new instances with modified field values for immutable objects.
/**
* Generates wither methods that return a clone of this object except with one field changed.
* Particularly useful for immutable data structures.
*/
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
public @interface With {
/**
* If you want your wither to be non-public, you can specify an alternate access level here.
*
* @return The wither method will be generated with this access modifier.
*/
AccessLevel value() default AccessLevel.PUBLIC;
/**
* Any annotations listed here are put on the generated method.
*
* @return List of annotations to apply to the generated wither method.
*/
AnyAnnotation[] onMethod() default {};
/**
* Any annotations listed here are put on the generated method's parameter.
*
* @return List of annotations to apply to the generated wither method's parameter.
*/
AnyAnnotation[] onParam() default {};
}Usage Examples:
import lombok.Value;
import lombok.With;
@Value
public class Person {
@With String name;
@With int age;
String email;
}
// Generated methods:
// public Person withName(String name) {
// return this.name == name ? this : new Person(name, this.age, this.email);
// }
// public Person withAge(int age) {
// return this.age == age ? this : new Person(this.name, age, this.email);
// }
// Usage
Person original = new Person("John", 30, "john@example.com");
Person older = original.withAge(31);
Person renamed = original.withName("Johnny");Class-Level Usage:
@Value
@With
public class ImmutablePoint {
int x;
int y;
String label;
}
// All fields get wither methods
ImmutablePoint point = new ImmutablePoint(10, 20, "origin");
ImmutablePoint moved = point.withX(15).withY(25);Lazy getters compute and cache values on first access:
public class DataProcessor {
@Getter(lazy = true)
private final List<String> processedData = loadAndProcessData();
private List<String> loadAndProcessData() {
// Expensive operation that runs only once
return Arrays.asList("processed", "data");
}
}
// Generated lazy getter with double-checked locking patternForward annotations to generated methods:
import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
public class User {
@Getter(onMethod_ = {@Override, @Nullable})
private String nickname;
@Setter(onParam_ = @NotNull)
private String email;
}
// Generated:
// @Override @Nullable public String getNickname() { return this.nickname; }
// public void setEmail(@NotNull String email) { this.email = email; }import lombok.*;
@Getter
@Setter
public class Product {
private String name;
@Getter(AccessLevel.NONE) // Override class-level @Getter
@Setter(AccessLevel.PROTECTED) // Override class-level @Setter
private double price;
@With // Add wither in addition to getter/setter
private String category;
}Property access annotations respect these lombok configuration settings:
lombok.getter.flagUsage: Control usage warnings for @Getterlombok.setter.flagUsage: Control usage warnings for @Setterlombok.accessors.fluent: Generate fluent accessor methodslombok.accessors.chain: Make setters return 'this'lombok.accessors.prefix: Strip prefixes from field names