diff --git a/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java b/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java index 63bec2b9e..654217ab8 100755 --- a/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java @@ -199,7 +199,8 @@ public class MappingResolverImpl implements MappingResolver { } // then direct assignable - if ( sourceType.isAssignableTo( targetType ) || isPropertyMappable( sourceType, targetType ) ) { + if ( sourceType.isAssignableTo( targetType ) || + isAssignableThroughCollectionCopyConstructor( sourceType, targetType ) ) { Assignment simpleAssignment = AssignmentFactory.createDirect( sourceReference ); return simpleAssignment; } @@ -503,32 +504,22 @@ public class MappingResolverImpl implements MappingResolver { } /** - * Whether the specified property can be mapped from source to target or not. A mapping if possible if one of - * the following conditions is true: - * - * - * @return {@code true} if the specified property can be mapped, {@code false} otherwise. + * Whether the given source and target type are both a collection type or both a map type and the source value + * can be propagated via a copy constructor. */ - private boolean isPropertyMappable(Type sourceType, Type targetType) { - boolean collectionOrMapTargetTypeHasCompatibleConstructor = false; + private boolean isAssignableThroughCollectionCopyConstructor(Type sourceType, Type targetType) { + boolean bothCollectionOrMap = false; - if ( sourceType.isCollectionType() || targetType.isMapType() ) { - collectionOrMapTargetTypeHasCompatibleConstructor = hasCompatibleCopyConstructor( - sourceType, - targetType.getImplementationType() != null - ? targetType.getImplementationType() : targetType - ); + if ( ( sourceType.isCollectionType() && targetType.isCollectionType() ) || + ( sourceType.isMapType() && targetType.isMapType() ) ) { + bothCollectionOrMap = true; } - if ( ( ( targetType.isCollectionType() || targetType.isMapType() ) - && collectionOrMapTargetTypeHasCompatibleConstructor ) ) { - return true; + if ( bothCollectionOrMap ) { + return hasCompatibleCopyConstructor( + sourceType, + targetType.getImplementationType() != null ? targetType.getImplementationType() : targetType + ); } return false; diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionMappingTest.java b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionMappingTest.java index feb6e8f5b..1b711e409 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionMappingTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionMappingTest.java @@ -65,7 +65,9 @@ public class ErroneousCollectionMappingTest { @Diagnostic(type = ErroneousCollectionToPrimitivePropertyMapper.class, kind = Kind.ERROR, line = 26, - messageRegExp = "Can't map property \"java.util.List strings\" to \"int strings\". Consider to declare/implement a mapping method: \"int map\\(java.util.List value\\)\"") + messageRegExp = "Can't map property \"java.util.List strings\" to \"int strings\". " + + "Consider to declare/implement a mapping method: \"int map\\(java.util.List" + + " value\\)\"") } ) public void shouldFailToGenerateImplementationBetweenCollectionAndPrimitive() {