#1013 Inherit(Reverse)Configuration must only consider abstract methods

This commit is contained in:
sjaakd 2016-12-25 14:01:14 +01:00
parent 283fd8ebda
commit a69627de66
2 changed files with 23 additions and 8 deletions

View File

@ -185,7 +185,7 @@ public class SourceMethod implements Method {
public SourceMethod build() {
MappingOptions mappingOptions =
new MappingOptions( mappings, iterableMapping, mapMapping, beanMapping, valueMappings );
new MappingOptions( mappings, iterableMapping, mapMapping, beanMapping, valueMappings );
SourceMethod sourceMethod = new SourceMethod(
declaringMapper,
@ -334,7 +334,8 @@ public class SourceMethod implements Method {
}
public boolean reverses(SourceMethod method) {
return getSourceParameters().size() == 1 && method.getSourceParameters().size() == 1
return isAbstract()
&& getSourceParameters().size() == 1 && method.getSourceParameters().size() == 1
&& equals( first( getSourceParameters() ).getType(), method.getResultType() )
&& equals( getResultType(), first( method.getSourceParameters() ).getType() );
}
@ -346,7 +347,8 @@ public class SourceMethod implements Method {
}
public boolean canInheritFrom(SourceMethod method) {
return isMapMapping() == method.isMapMapping()
return method.isAbstract()
&& isMapMapping() == method.isMapMapping()
&& isIterableMapping() == method.isIterableMapping()
&& isEnumMapping() == method.isEnumMapping()
&& getResultType().isAssignableTo( method.getResultType() )
@ -602,6 +604,14 @@ public class SourceMethod implements Method {
return Executables.isBeforeMappingMethod( getExecutable() );
}
/**
* @return returns true for interface methods (see jls 9.4) lacking a default or static modifier and for abstract
* methods
*/
public boolean isAbstract() {
return executable.getModifiers().contains( Modifier.ABSTRACT );
}
@Override
public boolean isUpdateMethod() {
return getMappingTargetParameter() != null;

View File

@ -23,6 +23,7 @@ import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingInheritanceStrategy;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;
/**
@ -32,17 +33,21 @@ import org.mapstruct.factory.Mappers;
config = AutoInheritedConfig.class,
mappingInheritanceStrategy = MappingInheritanceStrategy.EXPLICIT
)
public interface CarMapperWithExplicitInheritance {
CarMapperWithExplicitInheritance INSTANCE = Mappers.getMapper( CarMapperWithExplicitInheritance.class );
public abstract class CarMapperWithExplicitInheritance {
public static final CarMapperWithExplicitInheritance INSTANCE =
Mappers.getMapper( CarMapperWithExplicitInheritance.class );
@InheritConfiguration(name = "baseDtoToEntity")
@Mapping(target = "color", source = "colour")
CarEntity toCarEntity(CarDto carDto);
public abstract CarEntity toCarEntity(CarDto carDto);
@InheritInverseConfiguration(name = "toCarEntity")
CarDto toCarDto(CarEntity entity);
public abstract CarDto toCarDto(CarEntity entity);
@InheritConfiguration(name = "toCarEntity")
@Mapping(target = "auditTrail", constant = "fixed")
CarEntity toCarEntityWithFixedAuditTrail(CarDto carDto);
public abstract CarEntity toCarEntityWithFixedAuditTrail(CarDto carDto);
// this method should not be considered. See issue #1013
public void toCarEntity(CarDto carDto, @MappingTarget CarEntity carEntity) { }
}