Java Immutables Polymorphism - how to make generated class extend another class?

57 Views Asked by At

I'm using the immutables library to create immutable objects from interfaces. However, I can't find a way to make generated classes extend one another in order to make them polymorphic.

Consider this code:

@Value.Immutable
@ImmutableStyle
@JsonSerialize
public interface AbstractAdvert {

    String getAdvertName();

    @Nullable
    String getAdvertRef();

    AdvertPrice getPrice();

    AdvertProvider getProvider();

    @Value.Default
    default Boolean getBillsIncluded() {
        return false;
    }

    @Value.Immutable
    @ImmutableStyle
    @JsonSerialize
    interface AbstractStudioAdvert extends AbstractAdvert {

    }
}

This will generate two classes: Advert and StudioAdvert.

I'd like to have methods elsewhere that take List<Advert> as a parameter but it also accepts List<StudioAdvert> hence I need the polymorphism here.

TLDR: The above code doesn't make generated StudioAdvert extend Advert. I'd like them to have that relationship though.

Any suggestions appreciated.

EDIT 1

In my use case, let's say I have a List<StudioAdvert> and a List<RoomAdvert> that I would like to pass to the same function for processing. The following method does not work at the moment if I use List<Advert> or List<AbstractAdvert> as method's parameter.

public static ProcessedResult processResult(List<AbstractAdvert> adverts) {
    ...
}

The issue is as below. enter image description here

0

There are 0 best solutions below