Generated immutable value classes for Java 8+ using annotation processing
npx @tessl/cli install tessl/maven-com-google-auto-value--auto-value@1.11.0AutoValue is a Java annotation processor that automatically generates immutable value classes, eliminating the need to manually write repetitive equals(), hashCode(), and toString() methods. It provides a clean, declarative way to create value objects using annotations, supporting advanced features like builders, extensions for serialization and memoization, and integration with popular Java frameworks.
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value-annotations</artifactId>
<version>1.11.0</version>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
<version>1.11.0</version>
<scope>provided</scope>
</dependency>import com.google.auto.value.AutoValue;
import com.google.auto.value.AutoBuilder;
import com.google.auto.value.AutoAnnotation;
import com.google.auto.value.AutoOneOf;import com.google.auto.value.AutoValue;
@AutoValue
public abstract class Person {
public static Person create(String name, int age) {
return new AutoValue_Person(name, age);
}
public abstract String name();
public abstract int age();
}
// Usage
Person person = Person.create("Alice", 30);
System.out.println(person); // Person{name=Alice, age=30}
System.out.println(person.equals(Person.create("Alice", 30))); // trueAutoValue uses annotation processing to generate implementation classes at compile time. The key components are:
Generate immutable value classes with proper equals, hashCode, and toString implementations.
@AutoValue
public abstract class YourClass {
// Abstract getters define properties
public abstract PropertyType propertyName();
// Static factory method
public static YourClass create(PropertyType propertyName) {
return new AutoValue_YourClass(propertyName);
}
}Generate builder classes for complex object construction with optional properties.
@AutoValue
public abstract class YourClass {
public abstract PropertyType property();
public static Builder builder() {
return new AutoValue_YourClass.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder property(PropertyType value);
public abstract YourClass build();
}
}Generate builders for existing classes or constructors.
@AutoBuilder(ofClass = TargetClass.class)
public abstract class TargetClassBuilder {
public abstract TargetClassBuilder property(PropertyType value);
public abstract TargetClass build();
public static TargetClassBuilder builder() {
return new AutoBuilder_TargetClassBuilder();
}
}Generate proper annotation implementations with correct equals and hashCode.
@AutoAnnotation
public static AnnotationType createAnnotation(ParamType param) {
return new AutoAnnotation_ClassName_createAnnotation(param);
}Generate tagged union (one-of) types for representing values that can be one of several types.
@AutoOneOf(Kind.class)
public abstract class StringOrInteger {
public enum Kind { STRING, INTEGER }
public abstract Kind getKind();
public abstract String string();
public abstract int integer();
public static StringOrInteger ofString(String s) {
return AutoOneOf_StringOrInteger.string(s);
}
public static StringOrInteger ofInteger(int i) {
return AutoOneOf_StringOrInteger.integer(i);
}
}Cache method results for expensive computations with thread-safe lazy initialization.
@Memoized
public abstract ComputedType expensiveComputation() {
return performExpensiveCalculation();
}Generate serializable implementations for classes with non-serializable fields.
@SerializableAutoValue
@AutoValue
public abstract class SerializableClass implements Serializable {
public abstract Optional<String> optionalField();
public abstract ImmutableList<Integer> listField();
}Generate human-readable string representations with structured formatting.
@ToPrettyString
public abstract String toPrettyString();Create custom extensions to modify or enhance generated code.
public abstract class AutoValueExtension {
public abstract boolean applicable(Context context);
public abstract String generateClass(Context context, String className, String classToExtend, boolean isFinal);
}// Core annotation
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface AutoValue {
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface Builder {}
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface CopyAnnotations {
Class<? extends Annotation>[] exclude() default {};
}
}
// Builder annotation
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface AutoBuilder {
String callMethod() default "";
Class<?> ofClass() default Void.class;
}
// Annotation generation
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface AutoAnnotation {}
// Tagged unions
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
public @interface AutoOneOf {
Class<? extends Enum<?>> value();
}