r/java • • Oct 05 '23

How does the lombok magic work underneath?

https://www.unlogged.io/post/how-does-the-lombok-magic-work-underneath
133 Upvotes

143 comments sorted by

View all comments

60

u/roiroi1010 Oct 05 '23

Nice article. This comment section will probably soon be filled with people who argue strongly against using Lombok. I actually love Lombok, but in large project with many contributors, I have seen some developers are struggling with it. Nowadays I typically only use Lombok for my personal projects.

70

u/Rjs617 Oct 05 '23

My current company’s code base heavily uses Lombok, and I love it. I know IDEs can generate everything. However, getters, setters, constructors, builders, toString methods, logger declarations, and on and on clutter up the code so much. With Lombok, that’s all gone, and when someone adds new fields, there is nothing to update. It makes the language so much more readable and maintainable, and so much faster to write. It’s also easy to understand. Whoever those developers are who are confused by Lombok, they should not be let near production code.

35

u/rastaman1994 Oct 05 '23

At this point, Lombok feels like a risky dependency to add. Lombok uses internals Java platform devs don't want them to use, and they're continuing to further encapsulate those things. It's very clear that the platform devs do not like what Lombok is doing, so it's just a matter of time until it stops working altogether. Some of our projects were harder to upgrade to later Java versions because of Lombok, so we decided to boot Lombok and haven't missed it.

8

u/zman0900 Oct 06 '23

Worst case you can always just run delombok and your auto-formatter on everything, should the lombok project ever die.

1

u/rastaman1994 Oct 06 '23

I'd rather not wait for the inevitable. The time we saved with Lombok is lost arguing about Lombok or explaining the magic. How many times a month are you adding/removing fields anyway...

10

u/hsoj48 Oct 06 '23

I work at a fortune 50 company that heavily uses lombok. No one in real life is arguing about it. I don't get the issue. Don't use any library ever because someday it might change amirite?

→ More replies (1)
→ More replies (4)

0

u/[deleted] Oct 06 '23

[deleted] — view deleted comment

4

u/rastaman1994 Oct 06 '23

In the end its about preference, our teams decided we don't want this controversial dependency anymore, especially when Kotlin exists. The time saved with Lombok is lost arguing about it or explaining it.

0

u/[deleted] Oct 06 '23

[deleted] — view deleted comment

4

u/rastaman1994 Oct 06 '23

Like I said, preference. Its a decision every team can make for themselves, we decided against because its not a timesave for us. We spend maybe 30 minutes a month adding or removing fields from classes, so not worth adding a controversial dependency for automating that.

2

u/[deleted] Oct 06 '23

Lombok isn't for time savings related to adding fields. Any IDE does that easily. It's for keeping your C# devs from quitting.

→ More replies (1)
→ More replies (14)

-23

u/rzwitserloot Oct 05 '23

It's not, see our maven plugin branch. All relevant parts of the java ecosystem is open source. Go spread your FUD elsewhere.

30

u/nekokattt Oct 05 '23 edited Oct 05 '23

They never said it was not open source, they said it was encapsulated (i.e. behind closed JPMS modules like jdk.compiler) whereby you can see the code but the JVM prevents reflective access to it at runtime when the Java compiler is invoked. Some projects use --add-opens and --add-exports for now but there is a good chance this will be removed in the future. OpenJDK devs are not scared of breaking things that are considered bad practise to do. They are planning on deprecating the ability to dynamically attach JVM agents too, which will mean changes are needed in components like Surefire for Mockito to continue working in the future.

Same reason spotbugs broke for a while on Java 17 a year or two ago, that was accessing components within jdk.compiler that are not documented for external usage.

Manifold framework is also in the same boat for this.

IMO pointing out that something is potentially volatile in a changing environment is not FUD. It may be an over-cautious observation, but unless the Lombok devs are also part of the core language design discussions that OpenJDK have, then there is no guarantee it will always be able to work in the way it currently does.

Like any library, it is a very small risk. A risk that is increased slightly due to the use of APIs that clearly have javadocs that say something along the lines of "this API is for internal use and may break/change/go away without warning".

It is similar to apps that make use of kernel32.dll on Windows. Sure, you can see what is in it, but it doesn't mean the API inside is guaranteed to remain as-is.

→ More replies (2)
→ More replies (4)
→ More replies (3)

11

u/pragmatick Oct 05 '23

My main issue with it (and I use it in most of my projects) that it's a lot harder to find references to getters, setters and constructors if they're generated by lombok and even harder (or impossible?) to set breakpoints in them.

14

u/edubkn Oct 05 '23

That was naturally resolved for me when moving to Java 17 and using records

3

u/Devel93 Oct 06 '23

Intellij ctrl + click lists all getters/setters

3

u/Cell-i-Zenit Oct 05 '23

My main issue with it (and I use it in most of my projects) that it's a lot harder to find references to getters, setters and constructors if they're generated by lombok and even harder (or impossible?) to set breakpoints in them.

The intellij plugin is really good and you can always use Delombok to set breakpoints

0

u/krzyk Oct 05 '23

Delombok doesn't generate source? I'm also struggling with debugging code that uses lombok, it is a PITA (in IntelliJ) and I slowly migrate out of it.

6

u/Cell-i-Zenit Oct 05 '23

Delombok generates exactly the code lombok is adding behind the scenes

3

u/krzyk Oct 06 '23

Yeah, but it doesn't do it on the fly during debugging session. It does it once when you want to get rid of lombok.

4

u/hsoj48 Oct 06 '23

So your whole issue is that you have to put the breakpoint on the field access line rather than the getter line?

3

u/Enough-Ad-5528 Oct 05 '23

I am a big proponent of Lombok as well. You say:

I have seen some developers are struggling with it.

Curious, if you don't mind sharing, what kinds of problems have you seen in practice people struggling with?

12

u/roiroi1010 Oct 05 '23

Sad to say I’m stuck with some sub-par developers in my team and they struggle with plain Java also, lol.

But they have trouble understanding the concept of Lombok. And when they start learning it they will put @Data and @Builder and @With on every class without thinking about the consequences.

Most seasoned Java developers know it’s not good for JPA Entities.

But even having @ToString has caused us issues when they log fields that don’t belong in Kibana because privacy concerns or strings that get very long.

Ideally things like this should be noticed in code reviews, but I’ve seen some of our senior developers fail to catch mistakes done with Lombok annotations.

5

u/Cell-i-Zenit Oct 06 '23

But then you can say the same about basically anything. Hibernate, ApacheCommons, Java even. If the devs are bad, there is nothing to win here.

0

u/verocoder Oct 05 '23

There are a bunch of weird things here like the logging issues when your log ingested should be dealing with that. JPA persisted objects really suit Lombok you just want to add @noargsconstructor to them.

I will agree Lombok is an extra layer of complexity for new devs learning Java.

→ More replies (2)