#2880 Fix missing import for array mapping methods

Co-authored-by: Martin Kamp Jensen <martin.kamp.jensen@se.com>
This commit is contained in:
Filip Hrisafov 2022-06-18 13:59:03 +02:00 committed by GitHub
parent fa800926e7
commit 98eb46aee9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 130 additions and 7 deletions

View File

@ -181,8 +181,11 @@ public class Type extends ModelElement implements Comparable<Type> {
this.loggingVerbose = loggingVerbose; this.loggingVerbose = loggingVerbose;
this.topLevelType = topLevelType( this.typeElement, this.typeFactory ); // The top level type for an array type is the top level type of the component type
this.nameWithTopLevelTypeName = nameWithTopLevelTypeName( this.typeElement ); TypeElement typeElementForTopLevel =
this.componentType == null ? this.typeElement : this.componentType.getTypeElement();
this.topLevelType = topLevelType( typeElementForTopLevel, this.typeFactory );
this.nameWithTopLevelTypeName = nameWithTopLevelTypeName( typeElementForTopLevel, this.name );
} }
//CHECKSTYLE:ON //CHECKSTYLE:ON
@ -218,11 +221,21 @@ public class Type extends ModelElement implements Comparable<Type> {
* (if the top level type is important, otherwise the fully-qualified name. * (if the top level type is important, otherwise the fully-qualified name.
*/ */
public String createReferenceName() { 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; return name;
} }
if ( isTopLevelTypeToBeImported() && nameWithTopLevelTypeName != null ) { if ( shouldUseSimpleName() ) {
return name;
}
if ( isTopLevelTypeToBeImported() && nameWithTopLevelTypeName != null) {
return nameWithTopLevelTypeName; return nameWithTopLevelTypeName;
} }
@ -1566,16 +1579,16 @@ public class Type extends ModelElement implements Comparable<Type> {
return trimmedClassName; return trimmedClassName;
} }
private static String nameWithTopLevelTypeName(TypeElement element) { private static String nameWithTopLevelTypeName(TypeElement element, String name) {
if ( element == null ) { if ( element == null ) {
return null; return null;
} }
if ( !element.getNestingKind().isNested() ) { if ( !element.getNestingKind().isNested() ) {
return element.getSimpleName().toString(); return name;
} }
Deque<CharSequence> elements = new ArrayDeque<>(); Deque<CharSequence> elements = new ArrayDeque<>();
elements.addFirst( element.getSimpleName() ); elements.addFirst( name );
Element parent = element.getEnclosingElement(); Element parent = element.getEnclosingElement();
while ( parent != null && parent.getKind() != ElementKind.PACKAGE ) { while ( parent != null && parent.getKind() != ElementKind.PACKAGE ) {
elements.addFirst( parent.getSimpleName() ); elements.addFirst( parent.getSimpleName() );

View File

@ -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);
}

View File

@ -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() {
}
}

View File

@ -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
}
}

View File

@ -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<Outer.SourceData> data3;
public List<Outer.SourceData> data4;
// CHECKSTYLE:ON
}

View File

@ -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<TargetData> data2;
public TargetData[] data3;
public List<TargetData> data4;
// CHECKSTYLE:ON
}

View File

@ -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
}