#410 do not instantiate a field (referenced mapper) when not used, e.g. only static referenced

This commit is contained in:
sjaakd 2015-01-19 21:05:16 +01:00
parent d2796d7bf6
commit aa0e658f58
11 changed files with 43 additions and 10 deletions

View File

@ -33,8 +33,8 @@ public class AnnotationMapperReference extends MapperReference {
private final Annotation annotation;
public AnnotationMapperReference(Type type, String variableName, Annotation annotation) {
super( type, variableName );
public AnnotationMapperReference(Type type, String variableName, Annotation annotation, boolean isUsed) {
super( type, variableName, isUsed );
this.annotation = annotation;
}

View File

@ -96,6 +96,7 @@ public class AssignmentFactory {
private static MapperReference findMapperReference( List<MapperReference> mapperReferences, SourceMethod method ) {
for ( MapperReference ref : mapperReferences ) {
if ( ref.getType().equals( method.getDeclaringMapper() ) ) {
ref.setUsed( !method.isStatic() );
return ref;
}
}

View File

@ -70,7 +70,7 @@ public class Decorator extends GeneratedType {
mapperElement.getKind() == ElementKind.INTERFACE ? mapperElement.getSimpleName().toString() : null,
methods,
Arrays.asList(
new Field( typeFactory.getType( mapperElement ), "delegate" ),
new Field( typeFactory.getType( mapperElement ), "delegate", true ),
new DecoratorConstructor(
mapperElement.getSimpleName().toString() + IMPLEMENTATION_SUFFIX,
mapperElement.getSimpleName().toString() + "Impl_",

View File

@ -33,10 +33,17 @@ public class Field extends ModelElement {
private final Type type;
private final String variableName;
private boolean used;
public Field(Type type, String variableName, boolean used) {
this.type = type;
this.variableName = variableName;
this.used = used;
}
public Field(Type type, String variableName) {
this.type = type;
this.variableName = variableName;
this.used = false;
}
/**
@ -51,7 +58,7 @@ public class Field extends ModelElement {
/**
* Returns the variable name of this field.
*
* @return the variable name of this referfieldence
* @return the variable name of this reference
*/
public String getVariableName() {
return variableName;
@ -61,4 +68,21 @@ public class Field extends ModelElement {
public Set<Type> getImportTypes() {
return Collections.emptySet();
}
/**
* indicates whether the field is indeed used
* @return true when field is used
*/
public boolean isUsed() {
return used;
}
/**
* sets the field as being used
* @param isUsed must be true when being used.
*/
public void setUsed(boolean isUsed) {
this.used = isUsed;
}
}

View File

@ -30,4 +30,8 @@ public abstract class MapperReference extends Field {
public MapperReference(Type type, String variableName) {
super( type, variableName );
}
public MapperReference(Type type, String variableName, boolean isUsed) {
super( type, variableName, isUsed );
}
}

View File

@ -76,7 +76,8 @@ public abstract class AnnotationBasedComponentModelProcessor implements ModelEle
return new AnnotationMapperReference(
originalReference.getType(),
originalReference.getVariableName(),
getMapperReferenceAnnotation()
getMapperReferenceAnnotation(),
originalReference.isUsed()
);
}

View File

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

View File

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

View File

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

View File

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

View File

@ -22,9 +22,11 @@ package org.mapstruct.ap.test.references.statics;
*
* @author Sjaak Derksen
*/
//@CHECKSTYLE:OFF
public class CustomMapper {
private CustomMapper() {
}
public static Category toCategory(float in) {
if ( in < 2.5 ) {