Depending on a generic SuperBuilder

832 views
Skip to first unread message

Marc Guilera

unread,
Oct 22, 2020, 4:52:10 PM10/22/20
to Project Lombok
I have a base entity which has a couple of properties and the concrete implementation with a couple of extra ones. On top of that I need the base class to access "self" of the child which I do through generics. Here is a simplified example of what I am trying:

@Data
@SuperBuilder(toBuilder = true)
@ToString(callSuper = true)
public abstract class Entity<ENTITY extends Entity<ENTITY, BUILDER>, BUILDER extends Entity.EntityBuilder<ENTITY, BUILDER, ENTITY, BUILDER>> implements Mutable<ENTITY> {
    //...
    public abstract ENTITY mutate(Consumer<Context<ENTITY, BUILDER>> context) {
        var builder = createBuilder();
        context.accept(new Context<>(self(), builder));
        return builder.build();
    }
    protected abstract ENTITY self();
    protected abstract BUILDER createBuilder();
}

@Data
@SuperBuilder(toBuilder = true)
public class SubEntity extends Entity<SubEntity, Entity.EntityBuilder<?, ?>> {
    //...
    @Override
    protected SubEntity self() {
        return this;
    }
    @Override
    protected SubEntity.SubEntityBuilder<?, ?> createBuilder() {
        return toBuilder();
    }
}

The issue is in the Entity.EntityBuilder<?, ?> part where it complains that EntityBuilder is not accessible in current context. How can I achieve my goal? 

Thanks in advance.

Floris Kraak

unread,
Oct 23, 2020, 4:09:54 AM10/23/20
to project...@googlegroups.com

Superbuilder and generics is a rabbit hole. That annotation is using some very fancy tricks to work but combining it with generics is liable to blow up.

I would suggest making the base entity an '@Embeddable' annotated object instead, and embedding it I the concrete class with '@Embedded'. That way you can have the same set of columns but without the pain that is inheritance and generics 


--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/f7695b0b-eec2-4838-ab45-1a9897bcfd9bn%40googlegroups.com.

Floris Kraak

unread,
Oct 23, 2020, 4:11:04 AM10/23/20
to project...@googlegroups.com
Another suggestion: Try running your code through delombok, that way you will see exactly what is being generated here.

Marc Guilera

unread,
Oct 23, 2020, 11:02:03 PM10/23/20
to Project Lombok
I called it an "Entity" but it's just a model, I am not using JPA in the project so @Embedable is not feasible. Delegate is also not possible since it only goes one level up. 

Floris Kraak

unread,
Oct 24, 2020, 3:56:04 AM10/24/20
to project...@googlegroups.com

The person to ask about this stuff is Jan Rieke, it's his feature.

This example is definitely stretching superbuilder to the limits though, combining generics with customized builders and inheritance.


Reply all
Reply to author
Forward
0 new messages