Java record newContext generator
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:
- nullability checking
- builders - incorporating Randgalt/record-builder library
- memoization
- ignoring fields in
hashCode()
andequals()
methods - generated common methods if the record has an array recordComponent
- exclusion from JaCoCo test coverage analysis
AutoRecord allows users to customize record generation process by:
- specifying options
- using custom annotation templates
- implementing custom extensions
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.
To use AutoRecord, simply annotate your interface with @AutoRecord annotation:
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 interface methods:
import pl.com.labaj.autorecord.GeneratedWithAutoRecord;
import javax.annotation.processing.Generated;
import static java.util.Objects.requireNonNull;
@Generated("pl.com.labaj.autorecord.AutoRecord")
@GeneratedWithAutoRecord
record PersonRecord(String name, int age) implements Person {
PersonRecord {
requireNonNull(name, "name must not be null");
}
}
📝 Note |
---|
Here you can see example of generated record with all features provided by the library: Full.java |
For more information on how to use AutoRecord and all its features, please visit the project's Wiki.
Project | Description |
---|---|
Auto Record API | Annotations to mark interfaces to be processed |
Auto Record | Annotation processor to generate records |
Auto Record Utilities | Utility classes used by generated records |
ARICE API | Annotations to mark interface methods, used by ARICE |
ARICE (Auto Record Immutable Collections Extension) | The extension for customizing a record generation process with support for immutable collections |
ARICE Utilities | Utility classes used by ARICE |
Add the following dependency to your pom.xml
file:
<dependency>
<groupId>pl.com.labaj.autorecord</groupId>
<artifactId>auto-record</artifactId>
<version>${auto-record.version}</version>
<scope>provided</scope>
</dependency>
Declare the following dependency in your build.gradle
script:
dependencies {
annotationProcessor 'pl.com.labaj.autorecord:auto-record:${autoRecordVersion}'
}
Depending on your IDE you are likely to need to enable Annotation Processing in your IDE settings.
We welcome contributions from all developers! If you would like to contribute to AutoRecord, please refer to the Contributing guide for more information on how to get started.
This project is licensed under the Apache 2.0 License.