Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lombok @Data complains "lombok needs a default constructor in the base class", when abstract class having final field

@Data
public abstract class B {
   private final String str;
}

@Data
public class A extends B{
   private final String s;
}

Data on class A complains in intellij, but the codes can get compiled successfully through command line, not sure what to do

like image 438
Richard Avatar asked Sep 04 '19 21:09

Richard


2 Answers

One problem is that @Data is meant for mutable data and there's nothing mutable in your classes. So using @Data is simply wrong... and whether it compiles or not doesn't really matter.

If you want mutable data, then remove the final field. For immutable data, make all fields final and use @Value. Sometimes, partially mutable data is needed, but I try hard to avoid it as it's confusing (some fields can be set, some can't) and they provide disadvantages of both.


The other problem is that Lombok can't access class hierarchies. With B having a final field, you need it to be initialized in the constructor, which means that A's constructor has to call a non-default constructor. This isn't possible with Lombok. There's @Superbuilder in Lombok, which is about the only feature of Lombok dealing well with class hierarchies.

like image 160
maaartinus Avatar answered Sep 17 '22 13:09

maaartinus


This does not event compile. In Intellij, when you are not sure what is the problem with lombok code, you can open class in which you are unsure, go on Refactor -> Delombok -> All lombok annotations and you will see what lombok actually create for you.

like image 24
Spasoje Petronijević Avatar answered Sep 16 '22 13:09

Spasoje Petronijević