From ceaa869c65599de472f20255408497bc0c1e9a57 Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Tue, 30 May 2017 21:35:39 +0200 Subject: [PATCH] #1215 Add import correctly for array types --- .../ap/internal/model/GeneratedType.java | 10 +++- .../ap/internal/model/common/Type.java | 2 +- .../ap/internal/model/common/TypeFactory.java | 20 ++++++-- .../ap/internal/model/GeneratedType.ftl | 4 +- .../ap/test/bugs/_1215/Issue1215Test.java | 49 +++++++++++++++++++ .../ap/test/bugs/_1215/dto/EntityDTO.java | 46 +++++++++++++++++ .../ap/test/bugs/_1215/entity/AnotherTag.java | 34 +++++++++++++ .../ap/test/bugs/_1215/entity/Entity.java | 43 ++++++++++++++++ .../ap/test/bugs/_1215/entity/Tag.java | 34 +++++++++++++ .../bugs/_1215/mapper/Issue1215Mapper.java | 31 ++++++++++++ 10 files changed, 265 insertions(+), 8 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/Issue1215Test.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/dto/EntityDTO.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/entity/AnotherTag.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/entity/Entity.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/entity/Tag.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/mapper/Issue1215Mapper.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/GeneratedType.java b/processor/src/main/java/org/mapstruct/ap/internal/model/GeneratedType.java index 5ae330af4..c9eb107d6 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/GeneratedType.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/GeneratedType.java @@ -199,6 +199,14 @@ public abstract class GeneratedType extends ModelElement { return importedTypes; } + public SortedSet getImportTypeNames() { + SortedSet importTypeNames = new TreeSet(); + for ( Type type : getImportTypes() ) { + importTypeNames.add( type.getImportName() ); + } + return importTypeNames; + } + public Constructor getConstructor() { return constructor; } @@ -222,7 +230,7 @@ public abstract class GeneratedType extends ModelElement { return false; } - if ( typeToAdd.getTypeMirror().getKind() != TypeKind.DECLARED ) { + if ( typeToAdd.getTypeMirror().getKind() != TypeKind.DECLARED && !typeToAdd.isArrayType() ) { return false; } 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 a0dad2c0e..46b884396 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 @@ -290,7 +290,7 @@ public class Type extends ModelElement implements Comparable { * @return The name of this type as to be used within import statements. */ public String getImportName() { - return isArrayType() ? qualifiedName.substring( 0, qualifiedName.length() - 2 ) : qualifiedName; + return isArrayType() ? TypeFactory.trimSimpleClassName( qualifiedName ) : qualifiedName; } @Override diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java b/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java index a2dc7709f..89c3a2d90 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/common/TypeFactory.java @@ -175,6 +175,7 @@ public class TypeFactory { String qualifiedName; TypeElement typeElement; Type componentType; + boolean isImported; if ( mirror.getKind() == TypeKind.DECLARED ) { DeclaredType declaredType = (DeclaredType) mirror; @@ -195,28 +196,38 @@ public class TypeFactory { } componentType = null; + isImported = isImported( name, qualifiedName ); } else if ( mirror.getKind() == TypeKind.ARRAY ) { TypeMirror componentTypeMirror = getComponentType( mirror ); + StringBuilder builder = new StringBuilder("[]"); + + while ( componentTypeMirror.getKind() == TypeKind.ARRAY ) { + componentTypeMirror = getComponentType( componentTypeMirror ); + builder.append( "[]" ); + } if ( componentTypeMirror.getKind() == TypeKind.DECLARED ) { DeclaredType declaredType = (DeclaredType) componentTypeMirror; TypeElement componentTypeElement = (TypeElement) declaredType.asElement(); - name = componentTypeElement.getSimpleName().toString() + "[]"; + String arraySuffix = builder.toString(); + name = componentTypeElement.getSimpleName().toString() + arraySuffix; packageName = elementUtils.getPackageOf( componentTypeElement ).getQualifiedName().toString(); - qualifiedName = componentTypeElement.getQualifiedName().toString() + "[]"; + qualifiedName = componentTypeElement.getQualifiedName().toString() + arraySuffix; + isImported = isImported( name, qualifiedName ); } else { name = mirror.toString(); packageName = null; qualifiedName = name; + isImported = false; } isEnumType = false; isInterface = false; typeElement = null; - componentType = getType( componentTypeMirror ); + componentType = getType( getComponentType( mirror ) ); } else { isEnumType = false; @@ -226,6 +237,7 @@ public class TypeFactory { qualifiedName = name; typeElement = null; componentType = null; + isImported = false; } return new Type( @@ -244,7 +256,7 @@ public class TypeFactory { isCollectionType, isMapType, isStreamType, - isImported( name, qualifiedName ) + isImported ); } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/GeneratedType.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/GeneratedType.ftl index 01fe414e5..0656686b4 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/GeneratedType.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/GeneratedType.ftl @@ -23,8 +23,8 @@ package ${packageName}; -<#list importTypes as importedType> -import ${importedType.importName}; +<#list importTypeNames as importedType> +import ${importedType}; <#if !generatedTypeAvailable>/* diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/Issue1215Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/Issue1215Test.java new file mode 100644 index 000000000..1e4255eb0 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/Issue1215Test.java @@ -0,0 +1,49 @@ +/** + * Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.test.bugs._1215; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mapstruct.ap.test.bugs._1215.dto.EntityDTO; +import org.mapstruct.ap.test.bugs._1215.entity.AnotherTag; +import org.mapstruct.ap.test.bugs._1215.entity.Entity; +import org.mapstruct.ap.test.bugs._1215.entity.Tag; +import org.mapstruct.ap.test.bugs._1215.mapper.Issue1215Mapper; +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.WithClasses; +import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; + +/** + * @author Filip Hrisafov + */ +@WithClasses( { + EntityDTO.class, + Entity.class, + Tag.class, + AnotherTag.class, + Issue1215Mapper.class +} ) +@IssueKey( "1215" ) +@RunWith( AnnotationProcessorTestRunner.class ) +public class Issue1215Test { + + @Test + public void shouldCompile() { + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/dto/EntityDTO.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/dto/EntityDTO.java new file mode 100644 index 000000000..364dda4b1 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/dto/EntityDTO.java @@ -0,0 +1,46 @@ +/** + * Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.test.bugs._1215.dto; + +import org.mapstruct.ap.test.bugs._1215.entity.AnotherTag; +import org.mapstruct.ap.test.bugs._1215.entity.Tag; + +/** + * @author Filip Hrisafov + */ +public class EntityDTO { + private Tag[] tags; + private AnotherTag[][] otherTags; + + public Tag[] getTags() { + return tags; + } + + public void setTags(Tag[] tags) { + this.tags = tags; + } + + public AnotherTag[][] getOtherTags() { + return otherTags; + } + + public void setOtherTags(AnotherTag[][] otherTags) { + this.otherTags = otherTags; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/entity/AnotherTag.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/entity/AnotherTag.java new file mode 100644 index 000000000..16f454567 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/entity/AnotherTag.java @@ -0,0 +1,34 @@ +/** + * Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.test.bugs._1215.entity; + +/** + * @author Filip Hrisafov + */ +public class AnotherTag { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/entity/Entity.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/entity/Entity.java new file mode 100644 index 000000000..e6e9a170f --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/entity/Entity.java @@ -0,0 +1,43 @@ +/** + * Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.test.bugs._1215.entity; + +/** + * @author Filip Hrisafov + */ +public class Entity { + private Tag[] tags; + private AnotherTag[][] otherTags; + + public Tag[] getTags() { + return tags; + } + + public void setTags(Tag[] tags) { + this.tags = tags; + } + + public AnotherTag[][] getOtherTags() { + return otherTags; + } + + public void setOtherTags(AnotherTag[][] otherTags) { + this.otherTags = otherTags; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/entity/Tag.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/entity/Tag.java new file mode 100644 index 000000000..b5962fee1 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/entity/Tag.java @@ -0,0 +1,34 @@ +/** + * Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.test.bugs._1215.entity; + +/** + * @author Filip Hrisafov + */ +public class Tag { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/mapper/Issue1215Mapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/mapper/Issue1215Mapper.java new file mode 100644 index 000000000..da517a25b --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1215/mapper/Issue1215Mapper.java @@ -0,0 +1,31 @@ +/** + * Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.test.bugs._1215.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.ap.test.bugs._1215.dto.EntityDTO; +import org.mapstruct.ap.test.bugs._1215.entity.Entity; + +/** + * @author Filip Hrisafov + */ +@Mapper +public interface Issue1215Mapper { + Entity fromDTO(EntityDTO dto); +}