mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#1013 Inherit(Reverse)Configuration must only consider abstract methods
This commit is contained in:
parent
283fd8ebda
commit
a69627de66
@ -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;
|
||||
|
@ -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) { }
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user