From fcf96c36ebc0663ffa3db2b7a92772ae11105737 Mon Sep 17 00:00:00 2001 From: Sjaak Derksen Date: Tue, 19 Mar 2019 09:44:27 +0100 Subject: [PATCH] #1756 better forged method error based on empty target bean properties (#1757) --- .../ap/internal/model/BeanMappingMethod.java | 2 +- .../ap/internal/model/MapMappingMethod.java | 1 - .../mapstruct/ap/internal/util/Message.java | 3 +- .../ErroneousCollectionMappingTest.java | 20 +++++++------ .../forged/CollectionMappingTest.java | 14 ++++++---- .../erroneous/ErroneousStreamMappingTest.java | 28 +++++++++++-------- .../forged/ForgedStreamMappingTest.java | 5 ++-- .../selection/generics/ConversionTest.java | 20 +++++++------ .../ap/test/verbose/CreateMapMapping.java | 1 - 9 files changed, 52 insertions(+), 42 deletions(-) diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java index 4e05ad5d6..502e924aa 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java @@ -749,7 +749,7 @@ public class BeanMappingMethod extends NormalTypeMappingMethod { ForgedMethodHistory history = forgedMethod.getHistory(); ctx.getMessager().printMessage( this.method.getExecutable(), - Message.PROPERTYMAPPING_MAPPING_NOT_FOUND, + Message.PROPERTYMAPPING_FORGED_MAPPING_WITH_HISTORY_NOT_FOUND, history.createSourcePropertyErrorMessage(), history.getTargetType(), history.createTargetPropertyName(), diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java index 961fd9778..72a6da85f 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/MapMappingMethod.java @@ -196,7 +196,6 @@ public class MapMappingMethod extends NormalTypeMappingMethod { .getFactoryMethod( method, method.getResultType(), null, ctx ); } - keyAssignment = new LocalVarWrapper( keyAssignment, method.getThrownTypes(), keyTargetType, false ); valueAssignment = new LocalVarWrapper( valueAssignment, method.getThrownTypes(), valueTargetType, false ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/util/Message.java b/processor/src/main/java/org/mapstruct/ap/internal/util/Message.java index 2706a3379..a064873da 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/util/Message.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/util/Message.java @@ -39,7 +39,8 @@ public enum Message { PROPERTYMAPPING_CREATE_NOTE( "creating property mapping: %s.", Diagnostic.Kind.NOTE ), PROPERTYMAPPING_SELECT_NOTE( "selecting property mapping: %s.", Diagnostic.Kind.NOTE ), PROPERTYMAPPING_MAPPING_NOT_FOUND( "Can't map %s to \"%s %s\". Consider to declare/implement a mapping method: \"%s map(%s value)\"." ), - PROPERTYMAPPING_FORGED_MAPPING_NOT_FOUND( "Can't map %s to %s. Consider to implement a mapping method: \"%s map(%s value)\"." ), + PROPERTYMAPPING_FORGED_MAPPING_WITH_HISTORY_NOT_FOUND( "No target bean properties found: can't map %s to \"%s %s\". Consider to declare/implement a mapping method: \"%s map(%s value)\"." ), + PROPERTYMAPPING_FORGED_MAPPING_NOT_FOUND( "No target bean properties found: can't map %s to %s. Consider to implement a mapping method: \"%s map(%s value)\"." ), PROPERTYMAPPING_DUPLICATE_TARGETS( "Target property \"%s\" must not be mapped more than once." ), PROPERTYMAPPING_EMPTY_TARGET( "Target must not be empty in @Mapping." ), PROPERTYMAPPING_SOURCE_AND_CONSTANT_BOTH_DEFINED( "Source and constant are both defined in @Mapping, either define a source or a constant." ), 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 8a1a138c4..feefb92b4 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 @@ -104,9 +104,9 @@ public class ErroneousCollectionMappingTest { @Diagnostic(type = ErroneousCollectionNoElementMappingFound.class, kind = Kind.ERROR, line = 25, - messageRegExp = "Can't map Collection element \".*WithProperties withProperties\" to \".*NoProperties" + - " noProperties\". Consider to declare/implement a mapping method: \".*NoProperties map\\(" + - ".*WithProperties value\\)") + messageRegExp = "No target bean properties found: can't map Collection element \".*WithProperties " + + "withProperties\" to \".*NoProperties noProperties\". Consider to declare/implement " + + "a mapping method: \".*NoProperties map\\(.*WithProperties value\\)") } ) public void shouldFailOnNoElementMappingFound() { @@ -138,9 +138,10 @@ public class ErroneousCollectionMappingTest { @Diagnostic(type = ErroneousCollectionNoKeyMappingFound.class, kind = Kind.ERROR, line = 25, - messageRegExp = "Can't map Map key \".*WithProperties withProperties\" to \".*NoProperties " + - "noProperties\". Consider to declare/implement a mapping method: \".*NoProperties map\\(" + - ".*WithProperties value\\)") + messageRegExp = "No target bean properties found: can't map Map key \".*WithProperties " + + "withProperties\" to " + + "\".*NoProperties noProperties\". Consider to declare/implement a mapping method: " + + "\".*NoProperties map\\(.*WithProperties value\\)" ) } ) public void shouldFailOnNoKeyMappingFound() { @@ -171,9 +172,10 @@ public class ErroneousCollectionMappingTest { @Diagnostic(type = ErroneousCollectionNoValueMappingFound.class, kind = Kind.ERROR, line = 25, - messageRegExp = "Can't map Map value \".*WithProperties withProperties\" to \".*NoProperties " + - "noProperties\". Consider to declare/implement a mapping method: \".*NoProperties map\\(" + - ".*WithProperties value\\)") + messageRegExp = "No target bean properties found: can't map Map value \".*WithProperties " + + "withProperties\" to " + + "\".*NoProperties noProperties\". Consider to declare/implement a mapping method: " + + "\".*NoProperties map\\(.*WithProperties value\\)" ) } ) public void shouldFailOnNoValueMappingFound() { diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/forged/CollectionMappingTest.java b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/CollectionMappingTest.java index 0c4480b3b..c538318fd 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/collection/forged/CollectionMappingTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/CollectionMappingTest.java @@ -87,8 +87,8 @@ public class CollectionMappingTest { @Diagnostic(type = ErroneousCollectionNonMappableSetMapper.class, kind = Kind.ERROR, line = 17, - messageRegExp = "Can't map Collection element \".* nonMappableSet\" to \".* nonMappableSet\". " - + "Consider to declare/implement a mapping method: .*."), + messageRegExp = "No target bean properties found: can't map Collection element \".* nonMappableSet\" " + + "to \".* nonMappableSet\". Consider to declare/implement a mapping method: .*." ), } ) public void shouldGenerateNonMappleMethodForSetMapping() { @@ -107,13 +107,15 @@ public class CollectionMappingTest { @Diagnostic(type = ErroneousCollectionNonMappableMapMapper.class, kind = Kind.ERROR, line = 17, - messageRegExp = "Can't map Map key \".* nonMappableMap\\{:key\\}\" to \".* nonMappableMap\\{:key\\}\". " - + "Consider to declare/implement a mapping method: .*."), + messageRegExp = "No target bean properties found: can't map Map key \".* nonMappableMap\\{:key\\}\" " + + "to \".* " + + "nonMappableMap\\{:key\\}\". Consider to declare/implement a mapping method: .*." ), @Diagnostic(type = ErroneousCollectionNonMappableMapMapper.class, kind = Kind.ERROR, line = 17, - messageRegExp = "Can't map Map value \".* nonMappableMap\\{:value\\}\" to \".* " + - "nonMappableMap\\{:value\\}\". Consider to declare/implement a mapping method: .*."), + messageRegExp = "No target bean properties found: can't map Map value \".* " + + "nonMappableMap\\{:value\\}\" to \".* nonMappableMap\\{:value\\}\". " + + "Consider to declare/implement a mapping method: .*." ), } ) public void shouldGenerateNonMappleMethodForMapMapping() { diff --git a/processor/src/test/java/org/mapstruct/ap/test/java8stream/erroneous/ErroneousStreamMappingTest.java b/processor/src/test/java/org/mapstruct/ap/test/java8stream/erroneous/ErroneousStreamMappingTest.java index e195b037e..dfa9849f0 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/java8stream/erroneous/ErroneousStreamMappingTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/java8stream/erroneous/ErroneousStreamMappingTest.java @@ -104,9 +104,10 @@ public class ErroneousStreamMappingTest { @Diagnostic(type = ErroneousStreamToStreamNoElementMappingFound.class, kind = Kind.ERROR, line = 24, - messageRegExp = "Can't map Stream element \".*WithProperties withProperties\" to \".*NoProperties " + - "noProperties\". Consider to declare/implement a mapping method: \".*NoProperties map\\(" + - ".*WithProperties value\\)") + messageRegExp = "No target bean properties found: can't map Stream element \".*WithProperties " + + "withProperties\" to \".*NoProperties noProperties\". " + + "Consider to declare/implement a mapping method: \".*NoProperties " + + "map\\(.*WithProperties value\\)" ) } ) public void shouldFailOnNoElementMappingFoundForStreamToStream() { @@ -136,10 +137,11 @@ public class ErroneousStreamMappingTest { @Diagnostic(type = ErroneousListToStreamNoElementMappingFound.class, kind = Kind.ERROR, line = 25, - messageRegExp = - "Can't map Stream element \".*WithProperties withProperties\" to \".*NoProperties noProperties\"." + - " Consider to declare/implement a mapping method: \".*NoProperties map\\(.*WithProperties " + - "value\\)") + messageRegExp = "No target bean properties found: can't map Stream element \".*WithProperties " + + "withProperties\" to \".*NoProperties noProperties\"." + + " Consider to declare/implement a mapping method: \".*NoProperties map\\(" + + ".*WithProperties " + + "value\\)" ) } ) public void shouldFailOnNoElementMappingFoundForListToStream() { @@ -154,8 +156,9 @@ public class ErroneousStreamMappingTest { @Diagnostic(type = ErroneousListToStreamNoElementMappingFoundDisabledAuto.class, kind = Kind.ERROR, line = 20, - messageRegExp = "Can't map stream element \".*AttributedString\" to \".*String \". Consider to " + - "declare/implement a mapping method: \".*String map\\(.*AttributedString value\\)") + messageRegExp = "Can't map stream element \".*AttributedString\" to " + + "\".*String \". Consider to declare/implement a mapping method: \".*String " + + "map\\(.*AttributedString value\\)" ) } ) public void shouldFailOnNoElementMappingFoundForListToStreamWithDisabledAuto() { @@ -169,9 +172,10 @@ public class ErroneousStreamMappingTest { @Diagnostic(type = ErroneousStreamToListNoElementMappingFound.class, kind = Kind.ERROR, line = 25, - messageRegExp = - "Can't map Stream element \".*WithProperties withProperties\" to .*NoProperties noProperties\"." + - " Consider to declare/implement a mapping method: \".*NoProperties map(.*WithProperties value)") + messageRegExp = "No target bean properties found: can't map Stream element \".*WithProperties " + + "withProperties\" to .*NoProperties noProperties\"." + + " Consider to declare/implement a mapping method: \".*NoProperties map(" + + ".*WithProperties value)" ) } ) public void shouldFailOnNoElementMappingFoundForStreamToList() { diff --git a/processor/src/test/java/org/mapstruct/ap/test/java8stream/forged/ForgedStreamMappingTest.java b/processor/src/test/java/org/mapstruct/ap/test/java8stream/forged/ForgedStreamMappingTest.java index 3737584a8..67091f579 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/java8stream/forged/ForgedStreamMappingTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/java8stream/forged/ForgedStreamMappingTest.java @@ -70,9 +70,8 @@ public class ForgedStreamMappingTest { @Diagnostic(type = ErroneousStreamNonMappableStreamMapper.class, kind = Kind.ERROR, line = 17, - messageRegExp = "Can't map Stream element \".* nonMappableStream\" to \".* nonMappableStream\". " - + "Consider to declare/implement a mapping method: .*."), - } + messageRegExp = "No target bean properties found: can't map Stream element \".* nonMappableStream\" " + + "to \".* nonMappableStream\". Consider to declare/implement a mapping method: .*." ) } ) public void shouldGenerateNonMappableMethodForSetMapping() { } diff --git a/processor/src/test/java/org/mapstruct/ap/test/selection/generics/ConversionTest.java b/processor/src/test/java/org/mapstruct/ap/test/selection/generics/ConversionTest.java index 3d4df45bd..d46a4b6d0 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/selection/generics/ConversionTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/selection/generics/ConversionTest.java @@ -85,9 +85,9 @@ public class ConversionTest { diagnostics = { @Diagnostic(type = ErroneousSourceTargetMapper1.class, kind = javax.tools.Diagnostic.Kind.ERROR, line = 16, - messageRegExp = "Can't map property \"org.mapstruct.ap.test.selection.generics.UpperBoundWrapper" - + " fooUpperBoundFailure\" to " - + "\"org.mapstruct.ap.test.selection.generics.TypeA fooUpperBoundFailure\"") + messageRegExp = "No target bean properties found: can't map property \"org.mapstruct.ap.test.selection." + + "generics.UpperBoundWrapper " + + "fooUpperBoundFailure\" to \"org.mapstruct.ap.test.selection.generics.TypeA fooUpperBoundFailure\"") }) public void shouldFailOnUpperBound() { } @@ -99,7 +99,8 @@ public class ConversionTest { @Diagnostic(type = ErroneousSourceTargetMapper2.class, kind = javax.tools.Diagnostic.Kind.ERROR, line = 16, - messageRegExp = "Can't map property \"org.mapstruct.ap.test.selection.generics.WildCardExtendsWrapper" + messageRegExp = "No target bean properties found: can't map property " + + "\"org.mapstruct.ap.test.selection.generics.WildCardExtendsWrapper" + " fooWildCardExtendsTypeAFailure\" to" + " \"org.mapstruct.ap.test.selection.generics.TypeA fooWildCardExtendsTypeAFailure\"") }) @@ -113,7 +114,8 @@ public class ConversionTest { @Diagnostic(type = ErroneousSourceTargetMapper3.class, kind = javax.tools.Diagnostic.Kind.ERROR, line = 16, - messageRegExp = "Can't map property \"org.mapstruct.ap.test.selection.generics." + messageRegExp = "No target bean properties found: can't map property " + + "\"org.mapstruct.ap.test.selection.generics." + "WildCardExtendsMBWrapper " + "fooWildCardExtendsMBTypeBFailure\" to \"org.mapstruct.ap.test.selection.generics.TypeB " + "fooWildCardExtendsMBTypeBFailure\"") @@ -128,7 +130,8 @@ public class ConversionTest { @Diagnostic(type = ErroneousSourceTargetMapper4.class, kind = javax.tools.Diagnostic.Kind.ERROR, line = 16, - messageRegExp = "Can't map property \"org.mapstruct.ap.test.selection.generics.WildCardSuperWrapper" + messageRegExp = "No target bean properties found: can't map property " + + "\"org.mapstruct.ap.test.selection.generics.WildCardSuperWrapper" + " fooWildCardSuperTypeAFailure\" to" + " \"org.mapstruct.ap.test.selection.generics.TypeA fooWildCardSuperTypeAFailure\"") }) @@ -142,7 +145,8 @@ public class ConversionTest { @Diagnostic(type = ErroneousSourceTargetMapper5.class, kind = javax.tools.Diagnostic.Kind.ERROR, line = 16, - messageRegExp = "Can't map property \"org.mapstruct.ap.test.selection.generics.WildCardSuperWrapper" + messageRegExp = "No target bean properties found: can't map property " + + "\"org.mapstruct.ap.test.selection.generics.WildCardSuperWrapper" + " fooWildCardSuperTypeCFailure\" to" + " \"org.mapstruct.ap.test.selection.generics.TypeC fooWildCardSuperTypeCFailure\"") }) @@ -161,7 +165,7 @@ public class ConversionTest { @Diagnostic(type = ErroneousSourceTargetMapper6.class, kind = javax.tools.Diagnostic.Kind.ERROR, line = 16, - messageRegExp = "Can't map property \".*NoProperties " + messageRegExp = "No target bean properties found: can't map property \".*NoProperties " + "foo\\.wrapped\" to" + " \"org.mapstruct.ap.test.selection.generics.TypeA " + "foo\\.wrapped\""), diff --git a/processor/src/test/java/org/mapstruct/ap/test/verbose/CreateMapMapping.java b/processor/src/test/java/org/mapstruct/ap/test/verbose/CreateMapMapping.java index 815625689..69dc148e6 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/verbose/CreateMapMapping.java +++ b/processor/src/test/java/org/mapstruct/ap/test/verbose/CreateMapMapping.java @@ -17,7 +17,6 @@ public interface CreateMapMapping { Map map(Map source); - // empty beans fail.. TODO check class SourceKey { private String name;