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

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