jeudi 9 septembre 2021

Get actual interface parameter for class

I have interface like this:

public interface Converter<R extends Data> {
    CustomData convert(R source);
}

and few implementations. For example:

public class ShopConverter implements Converter<ShopData> {
    public CustomData convert(ShopData source) {}
}

Of course ShopData extends Data.

In the handler I receive my converters by DI and store their in List<Converter<? extends Data>> converters.

I want to fetch actual Data sub-class implementation. For this case it should be ShopData but I've receive only Data by code:

Class<? extends Converter> converterClass = converters.get(0).getClass();
Type interfaceType = converterClass.getGenericInterfaces()[0];
ParameterizedType parameterizedType = (ParameterizedType)interfaceType
Type actualTypeArgument = parameterizedType.getActualTypeArguments()[0]; //will be Data but I want to receive ShopData

Is it possible to fetch actual sub-class Type (or better Class) instead of super-class?





Aucun commentaire:

Enregistrer un commentaire