简体   繁体   中英

Java Mapstruct generic enum converter

I have two different enum classes and one of them use them as a value like

public enum Type{

    TEST1{
        @Override
        public Type convertToINT() {
            return Type.INTEGRATION1;
        }
    },
    TEST2{
        @Override
        public Type convertToINT() {
            return Type.INTEGRATION2;
        }
    },
    TEST3{
        @Override
        public TypeIntegration convertToINT(){
            return Type.INTEGRATION3;
        }
    };

   public abstract TypeIntegration convertToINT();
}


public enum TypeIntegration {

    INTEGRATION1,
    INTEGRATION2,
    INTEGRATION3
}

This enums uses in different classes for example ;

@Getter
@Setter
public class typeSaveReqDto{
 private blabla;
 private blabla;
 private Type type;
}


@Getter
@Setter
public class typeIntegrationObject{
 private blabla;
 private blabla
 private TypeIntegration type;
}

I want to use mapstructs and convert via auto generated classes, but mapstruct throws me exception like "The following constants from the property "Type type" enum have no corresponding constant in the "TypeIntegration type" enum and must be be mapped via adding additional mappings: TEST1, TEST2, TEST3"

I want to create EnumMapper classes for converting enums, How can i write generic classes for this with mapStructs java ?

Edit :

I generated EnumMapper

@Mapper
public class EnumMapper {

    EnumMapper INSTANCE = Mappers.getMapper(EnumMapper.class);

    @Named("enumToIntEnum")
    public static <T extends EnumConverter<INT>,INT> INT convertToINT(T enums, @TargetType INT enumClass){
        INT convertObj = ((T)enums).convertToINT();
        return convertObj;
    }
}

And Mapper interfaces like below

@Mapper(componentModel = "spring",uses = EnumMapper.class)
public interface TypeReqMapper {

    
        @Mapping(source = "type" , target = "type",qualifiedByName = "enumToIntEnum")
    public TypeIntegrationObject typeSaveReqDtoTotypeIntegrationObject(TypeSaveReqDto typeSaveReqDto);

}

But I got a fail like 'Can't map property "Type type" to "TypeIntegration type". Consider to declare/implement a mapping method: "TypeIntegration map(Type value)".'

Type T isn't know and doesn't contain a convertToINT method as it can be anything. So keep it simple and just write a dedicated mapper instead of trying to build one that does everything.

@Mapper(componentModel="spring")
public class TypeToTypeIntegrationMapper {

  @Mapping
  public TypeIntegration map(Type from) {
    return from.convertToINT();
  }
}

Don't make it more complex.

You could even ditch the mapper and write an expression instead.

@Mapper(componentModel = "spring",uses = EnumMapper.class)
public interface TypeReqMapper {

    
        @Mapping(source = "type" , target = "type", expression = "java(type.convertToINT())")
    public TypeIntegrationObject typeSaveReqDtoTotypeIntegrationObject(TypeSaveReqDto typeSaveReqDto);

}

MapStruct will now use the expression to generate the mapping code instead of you having to write a mapper yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM