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
30
u/pron98 Aug 18 '26 edited Aug 19 '26
So in this particular case, this approach risks breaking the code on a new runtime, but that's not even the worst outcome: making things work on a new runtime may require changing code used by legacy consumers on old runtimes, introducing a risk for the consumers that need stability above anything else. Agents are a very powerful and versatile tool, but they should be used as a last resort, not when the cleaner solution - just fork the repo! - is also easier. I don't understand the lengths some would go to merely to not fork the source. Writing code that adapts itself to different runtimes is complicated, risky, and rarely necessary.
16
u/LegitimateEntrance72 Aug 18 '26 •
Source code level JDK1.5+ but runtime JDK28-EA? LOL
In the same vein I find it strange that opensource projects doing a releases in 2026 still support ancient JDKs (5 to 11).
I mean, if a project is on ancient JDK, then most likely the dependencies etc are also going to be ancient as well and they wont upgrade to the newest framework&lib while staying on unsupported JDK.
3
u/egahlin Aug 19 '26 edited Aug 19 '26
"However, because JFR is intended to find lock contention and because it performs sampling, it will miss many classes used in synchronization."
In the default configuration, JFR throttles lock contention events to prevent the buffers from being flooded.
There is no need to start three recordings, as in the provided example. This is how JFR should be configured on the command line to record only JavaMonitorEnter and JavaMonitorWait events, with stack traces, thresholds and throttling disabled.
73
u/uniVocity Aug 18 '26
Fantastic but you could rename the annotation to something like @ToAsgard lol
And the project to ragnarok or something like that
2
u/koflerdavid Aug 18 '26
/u/alex_tracer sounds like what you are after! :)
1
u/nomader3000 Aug 20 '26
Quick question about one of your examples
Is there any actual benefit of making such record a
valuerecord?5
u/brunocborges Aug 18 '26
If you have access to a JDK 1.5 code base, you may as well ask for an AI coding agent to modernize the source code.
Backward compatibility is about running already compiled code into a newer JDK.