Search code examples
javacompiler-errorslombok

Lombok not compiling an abstract generic superclass


I have this class:

@SuperBuilder
public abstract class EventFinder<EventType> {
    private final String accountId;

public abstract List<EventType> find();

    public static abstract class EventFinderBuilder<C extends EventFinder<?>, B extends EventFinderBuilder<C, B>> {
        public B accountId(String accountId) {
            log.trace("Account ID: %1s".formatted(accountId));
            this.accountId = accountId;
            return self();
        }
    }
}

Been trying to make @SuperBuilder work with it but to no avail, I'm overriding some of the builder methods to include logs, which works great, Compiling outputs:

error: wrong number of type arguments; required 2
@SuperBuilder
^

I can do it just fine by removing the abstract method from the class and simply implementing it in the subclasses, but I would like to declare it here as this is the parent.

Tried to play with the @SuperBuilder parameters, nothing helps, tried to remove the abstract method, which worked, but did not solve the issue.


Solution

  • When your class has generic type parameters, The builders that lombok generates will also have those type parameters. The builders are static classes, and so the EventType type parameter of EventFinder is not in scope in the builder classes.

    If you want to write your own implementation for a part of the code that Lombok generates, the signature has to match exactly. You should declare the EventType type parameter too.

    public static abstract class EventFinderBuilder<
        EventType,
        C extends EventFinder<EventType>, 
        B extends EventFinderBuilder<EventType, C, B>
    > { ... }
    

    But as rzwitserloot said in their answer. This kind of defeats the purpose of Lombok. If you are adding a log for every field in your class, you might as well handwrite the entire builder yourself.