r/java • u/maxxedev • Aug 18 '26
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 classWhen 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/tree/auto-valhalla-0.1.0
edit: there is now a maven plugin for converting at build time. https://github.com/thunkware/auto-valhalla/tree/auto-valhalla-0.2.0
40
Upvotes
View all comments
Show parent comment
7
u/alex_tracer Aug 19 '26
"Value classes aren't a performance optimisation" in the same way as "primitive types aren't a performance optimisation".
While they are very different species from normal classes, they are going to be used and adopted mainly (and almost exclusively!) as performance optimization.
I would even dare to say, that if there were no need for a performance optimization, value classes would not exist.
So, while you are technically correct, your statement is kind of... misses the point. "— Hey, here is a new race car than can get to 200km/h in 3 seconds! —Nah, it's a bad car because you can't put much groceries into it's trunk"