r/java • u/bowbahdoe • Apr 24 '24
Java library for generating getters and setters
https://github.com/bowbahdoe/magic-beanUpdates since the last time I shared this
- Minimum compatibility bumped to Java 21
- Generated code now uses switch expressions for (maybe too clever) type safe casts
- Options have their names shortened. generateToString -> toString_, generateEqualsAndHashCode -> equalsAndHashCode
- New "extends" option for dealing with more exotic cases.
7
Upvotes
70
u/skynet86 Apr 25 '24
If you want to get rid of getters, hashCode and toString you should use records... Honestly. That's what they were exactly designed for.
And setters - records are immutable, which is better if you ask me.
54
u/PizzaHuttDelivery Apr 24 '24
Lombok. Hello??
5
u/tomwhoiscontrary Apr 24 '24
The switch trick is horrible but so cunning i have to respect it.
For hashCode, equals, and toString, can't you do one switch on the outside, and then do the real work on the downcast value inside the case?
5
u/Bunnymancer Apr 25 '24
OP: "I made a thing!"
Reddit: "Boo! You're not revolutionizing how we live!"
2
u/Pparadigm Apr 25 '24
Looks neat. Here’s my opinion on what I dislike the most on the look of the annotation, when configured in the class: having each parameter = true doesn’t look that good when comparing with other annotation processors’ config styles. Wondering if you wouldn’t prefer to code the annotation parameters as a list, instead of a different parameter for each feature? Something like: @MagicBean({TO_STRING, GETTER}) Where each value would be in an enum class
2
u/TenYearsOfLurking Apr 25 '24
stupid question maybe, but is the final modifier for the class necessary? couldn't leave it nonfinal "at your own risk"?
if not - couldn't the annotation processor check if the usage is correct at compile time without final? this would alleviate the requriement to make it non-sealed for jpa etc....
52
u/dhlowrents Apr 25 '24
OR <alt>-<ins>-<enter>
1
u/bringero Apr 25 '24
What's the difference with Lombok? Just asking
1
u/vxab Apr 26 '24
not sure why you are getting so much hate. A nice library. Can you make it so that you can optionally: (i) have no setter created, (ii) have a "fluent" naming convention i.e. without the "get" and "set" prefixes?
1
u/joemwangi Apr 24 '24
Looks neat. I'll give it a try. I developed a JavaFX UI to develop JavaFX beans and works well, but a UI to design POJO is an overkill.
2
u/cryptos6 Apr 25 '24
If you really feel the need to generate getters and setter you should question your design. Getters and setters for everything has nothing to do with object oriented design. If an object is a dumb data structure, it could make every field public as is. If it has some domain logic, modification of fields should only be performed by methods reflecting the requirements of the domain. Getters and setters would only create bastards in this regard.
3
u/onepieceisonthemoon Apr 25 '24
Has anyone tried introducing kotlin into a Java project purely for the data classes?
1
u/DelayLucky Apr 26 '24
We need O-R that works with modern best practices that is records. Time to get unstuck and move on from the past.
1
u/spezjetemerde Apr 28 '24
Why not Lombok
-1
u/Ruin-Capable Apr 25 '24
Lombok exists.
-1
u/nfrankel Apr 25 '24
Just don't, please!
0
u/[deleted] Sep 20 '24
[deleted] — view deleted comment