Skip to content
pawel_labaj edited this page Apr 6, 2023 · 17 revisions

AutoRecord - Java record source generator

What is AutoRecord

Google AutoValue has long been used as a way to work with Value Classes in an easy way. However, when Java records were introduced, they lacked some features that AutoValue had, such as nullability checking, builders, and memoization. This is why AutoRecord was created.

AutoRecord is a code generator that helps you easily generate Java records. It provides an easy way to avoid writing repetitive boilerplate code. It generates the code with features such as:

How to use AutoRecord

To use AutoRecord, simply annotate your interface with @AutoRecord:

import pl.com.labaj.autorecord.AutoRecord;

@AutoRecord
interface Person {
    String name();
    int age();
}

AutoRecord will then generate a Java record class that implements your interface. The constructor parameters correspond, in order, to the abstract accessor methods:

import static java.util.Objects.requireNonNull;

import java.lang.String;
import javax.annotation.processing.Generated;
import pl.com.labaj.autorecord.GeneratedWithAutoRecord;

@Generated("pl.com.labaj.autorecord.AutoRecord")
@GeneratedWithAutoRecord
record PersonRecord(String name, int age) implements Person {
  PersonRecord {
    requireNonNull(name, () -> "name must not be null");
  }
}

Getting started

Maven

Add the following dependency to your pom.xml file:

<dependency>
    <groupId>pl.com.labaj</groupId>
    <artifactId>auto-record</artifactId>
    <version>${auto-record.version}</version>
</dependency>

Gradle

Declare the following dependency in your build.gradle script:

dependencies {
    annotationProcessor 'pl.com.labaj:auto-record:${autoRecordVersion}'
}

IDE

Depending on your IDE you are likely to need to enable Annotation Processing in your IDE settings.

Clone this wiki locally