From e5668325f070acd93dd4220d1905778a88fb53a0 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Wed, 29 Jan 2014 21:03:20 +0100 Subject: [PATCH] #112 Making sure not more than one type with the same simple name is imported --- .../resources/build-config/checkstyle.xml | 5 + .../org/mapstruct/ap/MappingProcessor.java | 9 +- .../java/org/mapstruct/ap/model/Mapper.java | 4 +- .../org/mapstruct/ap/model/common/Type.java | 94 ++++++++----------- .../ap/model/common/TypeFactory.java | 80 +++++++++++++++- .../org.mapstruct.ap.model.Annotation.ftl | 2 +- ...uct.ap.model.AnnotationMapperReference.ftl | 4 +- ...g.mapstruct.ap.model.BeanMappingMethod.ftl | 4 +- ...struct.ap.model.DefaultMapperReference.ftl | 2 +- .../org.mapstruct.ap.model.Mapper.ftl | 6 +- ...org.mapstruct.ap.model.PropertyMapping.ftl | 2 +- .../org.mapstruct.ap.model.common.Type.ftl | 2 +- .../imports/ConflictingTypesNamesTest.java | 47 ++++++++++ .../org/mapstruct/ap/test/imports/List.java | 23 +++++ .../org/mapstruct/ap/test/imports/Map.java | 23 +++++ .../org/mapstruct/ap/test/imports/Named.java | 50 ++++++++++ .../ap/test/imports/ParseException.java | 52 ++++++++++ .../ap/test/imports/SourceTargetMapper.java | 38 ++++++++ .../samename/Jsr330SourceTargetMapper.java | 2 + ...ferencedMappersWithSameSimpleNameTest.java | 25 ++++- .../samename/SourceTargetMapper.java | 2 + .../samename/a/AnotherSourceTargetMapper.java | 38 ++++++++ .../samename/{ => model}/Source.java | 2 +- .../samename/{ => model}/Target.java | 2 +- 24 files changed, 440 insertions(+), 78 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/imports/ConflictingTypesNamesTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/imports/List.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/imports/Map.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/imports/Named.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/imports/ParseException.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/imports/SourceTargetMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/references/samename/a/AnotherSourceTargetMapper.java rename processor/src/test/java/org/mapstruct/ap/test/references/samename/{ => model}/Source.java (95%) rename processor/src/test/java/org/mapstruct/ap/test/references/samename/{ => model}/Target.java (95%) diff --git a/build-config/src/main/resources/build-config/checkstyle.xml b/build-config/src/main/resources/build-config/checkstyle.xml index 431648bf9..3de95264f 100644 --- a/build-config/src/main/resources/build-config/checkstyle.xml +++ b/build-config/src/main/resources/build-config/checkstyle.xml @@ -56,6 +56,9 @@ + + + @@ -183,4 +186,6 @@ + + diff --git a/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java b/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java index a4bfd929a..7dd6dd8f0 100644 --- a/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java @@ -39,7 +39,6 @@ import javax.lang.model.element.TypeElement; import javax.lang.model.util.ElementKindVisitor6; import javax.tools.Diagnostic.Kind; -import org.mapstruct.Mapper; import org.mapstruct.ap.option.Options; import org.mapstruct.ap.option.ReportingPolicy; import org.mapstruct.ap.processor.DefaultModelElementProcessorContext; @@ -122,7 +121,6 @@ public class MappingProcessor extends AbstractProcessor { @Override public boolean process(final Set annotations, final RoundEnvironment roundEnvironment) { - ProcessorContext context = new DefaultModelElementProcessorContext( processingEnv, options ); for ( TypeElement annotation : annotations ) { @@ -134,6 +132,13 @@ public class MappingProcessor extends AbstractProcessor { for ( Element mapperElement : roundEnvironment.getElementsAnnotatedWith( annotation ) ) { TypeElement mapperTypeElement = asTypeElement( mapperElement ); + + // create a new context for each generated mapper in order to have imports of referenced types + // correctly managed; + // note that this assumes that a new source file is created for each mapper which must not + // necessarily be the case, e.g. in case of several mapper interfaces declared as inner types + // of one outer interface + ProcessorContext context = new DefaultModelElementProcessorContext( processingEnv, options ); processMapperTypeElement( context, mapperTypeElement ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/Mapper.java b/processor/src/main/java/org/mapstruct/ap/model/Mapper.java index 0eef76753..842fd3214 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/Mapper.java +++ b/processor/src/main/java/org/mapstruct/ap/model/Mapper.java @@ -23,7 +23,6 @@ import java.util.Collection; import java.util.List; import java.util.SortedSet; import java.util.TreeSet; - import javax.annotation.Generated; import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement; @@ -149,7 +148,8 @@ public class Mapper extends ModelElement { return; } - if ( typeToAdd.getPackageName() != null && + if ( typeToAdd.isImported() && + typeToAdd.getPackageName() != null && !typeToAdd.getPackageName().equals( packageName ) && !typeToAdd.getPackageName().startsWith( "java.lang" ) ) { collection.add( typeToAdd ); diff --git a/processor/src/main/java/org/mapstruct/ap/model/common/Type.java b/processor/src/main/java/org/mapstruct/ap/model/common/Type.java index 88709c175..5cb47ecf0 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/common/Type.java +++ b/processor/src/main/java/org/mapstruct/ap/model/common/Type.java @@ -21,21 +21,16 @@ package org.mapstruct.ap.model.common; import java.util.Collections; import java.util.List; import java.util.Set; - import javax.lang.model.element.AnnotationMirror; -import javax.lang.model.element.ElementKind; import javax.lang.model.element.Name; import javax.lang.model.element.TypeElement; -import javax.lang.model.type.DeclaredType; -import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; -import javax.lang.model.util.Elements; -import javax.lang.model.util.SimpleElementVisitor6; import javax.lang.model.util.Types; /** - * Represents the type of a bean property, parameter etc. Each type corresponds to a {@link TypeMirror}, i.e. there are - * different instances for e.g. {@code Set} and {@code Set}. + * Represents (a reference to) the type of a bean property, parameter etc. Types are managed per generated source file. + * Each type corresponds to a {@link TypeMirror}, i.e. there are different instances for e.g. {@code Set} and + * {@code Set}. *

* Allows for a unified handling of declared and primitive types and usage within templates. Instances are obtained * through {@link TypeFactory}. @@ -44,58 +39,46 @@ import javax.lang.model.util.Types; */ public class Type extends ModelElement implements Comparable { + private final Types typeUtils; + + private final TypeMirror typeMirror; + private final TypeElement typeElement; + private final List typeParameters; + + private final Type implementationType; + private final String packageName; private final String name; private final String qualifiedName; - private final List typeParameters; + private final boolean isInterface; private final boolean isEnumType; private final boolean isIterableType; private final boolean isCollectionType; private final boolean isMapType; - private final Type implementationType; - private final TypeMirror typeMirror; - private final Types typeUtils; - private final TypeElement typeElement; + private final boolean isImported; - public Type(TypeMirror typeMirror, List typeParameters, Type implementationType, boolean isIterableType, - boolean isCollectionType, boolean isMapType, Types typeUtils, - Elements elementUtils) { + //CHECKSTYLE:OFF + public Type(Types typeUtils, TypeMirror typeMirror, TypeElement typeElement, List typeParameters, + Type implementationType, String packageName, String name, String qualifiedName, boolean isInterface, + boolean isEnumType, boolean isIterableType, boolean isCollectionType, boolean isMapType, + boolean isImported) { + this.typeUtils = typeUtils; this.typeMirror = typeMirror; - this.implementationType = implementationType; + this.typeElement = typeElement; this.typeParameters = typeParameters; + this.implementationType = implementationType; + this.packageName = packageName; + this.name = name; + this.qualifiedName = qualifiedName; + this.isInterface = isInterface; + this.isEnumType = isEnumType; this.isIterableType = isIterableType; this.isCollectionType = isCollectionType; this.isMapType = isMapType; - this.typeUtils = typeUtils; - - DeclaredType declaredType = typeMirror.getKind() == TypeKind.DECLARED ? (DeclaredType) typeMirror : null; - - if ( declaredType != null ) { - isEnumType = declaredType.asElement().getKind() == ElementKind.ENUM; - isInterface = declaredType.asElement().getKind() == ElementKind.INTERFACE; - name = declaredType.asElement().getSimpleName().toString(); - - typeElement = declaredType.asElement().accept( new TypeElementRetrievalVisitor(), null ); - - if ( typeElement != null ) { - packageName = elementUtils.getPackageOf( typeElement ).getQualifiedName().toString(); - qualifiedName = typeElement.getQualifiedName().toString(); - } - else { - packageName = null; - qualifiedName = name; - } - } - else { - isEnumType = false; - isInterface = false; - typeElement = null; - name = typeMirror.toString(); - packageName = null; - qualifiedName = name; - } + this.isImported = isImported; } + //CHECKSTYLE:ON public TypeMirror getTypeMirror() { return typeMirror; @@ -159,8 +142,18 @@ public class Type extends ModelElement implements Comparable { @Override public Set getImportTypes() { - return implementationType != null ? org.mapstruct.ap.util.Collections.asSet( implementationType ) : - Collections.emptySet(); + return implementationType != null ? Collections.singleton( implementationType ) : Collections.emptySet(); + } + + /** + * Whether this type is imported by means of an import statement in the currently generated source file (meaning it + * can be referenced in the generated source using its simple name) or not (meaning it has to be referenced using + * the fully-qualified name). + * + * @return {@code true} if the type is imported, {@code false} otherwise. + */ + public boolean isImported() { + return isImported; } /** @@ -266,11 +259,4 @@ public class Type extends ModelElement implements Comparable { public String toString() { return typeMirror.toString(); } - - private static class TypeElementRetrievalVisitor extends SimpleElementVisitor6 { - @Override - public TypeElement visitType(TypeElement e, Void p) { - return e; - } - } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/common/TypeFactory.java b/processor/src/main/java/org/mapstruct/ap/model/common/TypeFactory.java index 47f1209ff..34167e9d0 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/common/TypeFactory.java +++ b/processor/src/main/java/org/mapstruct/ap/model/common/TypeFactory.java @@ -35,6 +35,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentNavigableMap; import java.util.concurrent.ConcurrentSkipListMap; +import javax.lang.model.element.ElementKind; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; @@ -42,6 +43,7 @@ import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Elements; +import javax.lang.model.util.SimpleElementVisitor6; import javax.lang.model.util.Types; import org.mapstruct.ap.prism.MappingTargetPrism; @@ -62,6 +64,7 @@ public class TypeFactory { private final TypeMirror mapType; private final Map implementationTypes = new HashMap(); + private final Map importedQualifiedTypesBySimpleName = new HashMap(); public TypeFactory(Elements elementUtils, Types typeUtils) { this.elementUtils = elementUtils; @@ -129,15 +132,55 @@ public class TypeFactory { mapType ); + boolean isEnumType; + boolean isInterface; + String name; + String packageName; + TypeElement typeElement; + String qualifiedName; + + DeclaredType declaredType = mirror.getKind() == TypeKind.DECLARED ? (DeclaredType) mirror : null; + + if ( declaredType != null ) { + isEnumType = declaredType.asElement().getKind() == ElementKind.ENUM; + isInterface = declaredType.asElement().getKind() == ElementKind.INTERFACE; + name = declaredType.asElement().getSimpleName().toString(); + + typeElement = declaredType.asElement().accept( new TypeElementRetrievalVisitor(), null ); + + if ( typeElement != null ) { + packageName = elementUtils.getPackageOf( typeElement ).getQualifiedName().toString(); + qualifiedName = typeElement.getQualifiedName().toString(); + } + else { + packageName = null; + qualifiedName = name; + } + } + else { + isEnumType = false; + isInterface = false; + typeElement = null; + name = mirror.toString(); + packageName = null; + qualifiedName = name; + } + return new Type( + typeUtils, mirror, + typeElement, getTypeParameters( mirror ), implementationType, + packageName, + name, + qualifiedName, + isInterface, + isEnumType, isIterableType, isCollectionType, isMapType, - typeUtils, - elementUtils + isImported( name, qualifiedName ) ); } @@ -221,20 +264,49 @@ public class TypeFactory { if ( implementationType != null ) { return new Type( + typeUtils, typeUtils.getDeclaredType( implementationType.getTypeElement(), declaredType.getTypeArguments().toArray( new TypeMirror[] { } ) ), + implementationType.getTypeElement(), getTypeParameters( mirror ), null, + implementationType.getPackageName(), + implementationType.getName(), + implementationType.getFullyQualifiedName(), + implementationType.isInterface(), + implementationType.isEnumType(), implementationType.isIterableType(), implementationType.isCollectionType(), implementationType.isMapType(), - typeUtils, - elementUtils + isImported( implementationType.getName(), implementationType.getFullyQualifiedName() ) ); } return null; } + + private boolean isImported(String name, String qualifiedName) { + String importedType = importedQualifiedTypesBySimpleName.get( name ); + + boolean imported = false; + if ( importedType != null ) { + if ( importedType.equals( qualifiedName ) ) { + imported = true; + } + } + else { + importedQualifiedTypesBySimpleName.put( name, qualifiedName ); + imported = true; + } + return imported; + } + + private static class TypeElementRetrievalVisitor extends SimpleElementVisitor6 { + @Override + public TypeElement visitType(TypeElement e, Void p) { + return e; + } + } } diff --git a/processor/src/main/resources/org.mapstruct.ap.model.Annotation.ftl b/processor/src/main/resources/org.mapstruct.ap.model.Annotation.ftl index 6b5337a46..21ac3e8b2 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.Annotation.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.Annotation.ftl @@ -18,4 +18,4 @@ limitations under the License. --> -@${type.name} \ No newline at end of file +@<@includeModel object=type/> \ No newline at end of file diff --git a/processor/src/main/resources/org.mapstruct.ap.model.AnnotationMapperReference.ftl b/processor/src/main/resources/org.mapstruct.ap.model.AnnotationMapperReference.ftl index 0512ca301..bf887d76e 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.AnnotationMapperReference.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.AnnotationMapperReference.ftl @@ -18,5 +18,5 @@ limitations under the License. --> - <#nt><@includeModel object=annotation/> - private ${mapperType.name} ${variableName}; \ No newline at end of file +<#nt><@includeModel object=annotation/> +private <@includeModel object=mapperType/> ${variableName}; \ No newline at end of file diff --git a/processor/src/main/resources/org.mapstruct.ap.model.BeanMappingMethod.ftl b/processor/src/main/resources/org.mapstruct.ap.model.BeanMappingMethod.ftl index 4271fc521..886a3a142 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.BeanMappingMethod.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.BeanMappingMethod.ftl @@ -19,13 +19,13 @@ --> @Override -public ${returnType.name} ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, ) { +public <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, ) { if ( <#list sourceParameters as sourceParam>${sourceParam.name} == null<#if sourceParam_has_next> && ) { return<#if returnType.name != "void"> null; } <#if !existingInstanceMapping> - ${resultType.name} ${resultName} = new ${resultType.name}(); + <@includeModel object=resultType/> ${resultName} = new <@includeModel object=resultType/>(); <#if (sourceParameters?size > 1)> diff --git a/processor/src/main/resources/org.mapstruct.ap.model.DefaultMapperReference.ftl b/processor/src/main/resources/org.mapstruct.ap.model.DefaultMapperReference.ftl index 86c561eb4..79a7ceac8 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.DefaultMapperReference.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.DefaultMapperReference.ftl @@ -18,4 +18,4 @@ limitations under the License. --> -private final ${mapperType.name} ${variableName} = <#if annotatedMapper>Mappers.getMapper( ${mapperType.name}.class );<#else>new ${mapperType.name}(); \ No newline at end of file +private final <@includeModel object=mapperType/> ${variableName} = <#if annotatedMapper>Mappers.getMapper( <@includeModel object=mapperType/>.class );<#else>new <@includeModel object=mapperType/>(); \ No newline at end of file diff --git a/processor/src/main/resources/org.mapstruct.ap.model.Mapper.ftl b/processor/src/main/resources/org.mapstruct.ap.model.Mapper.ftl index 226a9420d..cbe2ea04e 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.Mapper.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.Mapper.ftl @@ -34,12 +34,10 @@ import ${importedType.fullyQualifiedName}; public class ${implementationName} <#if superTypeInterface>implements<#else>extends ${interfaceName} { <#list referencedMappers as mapper> - <@includeModel object=mapper/> +<#nt> <@includeModel object=mapper/> <#list mappingMethods as mappingMethod> - -<@includeModel object=mappingMethod/> +<#nt> <@includeModel object=mappingMethod/> - } diff --git a/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl b/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl index a2ce497f5..785feaeb7 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl @@ -60,7 +60,7 @@ ${ext.targetBeanName}.${targetAccessorName}( ${sourceBeanName}.${sourceAccessorN ${targetBeanName}.${targetAccessorName}( <@includeModel object=conversion/> ); } <#list conversion.exceptionTypes as exceptionType> - catch( ${exceptionType.name} e ) { + catch( <@includeModel object=exceptionType/> e ) { throw new RuntimeException( e ); } diff --git a/processor/src/main/resources/org.mapstruct.ap.model.common.Type.ftl b/processor/src/main/resources/org.mapstruct.ap.model.common.Type.ftl index fb2560bc4..124a4f999 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.common.Type.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.common.Type.ftl @@ -18,4 +18,4 @@ limitations under the License. --> -${name}<#if (typeParameters?size > 0) ><<#list typeParameters as typeParameter><@includeModel object=typeParameter /><#if typeParameter_has_next>, > \ No newline at end of file +<#if imported>${name}<#else>${fullyQualifiedName}<#if (typeParameters?size > 0) ><<#list typeParameters as typeParameter><@includeModel object=typeParameter /><#if typeParameter_has_next>, > \ No newline at end of file diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/ConflictingTypesNamesTest.java b/processor/src/test/java/org/mapstruct/ap/test/imports/ConflictingTypesNamesTest.java new file mode 100644 index 000000000..4e2ea27f6 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/ConflictingTypesNamesTest.java @@ -0,0 +1,47 @@ +/** + * Copyright 2012-2014 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.imports; + +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.MapperTestBase; +import org.mapstruct.ap.testutil.WithClasses; +import org.testng.annotations.Test; + +import static org.fest.assertions.Assertions.assertThat; + +/** + * Test for generating a mapper which references types whose names clash with names of used annotations and exceptions. + * + * @author Gunnar Morling + */ +@IssueKey("112") +@WithClasses({ Named.class, ParseException.class, SourceTargetMapper.class, List.class, Map.class }) +public class ConflictingTypesNamesTest extends MapperTestBase { + + @Test + public void mapperImportingTypesWithConflictingNamesCanBeGenerated() { + Named source = new Named(); + source.setFoo( 123 ); + source.setBar( 456L ); + + ParseException target = SourceTargetMapper.INSTANCE.sourceToTarget( source ); + + assertThat( target ).isNotNull(); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/List.java b/processor/src/test/java/org/mapstruct/ap/test/imports/List.java new file mode 100644 index 000000000..421b9fe5f --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/List.java @@ -0,0 +1,23 @@ +/** + * Copyright 2012-2014 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.imports; + +public class List { + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/Map.java b/processor/src/test/java/org/mapstruct/ap/test/imports/Map.java new file mode 100644 index 000000000..3da1ac405 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/Map.java @@ -0,0 +1,23 @@ +/** + * Copyright 2012-2014 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.imports; + +public class Map { + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/Named.java b/processor/src/test/java/org/mapstruct/ap/test/imports/Named.java new file mode 100644 index 000000000..38ea4d3dd --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/Named.java @@ -0,0 +1,50 @@ +/** + * Copyright 2012-2014 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.imports; + +public class Named { + + private int foo; + private long bar; + private String qax; + + public int getFoo() { + return foo; + } + + public void setFoo(int foo) { + this.foo = foo; + } + + public long getBar() { + return bar; + } + + public void setBar(long bar) { + this.bar = bar; + } + + public String getQax() { + return qax; + } + + public void setQax(String qax) { + this.qax = qax; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/ParseException.java b/processor/src/test/java/org/mapstruct/ap/test/imports/ParseException.java new file mode 100644 index 000000000..2631cc2eb --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/ParseException.java @@ -0,0 +1,52 @@ +/** + * Copyright 2012-2014 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.imports; + +import java.util.Date; + +public class ParseException { + + private String foo; + private String bar; + private Date qax; + + public String getFoo() { + return foo; + } + + public void setFoo(String foo) { + this.foo = foo; + } + + public String getBar() { + return bar; + } + + public void setBar(String bar) { + this.bar = bar; + } + + public Date getQax() { + return qax; + } + + public void setQax(Date qax) { + this.qax = qax; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/SourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/imports/SourceTargetMapper.java new file mode 100644 index 000000000..d8526aee2 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/SourceTargetMapper.java @@ -0,0 +1,38 @@ +/** + * Copyright 2012-2014 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.imports; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +/** + * @author Gunnar Morling + */ +@Mapper(componentModel = "jsr330") +public interface SourceTargetMapper { + + SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); + + ParseException sourceToTarget(Named source); + + //custom types + Map listToMap(List list); + + java.util.List namedsToExceptions(java.util.List source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/references/samename/Jsr330SourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/references/samename/Jsr330SourceTargetMapper.java index 6c55d960a..249776bc7 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/references/samename/Jsr330SourceTargetMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/references/samename/Jsr330SourceTargetMapper.java @@ -20,6 +20,8 @@ package org.mapstruct.ap.test.references.samename; import org.mapstruct.Mapper; import org.mapstruct.ap.test.references.samename.a.CustomMapper; +import org.mapstruct.ap.test.references.samename.model.Source; +import org.mapstruct.ap.test.references.samename.model.Target; import org.mapstruct.factory.Mappers; /** diff --git a/processor/src/test/java/org/mapstruct/ap/test/references/samename/SeveralReferencedMappersWithSameSimpleNameTest.java b/processor/src/test/java/org/mapstruct/ap/test/references/samename/SeveralReferencedMappersWithSameSimpleNameTest.java index 506497e0f..2b6d2d149 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/references/samename/SeveralReferencedMappersWithSameSimpleNameTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/references/samename/SeveralReferencedMappersWithSameSimpleNameTest.java @@ -18,7 +18,10 @@ */ package org.mapstruct.ap.test.references.samename; +import org.mapstruct.ap.test.references.samename.a.AnotherSourceTargetMapper; import org.mapstruct.ap.test.references.samename.a.CustomMapper; +import org.mapstruct.ap.test.references.samename.model.Source; +import org.mapstruct.ap.test.references.samename.model.Target; import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.MapperTestBase; import org.mapstruct.ap.testutil.WithClasses; @@ -33,8 +36,13 @@ import static org.fest.assertions.Assertions.assertThat; */ @IssueKey("112") @WithClasses({ - Source.class, Target.class, SourceTargetMapper.class, CustomMapper.class, - org.mapstruct.ap.test.references.samename.b.CustomMapper.class, Jsr330SourceTargetMapper.class + Source.class, + Target.class, + SourceTargetMapper.class, + CustomMapper.class, + org.mapstruct.ap.test.references.samename.b.CustomMapper.class, + Jsr330SourceTargetMapper.class, + AnotherSourceTargetMapper.class }) public class SeveralReferencedMappersWithSameSimpleNameTest extends MapperTestBase { @@ -50,4 +58,17 @@ public class SeveralReferencedMappersWithSameSimpleNameTest extends MapperTestBa assertThat( target.getFoo() ).isEqualTo( "246" ); assertThat( target.getBar() ).isEqualTo( "912" ); } + + @Test + public void mapperInSamePackageAndAnotherMapperWithSameNameInAnotherPackageCanBeReferenced() { + Source source = new Source(); + source.setFoo( 123 ); + source.setBar( 456L ); + + Target target = AnotherSourceTargetMapper.INSTANCE.sourceToTarget( source ); + + assertThat( target ).isNotNull(); + assertThat( target.getFoo() ).isEqualTo( "246" ); + assertThat( target.getBar() ).isEqualTo( "912" ); + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/references/samename/SourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/references/samename/SourceTargetMapper.java index 563fd8358..c6b93c8bc 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/references/samename/SourceTargetMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/references/samename/SourceTargetMapper.java @@ -20,6 +20,8 @@ package org.mapstruct.ap.test.references.samename; import org.mapstruct.Mapper; import org.mapstruct.ap.test.references.samename.a.CustomMapper; +import org.mapstruct.ap.test.references.samename.model.Source; +import org.mapstruct.ap.test.references.samename.model.Target; import org.mapstruct.factory.Mappers; /** diff --git a/processor/src/test/java/org/mapstruct/ap/test/references/samename/a/AnotherSourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/references/samename/a/AnotherSourceTargetMapper.java new file mode 100644 index 000000000..281687134 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/references/samename/a/AnotherSourceTargetMapper.java @@ -0,0 +1,38 @@ +/** + * Copyright 2012-2014 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.references.samename.a; + +import org.mapstruct.Mapper; +import org.mapstruct.ap.test.references.samename.model.Source; +import org.mapstruct.ap.test.references.samename.model.Target; +import org.mapstruct.factory.Mappers; + +/** + * @author Gunnar Morling + */ +@Mapper(uses = { + CustomMapper.class, + org.mapstruct.ap.test.references.samename.b.CustomMapper.class +}) +public interface AnotherSourceTargetMapper { + + AnotherSourceTargetMapper INSTANCE = Mappers.getMapper( AnotherSourceTargetMapper.class ); + + Target sourceToTarget(Source source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/references/samename/Source.java b/processor/src/test/java/org/mapstruct/ap/test/references/samename/model/Source.java similarity index 95% rename from processor/src/test/java/org/mapstruct/ap/test/references/samename/Source.java rename to processor/src/test/java/org/mapstruct/ap/test/references/samename/model/Source.java index 6d77eb400..15d12463e 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/references/samename/Source.java +++ b/processor/src/test/java/org/mapstruct/ap/test/references/samename/model/Source.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.references.samename; +package org.mapstruct.ap.test.references.samename.model; public class Source { diff --git a/processor/src/test/java/org/mapstruct/ap/test/references/samename/Target.java b/processor/src/test/java/org/mapstruct/ap/test/references/samename/model/Target.java similarity index 95% rename from processor/src/test/java/org/mapstruct/ap/test/references/samename/Target.java rename to processor/src/test/java/org/mapstruct/ap/test/references/samename/model/Target.java index 9ba9e9843..993868191 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/references/samename/Target.java +++ b/processor/src/test/java/org/mapstruct/ap/test/references/samename/model/Target.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.references.samename; +package org.mapstruct.ap.test.references.samename.model; public class Target {