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

Show parent comment

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)

1

u/[deleted] Oct 08 '23

[removed] — view removed comment

2

u/rastaman1994 Oct 08 '23

Without knowing the use case, it sounds like you need a value object, I.e a record. I'm assuming ur talking about value objects like SomeBusinessId or Birthday.

3

u/[deleted] Oct 08 '23

[removed] — view removed comment

3

u/rastaman1994 Oct 08 '23

I presume the majority of projects that have a use for Lombok are <17 like yours.

I disagree that Lombok in itself encourages good design, it's always the practices agreed on by devs, but I can't prove that. The annotations are noise too for some. I've just never really been bothered by Java's boilerplate, and the people that are can use Kotlin.

These discussions are just always so pointless.

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.

0

u/rastaman1994 Oct 06 '23

Ah Java vs C#, another debate as old as time :D just tell them to use Lombok.

→ 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.

-17

u/rzwitserloot Oct 05 '23

whereby you can see the code but the JVM prevents reflective access to it at runtime when the Java compiler is invoked.

The open source code.

Some projects use --add-opens and --add-exports for now but there is a good chance this will be removed in the future.

State your source.

The maven plugin currently adds the --add-opens stuff. Every source out there so far indicates that the OpenJDK maintainers want you to clearly decide to allow it, they want it to be a surprise - and --add-opens does that.

If somehow --add-opens goes away, well, it is open source.

4

u/nekokattt Oct 05 '23

Well, it is open source.

Relevant XKCD: https://xkcd.com/927/

15

u/rastaman1994 Oct 05 '23

There are loads of discussions between you and the Java developers where they state their issues with Lombok. I see it's happening here again. To me that adds risk to using Lombok because my assumption is the Java devs can decide supporting whatever Lombok is doing is hindering the platform development too much.

Lombok worked well for us in the past, before Jva 17+ and Kotlin addoption in our company, we just decided it's not for us anymore after evaluating what benefits Lombok actually has for us.

3

u/[deleted] Oct 06 '23

[removed] — view removed comment

3

u/rzwitserloot Oct 06 '23

Are you suggesting to create your own JDK distribution just for Lombok?

No. Just javac. Lombok is a compile time only dependency. The class files that are produced are things that could have been made with javac-without-lombok, we just replace boilerplate with less typing is all. Sometimes a lot less, but it's simply a matter of translating 'lombok stuff' into 'vanilla java stuff'. It's why delombok exists - it's not a translator (imagine there is a 'dekotlin' which turns kotlin code into java code - even if you could do that, that's a massive effort that exists quite separate from the main kotlin code which compiles kt files straight to class files - delombok is nothing like that, lombok translates various java constructs (generally, annotations) to java code. It's like a java macro system. Which plain jane annotation processors also are (they can generate code, 'code generation' is part of the JDK in the form of annotation processors), which is why I keep saying: Ron's claim that lombok is a new language implies any annotation processor is the same. The one and only difference is, the code we generate? It is stuck straight back in the very source file that kicked off the code generation, whereas plain jane annotation processors can only generate new code files. A difference, and one that goes beyond what the AP spec promises it can do, certainly a topic worthy of discussion.

However, 'lombok is a different language' - no. Not unless you consider the presence of any annotation processor in the compilation step as 'therefore this is written in a different language'.

1

u/ankercrank Oct 08 '23

Given how many projects rely on Lombok it would be crazy for the Java core devs to break it. If they did make a breaking change they’d have to provide a working replacement - anything short of this would cause many projects to never update Java to newer versions, I doubt the Java core devs want that.

1

u/[deleted] Oct 08 '23

[removed] — view removed comment

1

u/ankercrank Oct 08 '23

I can assure you support would be maintained. OpenJDK would provide support if Oracle doesn’t.