I am really enjoy the feature of project lombok that get rid of boilerplate code. My senior suggest me that lombok seems to use reflection to reduce boilerplate code and I once heard that using of reflection effect performance. My question is are there any issues to use project lombok?
Asked
Active
Viewed 9,843 times
2 Answers
51
Lombok does not use reflection at runtime. It hooks into the compiler internals and adds code to classes at compile-time, which is then compiled normally.

chrylis -cautiouslyoptimistic-
- 75,269
- 21
- 115
- 152
-
7Since this was the first answer, I saw when I searched for projectlombok resources, I like to add one point. You are right. It does not decrease performance at runtime, there is a performance flaw at compiletime. We tested it and see the compile time 2 times slower. For more information, please take a look at this answer: https://stackoverflow.com/questions/15518405/lombok-slowing-down-build-process-in-large-project – Barny Sep 06 '18 at 16:42
-
2@ChristianKaiser I'm not sure that really qualifies as a *flaw*, but yes, having to fork the compiler can really slow down builds. – chrylis -cautiouslyoptimistic- Sep 06 '18 at 16:49
-
@Barny That's no longer an issue, if you read the follow-ups. I did not experience it either. – gaborsch Aug 22 '22 at 08:13
11
@chrylis answer is correct, but be careful with objects graph (@Data
and @ToString
annotation) e.g.
@Data
public class A {
private B b;
}
@Data
public class B {
private A a;
}
Lombok will create toString with infinite recursion inside. You should use
something like @ToString(exclude = {"a"})
inside class B

sibnick
- 3,995
- 20
- 20
-
5Right, but note that this is pretty normal. Java and Guava collections behave the same, and so does `Arrays.toString(Object)`. There's little what lombok could do as it doesn't generate every `toString` implementation. – maaartinus Aug 22 '16 at 15:20
-
But I have not any collections in my sample. Default toString result looks like "my.package.A@35445". So there is no recursion. Moreover @Data lombok annotation may create inefficient implementation of methods"hashCode" and "equals" too. PS I happy with lombok and still recommend use it – sibnick Aug 23 '16 at 04:37
-
OK, you have no collections, but you may have some object with a hand-written `toString`. Depending on its implementation, you need to exclude a field or not. Lombok could handle it by keeping track of all its `toString` invocations in a `ThreadLocal
`, but this would slow down all invocations. It could be a useful feature, but hardly anyone asked for it. +++ Why inefficient `hashCode`/`equals`? Because of them treating arrays the way Java treates like lists? PS: I'm very happy with Lombok (I consider it greater than Java 8 features). – maaartinus Aug 23 '16 at 05:32 -
About `hashCode` for`@Data`. Default implementation of hashCode uses all fields. It may be very time consuming and makes hash code mutable. Of course it is common problem in Java code, but you can't see such problem in lombok code, because problem is hidden from your eyes. You should just remember this. – sibnick Aug 23 '16 at 07:44