Search code examples
javagenericsmapstruct

Mapstruct: mapping of Generics


@Mapper
public interface MyMapper<X extends Base_1, Y extends Base_2>{
    public X mapBase_2ToBase_1(Y obj);
}

I want to map an object of generic type Y to another object of generic type X. Is anything like that possible with mapstruct? Or do I have to write custom mappers for Generic mapping? When I compile the code above, I get compilation errors.

Caused by: java.lang.NullPointerException
    at org.mapstruct.ap.util.SpecificCompilerWorkarounds.replaceTypeElementIfNecessary(SpecificCompilerWorkarounds.java:90)
    at org.mapstruct.ap.util.Executables.getAllEnclosedExecutableElements(Executables.java:189)
    at org.mapstruct.ap.model.common.Type.getAllExecutables(Type.java:395)
    at org.mapstruct.ap.model.common.Type.getSetters(Type.java:464)
    at org.mapstruct.ap.model.common.Type.getPropertyWriteAccessors(Type.java:353)
    at org.mapstruct.ap.model.BeanMappingMethod$Builder.souceMethod(BeanMappingMethod.java:93)
    at org.mapstruct.ap.processor.MapperCreationProcessor.getMappingMethods(MapperCreationProcessor.java:345)
    at org.mapstruct.ap.processor.MapperCreationProcessor.getMapper(MapperCreationProcessor.java:143)
    at org.mapstruct.ap.processor.MapperCreationProcessor.process(MapperCreationProcessor.java:114)
    at org.mapstruct.ap.processor.MapperCreationProcessor.process(MapperCreationProcessor.java:73)
    at org.mapstruct.ap.MappingProcessor.process(MappingProcessor.java:193)
    at org.mapstruct.ap.MappingProcessor.processMapperTypeElement(MappingProcessor.java:173)
    at org.mapstruct.ap.MappingProcessor.process(MappingProcessor.java:154)

Solution

  • I've filed an issue in our tracker to address the NPE. I am doubtful though whether we can create a proper implementation for such mapper contract. We could not tell which type should be instantiated as the result type of the method.

    What's your rationale for using the type parameters in the shown ways? What implementation would you expect to be generated?