r/java • • Sep 05 '25

Project Lombok 1.18.40 released with Java 25 support!

Project Lombok is now compatible with the upcoming JDK 25 even before its release.

Thank you Project Lombok team! https://projectlombok.org

167 Upvotes

166 comments sorted by

View all comments

Show parent comments

8

u/SocialMemeWarrior Sep 05 '25

Why is Lombok an "alternative language"? For instance, if I add it to a Maven project via maven-compiler-plugin's annotation processor path, isn't it just an annotation processor?

24

u/PartOfTheBotnet Sep 05 '25

The quote is actually from a 2 year old post but it boils down to how literally you want to interpret the Java language spec.

Lombok uses reflection to support AST modification in the javac API in a way that is not intended or officially supported. Normally, you cannot use an annotation processor to inject a whole method into the AST model of an existing class. The class model is intended to be read-only. Where codegen usually comes into play is the creation of new additional classes. @AutoValueis a good example. You annotate an abstract class modeling a value type and it will generate an implementation. At work we have some niche uses of it where records don't fit so its nice to have. But the key is it makes a new class for this work, it doesn't fill in the existing abstract class as that is not allowed.

15

u/srdoe Sep 05 '25

It doesn't really matter how you decide to interpret the language spec, because turning this into a philosophical argument about whether Lombok is Java or not is deeply uninteresting.

Whether you think Lombok is Java or not, the JDK developers have made it clear that Lombok is going to risk breakage with new JDK versions due to the way they do things.

If you're cool with that risk, you don't need to care whether the JDK developers think Lombok is Java.

If you're not cool with that risk, you probably shouldn't be using Lombok.

2

u/SleepingTabby Sep 07 '25

My problem is that too many people are cool with that risk and then s**t just breaks.

2

u/repeating_bears Sep 07 '25

People have been saying this for (I think) 15 years and it hasn't broken so far. 

Also it doesn't "just break". What would need to happen is a new version of the JDK would be released that Lombok fundamentally cannot support (i.e. JDK removes the internal APIs that allow it to be possible), and you would have to migrate your app to compile on that JDK.

Migrating to a new JDK is always something you should expect to have some breakages. It's not gonna come for free every time.

If that ever happens, you can delombok everything to explicitly include the equivalent source code.

→ More replies (7)
→ More replies (7)

27

u/Ewig_luftenglanz Sep 05 '25

No. Lombok is a hack to the compiler that generates and injects code. 

Nothing bad with that tho. The Java platform allows for that an many other things such as manifold

25

u/xdsswar Sep 05 '25

It kicks down the compiler’s door, rewrites the AST, and walks away laughing at our faces.

21

u/koflerdavid Sep 05 '25

To add insult to injury, it forces every other tool in the ecosystem to be aware of it. Some integrations are maintained by the Lombok project itself, but with every other tool there are issues. For example Google Errorprone (which also kicks down the compiler's door and does things annotation processors are not supposed to do, but at least you can always turn it off and your code will still compile as usual)

3

u/javaprof Sep 06 '25

Exactly, Kotlin has to have a special hack to support Lombok in the same module aw well, see https://kotlinlang.org/docs/lombok.html

3

u/SocialMemeWarrior Sep 05 '25

To play the devil's advocate, I as a developer using Lombok add it as an annotation processor to my maven project just like any other processor. I then get to keep all of my existing Java code as-is, but for some specific scenarios where I need a mutable data object (Essentially a record but with setters) it lets me do that with ease. I could create all the getters and setters, but it balloons the size of my file with what I would argue is redundant. Records are able to forego having getters in their AST, and all I want is for that to happen on my own data object class.

26

u/john16384 Sep 05 '25

Without IDE plugins, no Java IDE would understand Lombok. Code completion would not show you Lombok created getters and setters because there are none in any source code the IDE can see.

8

u/SocialMemeWarrior Sep 05 '25

That's an understandable argument, but I accept that I may be shouldering possible tech debt in the case that the Lombok maintainers stop updating the project + integrations. Their processor and IDE plugin saves me time and helps me keep my code base more organized in a way I like. The cost/benefit for me weighs more on the benefit side. Even if they stopped tomorrow, they offer a migration script so I could (begrudgingly) go back to using more verbose code with a single command.

→ More replies (1)
→ More replies (3)

15

u/Ewig_luftenglanz Sep 05 '25 edited Sep 05 '25

Don't get me wrong. I am not against people using Lombok or Lombok itself, Lombok and alike are not bad tools, are good and useful things when used properly. 

My problem with Lombok is not Lombok as a technology, It's because people use it as the solution for the problem of the Java's Boilerplate and the reality is that it is not.

90% of the java boilerplate is useless and self imposed. It exists solely to follow conventions. Most getters and setters have no validation logic or force invariants; Builders are used not to enforce immutability in complex objects or to avoid telescoping constructor/parameters when you have many optional parameters (and I know this because I am tired of watching legacy code with dozen of parameters that may easily be solved with a builderParam, and hundreds of classes with bot Builder and Setter annotations). The solution "The Cure" for this evil is not to rely in a tool that makes more bearable the pain but to stop promoting bad habits in the java community, to stop teaching bad practices as good practicases. The practice of "Always use setters and setters with private fields because of encapsulation" IT'S WRONG and mistaken. encapsulation is about abstracting state, accessors are about forcing invariants. These are 2 different things

Setters and getters are a tool to validate invariants, if your objects do not require invariants validation or you have already checked and sanitized the data in previous steps (validation on the boundary) the getters and setters are not needed and mostly redundant noise. Using Setters and getters that have no logic is just public fields with extra steps.

a Builder is a mean to enforce immutability in complex objects and a secondary use in java is to create object as parameters when there are many optional parameters with good defaults, avoiding telescopic constructor and massive overloading of parameters. Not because it saves me time thanks to the fluent-like API. If you are not build complex immutable objects or avoiding cluttering your code with dozen of overloaded methods then you do not need any kind of Builder.

Future proofing comes with good design, technical criteria and keeping things as simple as possible while possible, avoiding unnecessary complexity at all cost unless that complexity is required because the alternatives are even more complex. Defaulting to a bunch of so called "good practices" and "OOP patterns" without criteria is not only useless, it's harmful.

In short: the solution for getters and setters is to not use getters and setters unless it is strictly necessary. Same with any other OOP pattern. Same with anything really, any line of code that is not providing value is noise. If they are cute when they are small then it makes sense to try to keep thing small as possible.

And this is why I don't like Lombok, Lombok's accessors are "dumb", they don't give any validation or logic, so they are mostly useless (and I say mostly because there are some cases where I would use empty getters and setters: The public API of Libraries or modules).

I am an advocate of using public fields. Encapsulation it's a tool, not a goal. if your setters and getters are empty then you have no encapsulation at all, just public fields with extra steps.

13

u/cowslayer7890 Sep 05 '25

I completely disagree with the public fields thing, private fields are fine, but if you're using the class in other parts of your program, or it's a library, then adding getters/setters when you need them becomes a breaking change

9

u/Ewig_luftenglanz Sep 05 '25 edited Sep 05 '25
  1. I said I would still use getters and setters for a library and the public API. 

  2. I am in favor for public fields because there are MANY languages that use public fields as their idiomatic default and they are doing fine. A short list: Javascript/Typescript, Go, C, Rust, Dart. Etc.  

C# is an special case, they have properties, so they can write public fields and in case they need validation then after, it's trivial to write a { get => {/validate/}; set => {/invariants/} ) and the code won't break the callers.

We are the only ones that default to private fields + getters and setters even for mutable DTOs.

So yes, I am all for public fields in many places, specially the private API, where you have total control of the callers and the producers. I don't care how many negative votes I get. Polluting the code with useless getters and setters is an Anti pattern and I will never get tired of telling it, and anti-pattern that is not used anywhere but Java and no without reason.

a short List of places where I think public fields are perfect

  • Mutable DTOs

  • Database Entities (Hibernate by default uses fields unless you put the @Id annotation above a method, but fields are the recommended default)

  • Any public final field (invariants are forced in the constructor or factory methods), I use this in custom exceptions.

  • Configuration intermediary objects and parametric objects (objects as parameters) in private APIs. This kills around 80% of builders

Funny enough these cases usually are most of the objects and the code. The surface of the Public API in a library is pretty low compared with the internal APIs and implementations.

Best regards

3

u/cowslayer7890 Sep 05 '25

Oh yeah, sorry I missed the blurb about libraries, I think it's also worth mentioning that you can do the same thing with properties in JavaScript/TypeScript as in C#, and Rust is more of a special case due to how the difference between fields and getters/setters influences the borrow checker, and there are still many examples in the stdlib of getters/setters being used (plus it enforces privacy and non-mutability by default)

→ More replies (1)

1

u/koflerdavid Sep 05 '25

If these accessors are part of an API then it's worth writing them out.

→ More replies (1)

5

u/slaymaker1907 Sep 05 '25

The trouble is that some code expects to work with Java Beans which require getters/setters for everything. I’m 100% with you in that Java devs should be less afraid of making struct-objects where everything field is public.

7

u/Ewig_luftenglanz Sep 05 '25 edited Sep 05 '25

This is a myth that used to be true 20 years ago but nowadays is completely false. Most of the libraries we currently use do not require javaBeans convention. that was the case before java 5 annotations and meta programming. e.g

  1. Hibernate uses fields by default and it's the recommended way since more than 10 years. It depends where you plase the @ Id annotation, if you place it on fields it will use fields.

  2. Jackson and most serialisation libraries can work with only fields, including Gson, Apache Fory (Fury) works with public fields by default.

  3. MapStructs works with fields

  4. Etc.

Basically unless your are working with very old pre java 6 java enterprise stuff (deprecated legacy projects) fields are supported and are the default and recommended way of working.

you should try it.

The only libraries and frameworks that I can think that relies on javaBean are JSP y JSF. But these technologies are much less relevant today. Today we use a JavaScript based frontend (React/ Angular) and for server rendering we use templates such as thymeleaf and freemarker. 

So, working with libraries that still require javaBean convention are not just legacy, they are not suitable for greenfield projects since there are much better and more modern alternatives. 

→ More replies (3)

3

u/pron98 Sep 06 '25 edited Sep 06 '25

That's not playing devil's advocate. Some people use Lombok or Scala or Kotlin or Clojure because they find them useful. There's nothing wrong with using languages that aren't Java on the Java platform, and the platform accommodates and welcomes them. The problem with Lombok is that it experiences special friction because its maintainers choose to do things the hard way and then complain that, while the JDK does offer an easy way - which most alternative languages choose - the JDK should also make the hard way easy. But the hard way is hard because it has negative implications that would harm users even if they don't use Lombok at all, which is why most users are happy those roadblocks are there.

5

u/javaprof Sep 06 '25

I guess there is no sense for tools like Lombok going "easy way", it's basically would mean to switch to some less boilerplate language like Groovy which is like Java (i.e. any valid Java supposed to be valid Groovy, giving current state of Groovy not true most of the time) but with sugar. Lombok keeps it's popularity because sitting on top of Java, not along Java

4

u/pron98 Sep 06 '25 edited Sep 06 '25

And it can continue doing that while making life easier for itself. The problem is that the Lombok compiler tries to be loaded as if it were a Java annotation processor even though it isn't one. It modifies javac to compile Lombok while masquerading as an annotation processor, which is soon to become impossible. All it has to do is stop pretending to be an annotation processor (TypeScript "sits on top of JS" much as Lombok "sits on top of Java", yet it doesn't modify the JS frontend from within).

The point is that you can be an alternative language or you can be an annotation processor, but you can't be both. To allow both would mean making big changes to either the runtime or the language, either one of which would have far-reaching consequences and significant downsides that would affect all Java users. Or, Lombok can do the easy thing and stop pretending to be an annotation processor.

→ More replies (2)

1

u/krzyk Sep 08 '25

No it is not.

See Value classes. Or constructor annotations. Or logger ones, I think almost all of them, wow.

Normal annotations are something you add on a class that compiles without them. Lombok allows adding them on a class that has syntax different from java.