From 98eb46aee9aec4135fed2bd0c2fbd0c621c22b08 Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Sat, 18 Jun 2022 13:59:03 +0200 Subject: [PATCH] #2880 Fix missing import for array mapping methods Co-authored-by: Martin Kamp Jensen --- .../ap/internal/model/common/Type.java | 27 ++++++++++++++----- .../ap/test/bugs/_2880/Issue2880Mapper.java | 14 ++++++++++ .../ap/test/bugs/_2880/Issue2880Test.java | 25 +++++++++++++++++ .../mapstruct/ap/test/bugs/_2880/Outer.java | 16 +++++++++++ .../mapstruct/ap/test/bugs/_2880/Source.java | 21 +++++++++++++++ .../mapstruct/ap/test/bugs/_2880/Target.java | 21 +++++++++++++++ .../ap/test/bugs/_2880/TargetData.java | 13 +++++++++ 7 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Issue2880Mapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Issue2880Test.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Outer.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Source.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Target.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/TargetData.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java b/processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java index ba5f62254..ce82fff62 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java @@ -181,8 +181,11 @@ public class Type extends ModelElement implements Comparable { this.loggingVerbose = loggingVerbose; - this.topLevelType = topLevelType( this.typeElement, this.typeFactory ); - this.nameWithTopLevelTypeName = nameWithTopLevelTypeName( this.typeElement ); + // The top level type for an array type is the top level type of the component type + TypeElement typeElementForTopLevel = + this.componentType == null ? this.typeElement : this.componentType.getTypeElement(); + this.topLevelType = topLevelType( typeElementForTopLevel, this.typeFactory ); + this.nameWithTopLevelTypeName = nameWithTopLevelTypeName( typeElementForTopLevel, this.name ); } //CHECKSTYLE:ON @@ -218,11 +221,21 @@ public class Type extends ModelElement implements Comparable { * (if the top level type is important, otherwise the fully-qualified name. */ public String createReferenceName() { - if ( isToBeImported() || shouldUseSimpleName() ) { + if ( isToBeImported() ) { + // isToBeImported() returns true for arrays. + // Therefore, we need to check the top level type when creating the reference + if ( isTopLevelTypeToBeImported() ) { + return nameWithTopLevelTypeName != null ? nameWithTopLevelTypeName : name; + } + return name; } - if ( isTopLevelTypeToBeImported() && nameWithTopLevelTypeName != null ) { + if ( shouldUseSimpleName() ) { + return name; + } + + if ( isTopLevelTypeToBeImported() && nameWithTopLevelTypeName != null) { return nameWithTopLevelTypeName; } @@ -1566,16 +1579,16 @@ public class Type extends ModelElement implements Comparable { return trimmedClassName; } - private static String nameWithTopLevelTypeName(TypeElement element) { + private static String nameWithTopLevelTypeName(TypeElement element, String name) { if ( element == null ) { return null; } if ( !element.getNestingKind().isNested() ) { - return element.getSimpleName().toString(); + return name; } Deque elements = new ArrayDeque<>(); - elements.addFirst( element.getSimpleName() ); + elements.addFirst( name ); Element parent = element.getEnclosingElement(); while ( parent != null && parent.getKind() != ElementKind.PACKAGE ) { elements.addFirst( parent.getSimpleName() ); diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Issue2880Mapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Issue2880Mapper.java new file mode 100644 index 000000000..218f6d384 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Issue2880Mapper.java @@ -0,0 +1,14 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.test.bugs._2880; + +import org.mapstruct.Mapper; + +@Mapper +public interface Issue2880Mapper { + + Target map(Source source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Issue2880Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Issue2880Test.java new file mode 100644 index 000000000..e3d0f810c --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Issue2880Test.java @@ -0,0 +1,25 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.test.bugs._2880; + +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.ProcessorTest; +import org.mapstruct.ap.testutil.WithClasses; + +@IssueKey("2880") +@WithClasses({ + Issue2880Mapper.class, + Outer.class, + Source.class, + Target.class, + TargetData.class +}) +public class Issue2880Test { + + @ProcessorTest + void shouldCompile() { + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Outer.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Outer.java new file mode 100644 index 000000000..90b756137 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Outer.java @@ -0,0 +1,16 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.test.bugs._2880; + +public class Outer { + + public static class SourceData { + + // CHECKSTYLE:OFF + public String value; + // CHECKSTYLE:ON + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Source.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Source.java new file mode 100644 index 000000000..0ab3b8166 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Source.java @@ -0,0 +1,21 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.test.bugs._2880; + +import java.util.List; + +public class Source { + + // CHECKSTYLE:OFF + public Outer.SourceData[] data1; + + public Outer.SourceData[] data2; + + public List data3; + + public List data4; + // CHECKSTYLE:ON +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Target.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Target.java new file mode 100644 index 000000000..28c6b1cca --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/Target.java @@ -0,0 +1,21 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.test.bugs._2880; + +import java.util.List; + +public class Target { + + // CHECKSTYLE:OFF + public TargetData[] data1; + + public List data2; + + public TargetData[] data3; + + public List data4; + // CHECKSTYLE:ON +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/TargetData.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/TargetData.java new file mode 100644 index 000000000..28cca9660 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_2880/TargetData.java @@ -0,0 +1,13 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.test.bugs._2880; + +public class TargetData { + + // CHECKSTYLE:OFF + public String value; + // CHECKSTYLE:ON +}