Immutables, Kotlin, Spring, Jackson, and Java 8 are the most popular alternatives and competitors to Lombok.
@Data is a convenient shortcut annotation that bundles the features of @ToString , @EqualsAndHashCode , @Getter / @Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, ...
The downside is it clutters your source with boilerplate code that take the focus off the important stuff. After 5 months, we're still using Lombok, but I have some other annoyances. The lack of a declared getter & setter can get annoying at times when you are trying to familiarize yourself with new code.
Omitting Getter or Setter Using AccessLevel. To override the access level, annotate the field or class with an explicit @Setter or @Getter annotation.
I'm afraid, there's no alternative unless you want to switch to something like Scala or are happy with a smaller set of features like those provided by AutoValue.
While AutoValue
is probably the best you can get with pure Java, it offers
but it misses
While I strongly agree that immutability is a virtue, it sometimes isn't applicable. Anyway, Lombok tries hard to support immutability, it even integrates with Guava's immutable collections, and you can write
@Builder @Getter public final class Sentence {
private final boolean truthValue;
@Singular private final ImmutableList<String> words;
}
and use it like
Sentence s = Sentence.builder().truthValue(true)
.word("Lombok").word("is").word("cool").build();
assertEquals(3, s.getWords().size());
Note: I'm not the author, so I can say it's cool.
For immutables, @Wither
and toBuilder
are pretty cool. The former allows you to create a copy differing by a single field and the latter gives you a builder starting with the current values and suitable for changing multiple fields. The following two lines are equivalent:
o.withA(1).withB(2)
o.toBuilder().a(1).b(2).build()
Both Lombok and AutoValue
use some magic. The magic of the latter is the standard annotation processing, so it's pretty robust. It has some disadvantages as listed on page 27. I'd add the fact that some AutoValue_foo gets generated which I didn't order.
Lombok uses some black magic and thus is much more fragile, but it offers more and works pretty well.
"Better" is fairly contentious here, but one alternative is AutoValue, available here. This presentation explains some of the differences between AutoValue and e.g. Lombok.
In particular, AutoValue emphasizes immutability (a good thing, generally!) and a minimum of extralinguistic magic (very different from Lombok).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With