From a69627de663b37adee46fdfa918fabe3f48a5c89 Mon Sep 17 00:00:00 2001 From: sjaakd Date: Sun, 25 Dec 2016 14:01:14 +0100 Subject: [PATCH] #1013 Inherit(Reverse)Configuration must only consider abstract methods --- .../ap/internal/model/source/SourceMethod.java | 16 +++++++++++++--- .../CarMapperWithExplicitInheritance.java | 15 ++++++++++----- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/SourceMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/SourceMethod.java index 804180b80..09ae561e4 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/SourceMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/SourceMethod.java @@ -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; diff --git a/processor/src/test/java/org/mapstruct/ap/test/inheritfromconfig/CarMapperWithExplicitInheritance.java b/processor/src/test/java/org/mapstruct/ap/test/inheritfromconfig/CarMapperWithExplicitInheritance.java index c7df3a98e..365b2db57 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/inheritfromconfig/CarMapperWithExplicitInheritance.java +++ b/processor/src/test/java/org/mapstruct/ap/test/inheritfromconfig/CarMapperWithExplicitInheritance.java @@ -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) { } }