#194 Only generate field / imports for a referenced mapper if it is actually used

This commit is contained in:
sjaakd 2015-02-04 23:10:37 +01:00
parent 0772dce41b
commit 4e10c8451c
15 changed files with 278 additions and 58 deletions

View File

@ -0,0 +1,30 @@
/**
* Copyright 2012-2015 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.model;
/**
* Basic interface class that facilitates an empty constructor
*
* @author Sjaak Derksen
*/
public interface Constructor {
String getName();
}

View File

@ -27,7 +27,6 @@ import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import org.mapstruct.ap.model.common.Accessibility;
import org.mapstruct.ap.model.common.ModelElement;
import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.common.TypeFactory;
import org.mapstruct.ap.option.Options;
@ -43,9 +42,86 @@ public class Decorator extends GeneratedType {
private static final String IMPLEMENTATION_SUFFIX = "Impl";
public static class Builder {
private Elements elementUtils;
private TypeFactory typeFactory;
private TypeElement mapperElement;
private DecoratedWithPrism decoratorPrism;
private List<MappingMethod> methods;
private Options options;
private VersionInformation versionInformation;
private boolean hasDelegateConstructor;
public Builder elementUtils(Elements elementUtils) {
this.elementUtils = elementUtils;
return this;
}
public Builder typeFactory(TypeFactory typeFactory) {
this.typeFactory = typeFactory;
return this;
}
public Builder mapperElement(TypeElement mapperElement) {
this.mapperElement = mapperElement;
return this;
}
public Builder decoratorPrism(DecoratedWithPrism decoratorPrism) {
this.decoratorPrism = decoratorPrism;
return this;
}
public Builder methods(List<MappingMethod> methods) {
this.methods = methods;
return this;
}
public Builder options(Options options) {
this.options = options;
return this;
}
public Builder versionInformation(VersionInformation versionInformation) {
this.versionInformation = versionInformation;
return this;
}
public Builder hasDelegateConstructor(boolean hasDelegateConstructor) {
this.hasDelegateConstructor = hasDelegateConstructor;
return this;
}
public Decorator build() {
Type decoratorType = typeFactory.getType( decoratorPrism.value() );
DecoratorConstructor decoratorConstructor = new DecoratorConstructor(
mapperElement.getSimpleName().toString() + IMPLEMENTATION_SUFFIX,
mapperElement.getSimpleName().toString() + "Impl_",
hasDelegateConstructor );
return new Decorator(
typeFactory,
elementUtils.getPackageOf( mapperElement ).getQualifiedName().toString(),
mapperElement.getSimpleName().toString() + IMPLEMENTATION_SUFFIX,
decoratorType.getName(),
mapperElement.getKind() == ElementKind.INTERFACE ? mapperElement.getSimpleName().toString() : null,
methods,
Arrays.asList( new Field( typeFactory.getType( mapperElement ), "delegate", true ) ) ,
options,
versionInformation,
Accessibility.fromModifiers( mapperElement.getModifiers() ),
decoratorConstructor
);
}
}
@SuppressWarnings( "checkstyle:parameternumber" )
private Decorator(TypeFactory typeFactory, String packageName, String name, String superClassName,
String interfaceName, List<MappingMethod> methods, List<? extends ModelElement> fields,
Options options, VersionInformation versionInformation, Accessibility accessibility) {
String interfaceName, List<MappingMethod> methods, List<? extends Field> fields,
Options options, VersionInformation versionInformation, Accessibility accessibility,
DecoratorConstructor decoratorConstructor) {
super(
typeFactory,
packageName,
@ -57,34 +133,8 @@ public class Decorator extends GeneratedType {
options,
versionInformation,
accessibility,
new TreeSet<Type>()
);
}
public static Decorator getInstance(Elements elementUtils, TypeFactory typeFactory, TypeElement mapperElement,
DecoratedWithPrism decoratorPrism, List<MappingMethod> methods,
boolean hasDelegateConstructor, Options options,
VersionInformation versionInformation) {
Type decoratorType = typeFactory.getType( decoratorPrism.value() );
return new Decorator(
typeFactory,
elementUtils.getPackageOf( mapperElement ).getQualifiedName().toString(),
mapperElement.getSimpleName().toString() + IMPLEMENTATION_SUFFIX,
decoratorType.getName(),
mapperElement.getKind() == ElementKind.INTERFACE ? mapperElement.getSimpleName().toString() : null,
methods,
Arrays.asList(
new Field( typeFactory.getType( mapperElement ), "delegate", true ),
new DecoratorConstructor(
mapperElement.getSimpleName().toString() + IMPLEMENTATION_SUFFIX,
mapperElement.getSimpleName().toString() + "Impl_",
hasDelegateConstructor
)
),
options,
versionInformation,
Accessibility.fromModifiers( mapperElement.getModifiers() )
new TreeSet<Type>(),
decoratorConstructor
);
}

View File

@ -20,8 +20,8 @@ package org.mapstruct.ap.model;
import java.util.Collections;
import java.util.Set;
import org.mapstruct.ap.model.common.ModelElement;
import org.mapstruct.ap.model.common.Type;
/**
@ -29,7 +29,7 @@ import org.mapstruct.ap.model.common.Type;
*
* @author Gunnar Morling
*/
public class DecoratorConstructor extends ModelElement {
public class DecoratorConstructor extends ModelElement implements Constructor {
private final String name;
private final String delegateName;
@ -46,6 +46,7 @@ public class DecoratorConstructor extends ModelElement {
return Collections.emptySet();
}
@Override
public String getName() {
return name;
}

View File

@ -34,16 +34,19 @@ public class Field extends ModelElement {
private final Type type;
private final String variableName;
private boolean used;
private boolean typeRequiresImport;
public Field(Type type, String variableName, boolean used) {
this.type = type;
this.variableName = variableName;
this.used = used;
this.typeRequiresImport = used;
}
public Field(Type type, String variableName) {
this.type = type;
this.variableName = variableName;
this.used = false;
this.typeRequiresImport = false;
}
/**
@ -85,4 +88,20 @@ public class Field extends ModelElement {
this.used = isUsed;
}
/**
* field needs to be imported
* @return true if the type should be included in the import of the generated type
*/
public boolean isTypeRequiresImport() {
return typeRequiresImport;
}
/**
* set field needs to be imported
* @param typeRequiresImport needs to be imported
*/
public void setTypeRequiresImport(boolean typeRequiresImport) {
this.typeRequiresImport = typeRequiresImport;
}
}

View File

@ -49,13 +49,14 @@ public abstract class GeneratedType extends ModelElement {
private final List<Annotation> annotations;
private final List<MappingMethod> methods;
private final List<? extends ModelElement> fields;
private final List<? extends Field> fields;
private final SortedSet<Type> extraImportedTypes;
private final boolean suppressGeneratorTimestamp;
private final boolean suppressGeneratorVersionComment;
private final VersionInformation versionInformation;
private final Accessibility accessibility;
private final Constructor constructor;
/**
* Type representing the {@code @Generated} annotation
@ -66,11 +67,12 @@ public abstract class GeneratedType extends ModelElement {
protected GeneratedType(TypeFactory typeFactory, String packageName, String name, String superClassName,
String interfaceName,
List<MappingMethod> methods,
List<? extends ModelElement> fields,
List<? extends Field> fields,
Options options,
VersionInformation versionInformation,
Accessibility accessibility,
SortedSet<Type> extraImportedTypes) {
SortedSet<Type> extraImportedTypes,
Constructor constructor ) {
this.packageName = packageName;
this.name = name;
this.superClassName = superClassName;
@ -87,6 +89,7 @@ public abstract class GeneratedType extends ModelElement {
this.accessibility = accessibility;
this.generatedType = typeFactory.getType( Generated.class );
this.constructor = constructor;
}
// CHECKSTYLE:ON
@ -150,9 +153,11 @@ public abstract class GeneratedType extends ModelElement {
}
}
for ( ModelElement field : fields ) {
for ( Type type : field.getImportTypes() ) {
addWithDependents( importedTypes, type );
for ( Field field : fields ) {
if ( field.isTypeRequiresImport() ) {
for ( Type type : field.getImportTypes() ) {
addWithDependents( importedTypes, type );
}
}
}
@ -167,6 +172,10 @@ public abstract class GeneratedType extends ModelElement {
return importedTypes;
}
public Constructor getConstructor() {
return constructor;
}
private void addWithDependents(Collection<Type> collection, Type typeToAdd) {
if ( typeToAdd == null ) {
return;

View File

@ -63,7 +63,8 @@ public class Mapper extends GeneratedType {
options,
versionInformation,
accessibility,
extraImportedTypes
extraImportedTypes,
null
);
this.referencedMappers = referencedMappers;

View File

@ -205,16 +205,18 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
messager.printMessage( element, decoratorPrism.mirror, Message.DECORATOR_CONSTRUCTOR );
}
return Decorator.getInstance(
elementUtils,
typeFactory,
element,
decoratorPrism,
mappingMethods,
hasDelegateConstructor,
options,
versionInformation
);
Decorator decorator = new Decorator.Builder()
.elementUtils( elementUtils )
.typeFactory( typeFactory )
.mapperElement( element )
.decoratorPrism( decoratorPrism )
.methods( mappingMethods )
.hasDelegateConstructor( hasDelegateConstructor )
.options( options )
.versionInformation( versionInformation )
.build();
return decorator;
}
private SortedSet<Type> getExtraImports(TypeElement element) {

View File

@ -502,6 +502,7 @@ public class MappingResolverImpl implements MappingResolver {
for ( MapperReference ref : mapperReferences ) {
if ( ref.getType().equals( method.getDeclaringMapper() ) ) {
ref.setUsed( !method.isStatic() );
ref.setTypeRequiresImport( true );
return ref;
}
}

View File

@ -18,5 +18,5 @@
limitations under the License.
-->
<#if used><#nt><@includeModel object=annotation/>
private <@includeModel object=type/> ${variableName};</#if>
<#nt><@includeModel object=annotation/>
private <@includeModel object=type/> ${variableName};

View File

@ -18,4 +18,4 @@
limitations under the License.
-->
<#if used>private final <@includeModel object=type/> ${variableName} = <#if annotatedMapper>Mappers.getMapper( <@includeModel object=type/>.class );<#else>new <@includeModel object=type/>();</#if></#if>
private final <@includeModel object=type/> ${variableName} = <#if annotatedMapper>Mappers.getMapper( <@includeModel object=type/>.class );<#else>new <@includeModel object=type/>();</#if>

View File

@ -18,4 +18,4 @@
limitations under the License.
-->
<#if used>private final <@includeModel object=type/> ${variableName};</#if>
private final <@includeModel object=type/> ${variableName};

View File

@ -34,9 +34,10 @@ import ${importedType.importName};
</#list>
<#lt>${accessibility.keyword} class ${name}<#if superClassName??> extends ${superClassName}</#if><#if interfaceName??> implements ${interfaceName}</#if> {
<#list fields as field>
<#nt> <@includeModel object=field/>
</#list>
<#list fields as field><#if field.used><#nt> <@includeModel object=field/>
</#if></#list>
<#if constructor??><#nt> <@includeModel object=constructor/></#if>
<#list methods as method>
<#nt> <@includeModel object=method/>

View File

@ -0,0 +1,53 @@
/**
* Copyright 2012-2015 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.statics;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ap.test.references.statics.nonused.NonUsedMapper;
import org.mapstruct.factory.Mappers;
/**
*
* @author Sjaak Derksen
*/
@Mapper(uses = NonUsedMapper.class )
public abstract class BeerMapperWithNonUsedMapper {
public static final BeerMapperWithNonUsedMapper INSTANCE = Mappers.getMapper( BeerMapperWithNonUsedMapper.class );
@Mapping( target = "category", source = "percentage")
public abstract BeerDto mapBeer(Beer beer);
public static Category toCategory(float in) {
if ( in < 2.5 ) {
return Category.LIGHT;
}
else if ( in < 5.5 ) {
return Category.LAGER;
}
else if ( in < 10 ) {
return Category.STRONG;
}
else {
return Category.BARLEY_WINE;
}
}
}

View File

@ -23,18 +23,29 @@ import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import static org.fest.assertions.Assertions.assertThat;
import org.junit.Rule;
import org.junit.Test;
import org.mapstruct.ap.test.references.statics.nonused.NonUsedMapper;
import org.mapstruct.ap.testutil.runner.GeneratedSource;
/**
*
* @author Sjaak Derksen
*/
@IssueKey( "410" )
@WithClasses( { Beer.class, BeerDto.class, BeerMapper.class, Category.class, CustomMapper.class } )
@WithClasses( { Beer.class, BeerDto.class, Category.class } )
@RunWith(AnnotationProcessorTestRunner.class)
public class StaticsTest {
private final GeneratedSource generatedSource = new GeneratedSource();
@Rule
public GeneratedSource getGeneratedSource() {
return generatedSource;
}
@Test
@WithClasses( { BeerMapper.class, CustomMapper.class } )
public void shouldUseStaticMethod() {
Beer beer = new Beer(); // what the heck, open another one..
@ -44,4 +55,19 @@ public class StaticsTest {
assertThat( result ).isNotNull();
assertThat( result.getCategory() ).isEqualTo( Category.STRONG ); // why settle for less?
}
@Test
@WithClasses( { BeerMapperWithNonUsedMapper.class, NonUsedMapper.class } )
public void shouldNotImportNonUsed() {
Beer beer = new Beer(); // what the heck, open another one..
beer.setPercentage( 7 );
BeerDto result = BeerMapperWithNonUsedMapper.INSTANCE.mapBeer( beer );
assertThat( result ).isNotNull();
assertThat( result.getCategory() ).isEqualTo( Category.STRONG ); // I could shurly use one now..
generatedSource.forMapper( BeerMapperWithNonUsedMapper.class ).containsNoImportFor( NonUsedMapper.class );
}
}

View File

@ -0,0 +1,27 @@
/**
* Copyright 2012-2015 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.statics.nonused;
/**
*
* @author Sjaak Derksen
*/
public class NonUsedMapper {
}