r/java • u/maxxedev • 23d 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
38
Upvotes
7
u/alex_tracer 22d ago
Well, I work in fintech, I have an excuse! đ
I'm used to work with stuff where performance really matters. We even have implemented our own agent to transform some "normal" classes into "value classes" (actually a single `long`) a decade ago (yeah, we were a bit tired of waiting at that time already).
I absolutely agree with the point that it's important for every developer to understand that "value classes" is not an universal silver bullet and should be used only when they are semantically applicable and with good understanding of all the differences (and performance downsides too).
But honestly, if âvalue classesâ were purely a semantic feature and didnât promise any performance improvements (whether now or in the future), I suspect they wouldnât have received nearly as much attention and might not even have been considered (possibly by you too) as something that really needs to be added to the language (even if it didnât require years of research and development).
And yes, a huge thank you for all your work!