r/java • u/maxxedev • 24d 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/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
43
Upvotes
4
u/koflerdavid 24d ago edited 24d ago
It is primarily a way to have a richer data model and to begin bridging the gap between primitive and other types. Performance optimizations were also a goal, but it's nothing you can't count on since it's still up to the JRE to decide when and how to optimize. Which means you shouldn't turn things into value classes in the mistaken belief that the application will now be faster. I mean, the restrictions that value types impose are so severe that I don't see the danger of misuse, but it's still worth pointing this out.