Search code examples
javamapstruct

No method found annotated with @Named#value on upper class


i'm using mapstruct with bellow dependency:

<dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>1.3.0.Final</version>
        </dependency>

        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.3.0.Final</version>
            <scope>provided</scope>
        </dependency>

Then i have an interface that contain basic @Named method:

public interface CommonConverter {
    @Named("formatDateString")
    default String formatDateString(Date date){
        if(date == null){
            return null;
        }
        return DateUtils.formatDateString(date);
    }
    @Named("formatStringDate")
    default Date formatStringDate(String dateStr){
        if(StringUtils.isEmpty(dateStr)){
            return null;
        }
        return DateUtils.formatStringDate(dateStr);
    }

    @Named("convertTimestampToString")
    default String convertTimestampToString(Long date) {
        if (date == null || date.equals(0L)){
            return StringUtils.EMPTY;
        }
        return DateUtils.convertTimeToDateStr(date, 0);
    }
}

Now i have another mapstruct converter which extend CommonConverter

@Mapper(componentModel = "spring")
public interface EmployeeEventConverter extends CommonConverter{
    @Mappings({
            @Mapping(source = "claimDate", target = "claimDate", qualifiedByName = "formatDateString"),
            @Mapping(source = "eventStartDate", target = "eventStartDate", qualifiedByName = "formatDateString"),
            //@Mapping(source = "target", target = "targetAsString", qualifiedByName = "formatTargetString"),
            @Mapping(source = "target", target = "targets", qualifiedByName = "convertTarget")
    })
    EmployeeEventClaimDTO toClaimDTO(EmployeeEventClaimDO entity);
    List<EmployeeEventClaimDTO> toClaimDTOs(List<EmployeeEventClaimDO> entities);
}

This class works ok until i upgrade to 1.4.1.Final , i got this exception when compile the project:

Qualifier error. No method found annotated with @Named#value: [ formatDateString ].

To overcome this, i need to copy all @Named to class EmployeeEventConverter (and many other similar classes) which increase LOC and duplicated code.
Anybody know why it happen and how to solve it?
Thanks


Solution

  • Alternative to extends

    instead of extends you can try the uses property of the Mapper annotation.

    @Mapper(componentModel = "spring", uses = CommonConverter.class )
    public interface EmployeeEventConverter
    

    Reporting issue at MapStruct

    This however does feel like an issue with Mapstruct. Can you check if this problem also exists when using the 1.4.2.Final version or the 1.5.0.RC1 version? If so then please create an issue at https://github.com/mapstruct/mapstruct/issues.