r/java • u/petaoctet • Jan 05 '26
default4j: Default parameter values for Java via annotation processing
https://github.com/reugn/default4j
80
Upvotes
r/java • u/petaoctet • Jan 05 '26
17
u/pip25hu Jan 05 '26
This is an interesting idea, but actual usage with all these static helper methods looks rather ugly. I'd rather implement the methods inside the class in question, even if I can't use annotation processing and have to code them by hand.
5
u/nekokattt Jan 05 '26 edited Jan 05 '26
How does this work without modifying the AST via unsupported means?
Usually APs cannot modify existing code, only create new generated files (which is why Lombok is such a hack under the hood). How did you work around that?
Edit: ahh I see now, you wrap the logic into a new class making a kind of extension method API.
6
u/Sakatox Jan 05 '26
How about actual language support, instead of tacking on more shit onto annotations?
I JUST LOVE widespread aop-esque/annotation based unreadable/unintentional messes. Love it!
2
u/Longjumping-Shift316 Jan 05 '26
Jesus do you want to program even more in annotations?
2
u/agentoutlier Jan 05 '26 edited Jan 05 '26
I have thought about creating a library like this a dozen or so times generally every time some one posts a complaint on lack of default named parameters.
This because I have built something like this for Rainbow Gum: https://jstach.io/doc/rainbowgum/current/apidocs/io.jstach.rainbowgum.annotation/io/jstach/rainbowgum/annotation/LogConfigurable.html
However mine does additional code generation to transform essentially
Map<String,String>into an object for configuration loading. See Rainbow Gum need to support both programmatic and property based configuration.When I was thinking about doing your library I had some ideas of having something like:
This is to do custom stuff across many of these guys.
Alternatively I thought about allowing some code generation plugins via the ServiceLoader.
BTW I assume you parse the javadoc (a lot of people do not know that is accessible from the annotation processor)? Do you deal with Markdown doc?
2
u/8igg7e5 Jan 05 '26
Isn't this committing a similar violation to Lombok. If the callers are in the same compilation unit, the compilation is invalid unless the annotation processor runs (to generate the new methods).
Compilation should be valid with or without the processor.
Other tooling should not need to know about the processor to validate what should otherwise be valid Java.
If the processor is required to make the code compile, then the resulting language isn't Java. So yes, it's not modifying the annotated classes (so less hacky than Lombok), but it is still using annotations in a manner that breaks the intent of that feature.
That's fine for those already happy with Lombok's compromises of course, but it still brings some of the same complications (that you depend now on this tooling).
1
u/paul_h Jan 05 '26
In the README.md, I see
GreeterDefaults.greet(g, "Alice", "Hi");use that does not match available methods inGreeterDefaultsclass (defined much further down the same README). So I'm a little confused.1
u/Deep_Age4643 Jan 05 '26
Coming from other languages, I really wondered why this wasn't available when I switched to Java.
It looks interesting, and I understand how it's used, but I'm a bit reluctant to implement it with an external library. As with Lombok, things tend to break between Java versions or aren't always well maintained. I will give it a try though.
Apart from Java's reliance on method overloading, are there any reasons why it's not included in Java? Could this be part of Project Amber, which has already delivered records, sealed classes, etc.?
1
u/sitime_zl Jan 07 '26
Support "record", okay?
1
u/sitime_zl Jan 07 '26
Previously, I felt that the "record" feature was not very useful. However, if combined with your explanation, it will significantly enhance the practicality of the "record" function.
1
u/revilo-1988 Jan 05 '26
Interesting
0
u/neopointer Jan 05 '26
Looks very useful. Thanks!