Generated immutable value classes for Java 8+ using annotation processing
—
The @ToPrettyString annotation generates human-readable string representations with structured formatting, making complex objects easy to read and debug.
@AutoValue
public abstract class Person {
public abstract String name();
public abstract int age();
public abstract List<String> hobbies();
@ToPrettyString
public abstract String toPrettyString();
public static Person create(String name, int age, List<String> hobbies) {
return new AutoValue_Person(name, age, hobbies);
}
}Person person = Person.create("Alice", 30, Arrays.asList("reading", "coding", "hiking"));
System.out.println(person.toPrettyString());
// Output:
// Person{
// name = Alice,
// age = 30,
// hobbies = [
// reading,
// coding,
// hiking,
// ]
// }Compare pretty string output with regular toString():
Person person = Person.create("Alice", 30, Arrays.asList("reading", "coding"));
System.out.println(person.toString());
// Person{name=Alice, age=30, hobbies=[reading, coding]}
System.out.println(person.toPrettyString());
// Person{
// name = Alice,
// age = 30,
// hobbies = [
// reading,
// coding,
// ]
// }Pretty strings handle nested objects and collections beautifully:
@AutoValue
public abstract class Address {
public abstract String street();
public abstract String city();
public abstract String zipCode();
@ToPrettyString
public abstract String toPrettyString();
public static Address create(String street, String city, String zipCode) {
return new AutoValue_Address(street, city, zipCode);
}
}
@AutoValue
public abstract class Company {
public abstract String name();
public abstract Address headquarters();
public abstract List<Address> offices();
public abstract Map<String, String> contacts();
@ToPrettyString
public abstract String toPrettyString();
public static Company create(
String name,
Address headquarters,
List<Address> offices,
Map<String, String> contacts) {
return new AutoValue_Company(name, headquarters, offices, contacts);
}
}Usage:
Address hq = Address.create("123 Main St", "Springfield", "12345");
Address office = Address.create("456 Oak Ave", "Springfield", "12346");
Company company = Company.create(
"Tech Corp",
hq,
Arrays.asList(office),
Map.of("sales", "sales@techcorp.com", "support", "help@techcorp.com"));
System.out.println(company.toPrettyString());
// Output:
// Company{
// name = Tech Corp,
// headquarters = Address{
// street = 123 Main St,
// city = Springfield,
// zipCode = 12345,
// },
// offices = [
// Address{
// street = 456 Oak Ave,
// city = Springfield,
// zipCode = 12346,
// },
// ],
// contacts = {
// sales = sales@techcorp.com,
// support = help@techcorp.com,
// }
// }Pretty strings provide special formatting for different collection types:
@AutoValue
public abstract class DataContainer {
public abstract List<String> items();
public abstract Set<Integer> numbers();
public abstract Map<String, Object> properties();
public abstract Optional<String> description();
@ToPrettyString
public abstract String toPrettyString();
public static DataContainer create(
List<String> items,
Set<Integer> numbers,
Map<String, Object> properties,
Optional<String> description) {
return new AutoValue_DataContainer(items, numbers, properties, description);
}
}Usage:
DataContainer container = DataContainer.create(
Arrays.asList("apple", "banana", "cherry"),
Set.of(1, 2, 3, 5, 8),
Map.of("color", "red", "size", "large", "count", 42),
Optional.of("A sample data container"));
System.out.println(container.toPrettyString());
// Output:
// DataContainer{
// items = [
// apple,
// banana,
// cherry,
// ],
// numbers = [
// 1,
// 2,
// 3,
// 5,
// 8,
// ],
// properties = {
// color = red,
// size = large,
// count = 42,
// },
// description = A sample data container
// }Pretty strings handle special characters and newlines gracefully:
@AutoValue
public abstract class TextData {
public abstract String title();
public abstract String content();
public abstract List<String> lines();
@ToPrettyString
public abstract String toPrettyString();
public static TextData create(String title, String content, List<String> lines) {
return new AutoValue_TextData(title, content, lines);
}
}Usage:
TextData data = TextData.create(
"Sample Document",
"This is a document\nwith multiple lines\nand\ttabs",
Arrays.asList("Line 1", "Line with\nnewline", "Line\twith\ttabs"));
System.out.println(data.toPrettyString());
// Output:
// TextData{
// title = Sample Document,
// content = This is a document
// with multiple lines
// and tabs,
// lines = [
// Line 1,
// Line with
// newline,
// Line with tabs,
// ]
// }Non-AutoValue classes can provide pretty string methods:
public class CustomClass {
private final String name;
private final int value;
public CustomClass(String name, int value) {
this.name = name;
this.value = value;
}
@ToPrettyString
public String toPrettyString() {
return "CustomClass{name=" + name + ", value=" + value + "}";
}
// Regular getters...
}
@AutoValue
public abstract class Container {
public abstract CustomClass custom();
public abstract List<CustomClass> items();
@ToPrettyString
public abstract String toPrettyString();
public static Container create(CustomClass custom, List<CustomClass> items) {
return new AutoValue_Container(custom, items);
}
}The pretty string generator will use the custom toPrettyString() method:
CustomClass custom1 = new CustomClass("first", 1);
CustomClass custom2 = new CustomClass("second", 2);
Container container = Container.create(custom1, Arrays.asList(custom1, custom2));
System.out.println(container.toPrettyString());
// Output:
// Container{
// custom = CustomClass{name=first, value=1},
// items = [
// CustomClass{name=first, value=1},
// CustomClass{name=second, value=2},
// ]
// }Pretty string works with AutoValue class hierarchies:
@AutoValue
public abstract class Animal {
public abstract String name();
public abstract int age();
@ToPrettyString
public abstract String toPrettyString();
}
@AutoValue
public abstract class Dog extends Animal {
public abstract String breed();
public abstract boolean trained();
@ToPrettyString
@Override
public abstract String toPrettyString();
public static Dog create(String name, int age, String breed, boolean trained) {
return new AutoValue_Dog(name, age, breed, trained);
}
}Usage:
Dog dog = Dog.create("Buddy", 3, "Golden Retriever", true);
System.out.println(dog.toPrettyString());
// Output:
// Dog{
// name = Buddy,
// age = 3,
// breed = Golden Retriever,
// trained = true,
// }You can use @ToPrettyString to override the default toString():
@AutoValue
public abstract class Product {
public abstract String name();
public abstract double price();
public abstract List<String> categories();
public abstract Map<String, String> attributes();
@ToPrettyString
@Override
public abstract String toString();
public static Product create(
String name,
double price,
List<String> categories,
Map<String, String> attributes) {
return new AutoValue_Product(name, price, categories, attributes);
}
}Now toString() will return the pretty formatted string:
Product product = Product.create(
"Laptop",
999.99,
Arrays.asList("Electronics", "Computers"),
Map.of("brand", "TechCorp", "warranty", "1 year"));
System.out.println(product); // Calls toString() which uses pretty formatting
// Output:
// Product{
// name = Laptop,
// price = 999.99,
// categories = [
// Electronics,
// Computers,
// ],
// attributes = {
// brand = TechCorp,
// warranty = 1 year,
// }
// }Pretty strings work with other AutoValue extensions:
@SerializableAutoValue
@AutoValue
public abstract class CachedData implements Serializable {
public abstract String data();
public abstract Optional<List<String>> tags();
@Memoized
public String processedData() {
return data().toUpperCase().trim();
}
@ToPrettyString
public abstract String toPrettyString();
public static CachedData create(String data, Optional<List<String>> tags) {
return new AutoValue_CachedData(data, tags);
}
}Pretty strings handle empty collections and null values gracefully:
@AutoValue
public abstract class Example {
@Nullable
public abstract String nullableField();
public abstract List<String> emptyList();
public abstract Optional<String> emptyOptional();
@ToPrettyString
public abstract String toPrettyString();
public static Example create(
@Nullable String nullableField,
List<String> emptyList,
Optional<String> emptyOptional) {
return new AutoValue_Example(nullableField, emptyList, emptyOptional);
}
}
Example example = Example.create(null, Collections.emptyList(), Optional.empty());
System.out.println(example.toPrettyString());
// Output:
// Example{
// nullableField = null,
// emptyList = [],
// emptyOptional = <absent>,
// }Install with Tessl CLI
npx tessl i tessl/maven-com-google-auto-value--auto-value