Can you use Lombok toBuilder on an Abstract Class?

Advocate for better developer's productivity and experience
Documenting my learnings and sharing it
Search for a command to run...

Advocate for better developer's productivity and experience
Documenting my learnings and sharing it
No comments yet. Be the first to comment.
Background Recently, I redesigned my team's pipeline running on a multi-module Maven monorepo using GitLab CI. It wasn't that the previous setup was broken, but my team faced a few persistent issues t
Background At work, there’s some test written where I wanted to query the database (repository) after mockMvc request has been made to verify the result. This is a very simple illustration of what it looks like. @SpringBootTest @AutoConfigureMockMvc ...

It's been a long time coming, but I finally came around to set up MongoDB with replicas for local development. TLDR; clone the repository, run docker compose up -d, and you are good to go! If you want to know more, continue to read below. Context Run...

Context I recently came across TaskFile and wanted to use it to automate some of my workflows, which underlying invokes PowerShell commands. However, I faced several errors while doing so, and this post is to reflect on it, and hopefully help myself ...

Problem I was always out of disk space in my C drive, and even though I clear my stuff often, I notice that my space is never reclaimed, instead, it kept going down. I used WizTree to view which file is taking up my disk space, and I noticed this No...

Yes, you can, and you just need an additional line of code in the Abstract Class. Let's take a look at how we can do so.
If you have an abstract class with various subclasses, and you need to define some values through builder method, you won't be able to do so by default because Lombok does not know if all the subclasses are annotated with toBuilder but we can force all subclasses to implement toBuilder method.
To make it simple, we only have an abstract class with 2 subclasses, and a couple of fields.
@SuperBuilder(toBuilder = true)
public abstract class AddressDO {
private String id;
private String street;
private String postalCode;
private String unit;
}
@SuperBuilder(toBuilder = true)
public class HomeAddressDO extends AddressDO {
private boolean isRental;
}
@SuperBuilder(toBuilder = true)
public class OfficeAddressDO extends AddressDO {
private String building;
}
Now, imagine we have a method that takes in AddressDO as the argument
public AddressDO createAddress(AddressDO addressDO) {
// 1
return addressDO;
}
toBuilder on addressDO to define some fieldsTake a look at the GIF below

From the GIF, we can see that even though we defined @SuperBuilder(toBuilder = true), we are unable to call toBuilder method. As mentioned above, this is done on purpose by Lombok, since it cannot ensure all subclasses are annotated with toBuilder.
So how can we resolve this?
public abstract class AddressDO {
public abstract AddressDOBuilder<?, ?> toBuilder(); // added this
// omitted
}
We create an abstract toBuilder method which will ensure that all subclasses also implement this method.
This is what happens if the subclass did not implement toBuilder method
The type HomeAddressDO must implement the inherited abstract method AddressDO.toBuilder()

And now, if we try again

The limitation is that it only allows for modification to the fields on the abstract class unless we cast it first. However, if we were to cast it, then there is no (less) need for the abstract method in the first place.

We have seen how we can overcome the limitation of calling toBuilder in an abstract class, and the limitation of doing so. I wouldn't call this a hack, but do use it only when you need it.
As usual, the full source code is available on GitHub