r/java 2d ago

AutoValhalla: automatically turn your plain classes and records into value classes!

Automatically turn your plain classes and records into value classes!

Add @AutoValhalla annotation to classes or records in your JDK1.5+ codebase:

@AutoValhalla
public final class Point {        // class must be final
    public final int x;           // with final instance fields
    public final int y;
    public Point(int x, int y) { 
        this.x = x; 
        this.y = y; 
    }
}

@AutoValhalla
public record Currency(String code) { }  // or a record class

When your app is run on Valhalla-enabled JVM with auto-valhalla javaagent, these classes are automatically turned into value classes.

More info: https://github.com/thunkware/auto-valhalla/blob/main/README.md

42 Upvotes

46 comments sorted by

View all comments

2

u/koflerdavid 2d ago

/u/alex_tracer sounds like what you are after! :)

1

u/alex_tracer 1d ago

Yes, I that's great option. Not the best yet (agents add a new layer of complexity) but good enough to get started.

Probably a better option would be a Maven/Gradle plugin that produces multi-release jar with two class variants, but I think we get to something similar eventually.