Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active September 8, 2019 19:53
Show Gist options
  • Select an option

  • Save drmalex07/e1d4ed4372382e628fac885d436d9886 to your computer and use it in GitHub Desktop.

Select an option

Save drmalex07/e1d4ed4372382e628fac885d436d9886 to your computer and use it in GitHub Desktop.
An example with Lombok-generated classes. #lombok #java

README - Lombok

In the following examples, i also add field-level constraints for JSR-303 validation.

1. Typical usage

1.A. A java bean (getters/setters)

@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());       

1.B. A value object with static constructor and getters

@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());

1.C. A value object with a builder

// 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();     

2. Eclipse setup

Follow the official instructions and run the installer (inside lombok.jar); will ask for the path to an Eclipse installation

2. Typical Maven Setup

<?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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment