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 70af263fc..bcc1ab14f 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.BeanMappingMethod.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.BeanMappingMethod.ftl @@ -24,7 +24,7 @@ return<#if returnType.name != "void"> null; } - <#if !existingInstanceMapping><@includeModel object=resultType/> ${resultName} = <#if factoryMethod??><@includeModel object=factoryMethod targetType="${resultType.name}"/><#else>new <@includeModel object=resultType/>(); + <#if !existingInstanceMapping><@includeModel object=resultType/> ${resultName} = <#if factoryMethod??><@includeModel object=factoryMethod targetType=resultType raw=true/><#else>new <@includeModel object=resultType/>(); <#if (sourceParameters?size > 1)> <#list sourceParameters as sourceParam> if ( ${sourceParam.name} != null ) { 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 0efba97cf..69d42e91c 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl @@ -29,7 +29,7 @@ targetAccessorName=targetAccessorName sourceBeanName=sourceBeanName sourceAccessorName=sourceAccessorName><#compress> - <@includeModel object=mappingMethod input="${sourceBeanName}.${sourceAccessorName}()" targetType=targetType.name/> + <@includeModel object=mappingMethod input="${sourceBeanName}.${sourceAccessorName}()" targetType=targetType raw=true/> <#-- b) simple conversion --> <#elseif conversion??> 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 124a4f999..1cc27a83d 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. --> -<#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 +<#if imported>${name}<#else>${fullyQualifiedName}<#if (!ext.raw?? && 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 index 2207fc90d..0c77fe63d 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/imports/ConflictingTypesNamesTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/ConflictingTypesNamesTest.java @@ -24,6 +24,8 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.mapstruct.ap.test.imports.from.Foo; +import org.mapstruct.ap.test.imports.from.FooWrapper; +import org.mapstruct.ap.test.imports.referenced.GenericMapper; import org.mapstruct.ap.test.imports.referenced.NotImportedDatatype; import org.mapstruct.ap.test.imports.referenced.Source; import org.mapstruct.ap.test.imports.referenced.Target; @@ -45,13 +47,24 @@ import org.mapstruct.ap.testutil.runner.GeneratedSource; List.class, Map.class, Foo.class, - org.mapstruct.ap.test.imports.to.Foo.class, Source.class, Target.class, NotImportedDatatype.class + org.mapstruct.ap.test.imports.to.Foo.class, + Source.class, + Target.class, + NotImportedDatatype.class, + GenericMapper.class, + FooWrapper.class, + org.mapstruct.ap.test.imports.to.FooWrapper.class, + SecondSourceTargetMapper.class }) @RunWith(AnnotationProcessorTestRunner.class) public class ConflictingTypesNamesTest { + private GeneratedSource generatedSource = new GeneratedSource(); + @Rule - public GeneratedSource generatedSource = new GeneratedSource(); + public GeneratedSource getGeneratedSource() { + return generatedSource; + } @Test public void mapperImportingTypesWithConflictingNamesCanBeGenerated() { @@ -72,7 +85,8 @@ public class ConflictingTypesNamesTest { } @Test - public void mapperNoUnecessaryImports() { + @IssueKey( "178" ) + public void mapperHasNoUnecessaryImports() { Source source = new Source(); source.setNotImported( new NotImportedDatatype( 42 ) ); @@ -83,4 +97,20 @@ public class ConflictingTypesNamesTest { generatedSource.forMapper( SourceTargetMapper.class ).containsNoImportFor( NotImportedDatatype.class ); } + + @Test + @IssueKey( "156" ) + public void importsForTargetTypes() { + FooWrapper source = new FooWrapper(); + Foo value = new Foo(); + value.setName( "foo" ); + source.setValue( value ); + + org.mapstruct.ap.test.imports.to.FooWrapper result = + SecondSourceTargetMapper.INSTANCE.fooWrapperToFooWrapper( source ); + + assertThat( result ).isNotNull(); + assertThat( result.getValue() ).isNotNull(); + assertThat( result.getValue().getName() ).isEqualTo( "foo" ); + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/SecondSourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/imports/SecondSourceTargetMapper.java new file mode 100644 index 000000000..03ed6789d --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/SecondSourceTargetMapper.java @@ -0,0 +1,35 @@ +/** + * 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.ap.test.imports.referenced.GenericMapper; +import org.mapstruct.ap.test.imports.to.FooWrapper; +import org.mapstruct.factory.Mappers; + +/** + * @author Andreas Gudian + */ +@Mapper( uses = GenericMapper.class ) +public interface SecondSourceTargetMapper { + + SecondSourceTargetMapper INSTANCE = Mappers.getMapper( SecondSourceTargetMapper.class ); + + FooWrapper fooWrapperToFooWrapper(org.mapstruct.ap.test.imports.from.FooWrapper foo); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/from/FooWrapper.java b/processor/src/test/java/org/mapstruct/ap/test/imports/from/FooWrapper.java new file mode 100644 index 000000000..805beb037 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/from/FooWrapper.java @@ -0,0 +1,35 @@ +/** + * 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.from; + +/** + * @author Andreas Gudian + * + */ +public class FooWrapper { + private Foo value; + + public Foo getValue() { + return value; + } + + public void setValue(Foo value) { + this.value = value; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/referenced/GenericMapper.java b/processor/src/test/java/org/mapstruct/ap/test/imports/referenced/GenericMapper.java new file mode 100644 index 000000000..c338ddd65 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/referenced/GenericMapper.java @@ -0,0 +1,41 @@ +/** + * 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.referenced; + +import org.mapstruct.TargetType; +import org.mapstruct.ap.test.imports.from.Foo; + +/** + * @author Andreas Gudian + * + */ +public class GenericMapper { + + @SuppressWarnings( "unchecked" ) + public T fromFoo(Foo fromFoo, @TargetType Class toFooClass) { + if ( org.mapstruct.ap.test.imports.to.Foo.class == toFooClass ) { + org.mapstruct.ap.test.imports.to.Foo result = new org.mapstruct.ap.test.imports.to.Foo(); + result.setName( fromFoo.getName() ); + + return (T) result; + } + + return null; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/imports/to/FooWrapper.java b/processor/src/test/java/org/mapstruct/ap/test/imports/to/FooWrapper.java new file mode 100644 index 000000000..9621c13c5 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/imports/to/FooWrapper.java @@ -0,0 +1,35 @@ +/** + * 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.to; + +/** + * @author Andreas Gudian + * + */ +public class FooWrapper { + private Foo value; + + public Foo getValue() { + return value; + } + + public void setValue(Foo value) { + this.value = value; + } +}