In the following examples, i also add field-level constraints for JSR-303 validation.
@lombok.Data
public class UserInfo {
@Min(1)
private int id = -1;
@NotNull
private String username;
@NotNull
@Length(min = 5)
private String fullname;
@NotNull
private ZonedDateTime registeredAt;
}Use as a normal bean (instantiate with default constructor and set fields):
UserInfo userinfo = new UserInfo();
userinfo.setId(17);
userinfo.setFullname("Michail Alexakis");
userinfo.setUsername("malex");
userinfo.setRegisteredAt(ZonedDateTime.now()); @lombok.Value(staticConstructor = "of")
public class UserInfo {
@Min(1)
private final int id;
@NotNull
private final String username;
@NotNull
@Length(min = 5)
private final String fullname;
@NotNull
private final ZonedDateTime registeredAt;
}Instantiate using the static constructor (with all fields):
UserInfo userinfo = UserInfo.of(15, "lalakis", "Michail Alexakis", ZonedDateTime.now());// Note #1: The initializing expressions inf fields annotated with lombok.Builder.Default
// will be used as default values for builder's (corresponding) field
// Note #2: The fields are defined as non-final in order to supply an initializing expression
// but in the value class will actually be final!
@lombok.Builder
@lombok.ToString
@lombok.Value
public class UserInfo {
@Min(1)
@lombok.Builder.Default
private int id = -1;
@NotNull
private String username;
@NotNull
@Length(min = 5)
private String fullname;
@NotNull
@lombok.Builder.Default
private ZonedDateTime registeredAt = ZonedDateTime.of(1999, 1, 1, 0, 0, 0, 0, ZoneId.of("Europe/Athens"));
}Instantiate using the dedicated builder:
UserInfo userinfo = UserInfo.builder()
.id(17)
.fullname("Michail Alexakis")
.username("lalakis")
.registeredAt(ZonedDateTime.now())
.build(); Follow the official instructions and run the installer (inside lombok.jar); will ask for the path to an Eclipse installation
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>hello-lombok</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello-lombok</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
<!-- For JSR-303 validation -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.17.Final</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b09</version>
</dependency>
<!-- For unit testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Debug generated code from Lombok -->
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.8.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
<configuration>
<verbose>true</verbose>
<sourceDirectory>src/main/java</sourceDirectory>
<outputDirectory>target/delombok</outputDirectory>
<!-- do not add to build path! will cause duplicate classes to be defined -->
<addOutputDirectory>false</addOutputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>