#156 correctly handle imports / fully qualified names for @TargetType class literals

This commit is contained in:
Andreas Gudian 2014-04-04 23:04:07 +02:00 committed by Gunnar Morling
parent e5b9ee2cb1
commit c051741824
8 changed files with 182 additions and 6 deletions

View File

@ -24,7 +24,7 @@
return<#if returnType.name != "void"> null</#if>;
}
<#if !existingInstanceMapping><@includeModel object=resultType/> ${resultName} = <#if factoryMethod??><@includeModel object=factoryMethod targetType="${resultType.name}"/><#else>new <@includeModel object=resultType/>()</#if>;</#if>
<#if !existingInstanceMapping><@includeModel object=resultType/> ${resultName} = <#if factoryMethod??><@includeModel object=factoryMethod targetType=resultType raw=true/><#else>new <@includeModel object=resultType/>()</#if>;</#if>
<#if (sourceParameters?size > 1)>
<#list sourceParameters as sourceParam>
if ( ${sourceParam.name} != null ) {

View File

@ -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/>
</#compress></@assignResult>
<#-- b) simple conversion -->
<#elseif conversion??>

View File

@ -18,4 +18,4 @@
limitations under the License.
-->
<#if imported>${name}<#else>${fullyQualifiedName}</#if><#if (typeParameters?size > 0) ><<#list typeParameters as typeParameter><@includeModel object=typeParameter /><#if typeParameter_has_next>, </#if></#list>></#if>
<#if imported>${name}<#else>${fullyQualifiedName}</#if><#if (!ext.raw?? && typeParameters?size > 0) ><<#list typeParameters as typeParameter><@includeModel object=typeParameter /><#if typeParameter_has_next>, </#if></#list>></#if>

View File

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

View File

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

View File

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

View File

@ -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> T fromFoo(Foo fromFoo, @TargetType Class<T> 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;
}
}

View File

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