#82 Use Mappers factory to instanciate used mappers annotated with @Mapper

This commit is contained in:
Andreas Gudian 2014-01-03 20:43:31 +01:00
parent 1c64a704d9
commit 7d9b42cf35
4 changed files with 38 additions and 4 deletions

View File

@ -23,6 +23,7 @@ import java.util.Set;
import org.mapstruct.ap.util.Collections; import org.mapstruct.ap.util.Collections;
import org.mapstruct.ap.util.Strings; import org.mapstruct.ap.util.Strings;
import org.mapstruct.ap.util.TypeFactory;
/** /**
* Mapper reference which is retrieved via the {@code Mappers#getMapper()} method. Used by default if no other component * Mapper reference which is retrieved via the {@code Mappers#getMapper()} method. Used by default if no other component
@ -33,9 +34,18 @@ import org.mapstruct.ap.util.Strings;
public class DefaultMapperReference extends AbstractModelElement implements MapperReference { public class DefaultMapperReference extends AbstractModelElement implements MapperReference {
private final Type type; private final Type type;
private final boolean isAnnotatedMapper;
private final Set<Type> importTypes;
public DefaultMapperReference(Type type) { public DefaultMapperReference(Type type, TypeFactory typeFactory) {
this.type = type; this.type = type;
isAnnotatedMapper = type.isAnnotatedWith( "org.mapstruct.ap.model.Mapper" );
importTypes = Collections.asSet( type );
if ( isAnnotatedMapper() ) {
importTypes.add( typeFactory.getType( "org.mapstruct.factory.Mappers" ) );
}
} }
@Override @Override
@ -45,10 +55,14 @@ public class DefaultMapperReference extends AbstractModelElement implements Mapp
@Override @Override
public Set<Type> getImportTypes() { public Set<Type> getImportTypes() {
return Collections.asSet( type ); return importTypes;
} }
public String getVariableName() { public String getVariableName() {
return Strings.getSaveVariableName( Introspector.decapitalize( type.getName() ) ); return Strings.getSaveVariableName( Introspector.decapitalize( type.getName() ) );
} }
public boolean isAnnotatedMapper() {
return isAnnotatedMapper;
}
} }

View File

@ -21,7 +21,10 @@ package org.mapstruct.ap.model;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.ElementKind; import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType; import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
@ -161,6 +164,23 @@ public class Type extends AbstractModelElement implements Comparable<Type> {
Collections.<Type>emptySet(); Collections.<Type>emptySet();
} }
/**
* @param annotationTypeName the fully qualified name of the annotation type
* @return true, if the type is annotated with an annotation of the specified type (super-types are not inspected)
*/
public boolean isAnnotatedWith(String annotationTypeName) {
List<? extends AnnotationMirror> annotationMirrors = typeElement.getAnnotationMirrors();
for ( AnnotationMirror mirror : annotationMirrors ) {
Name mirrorAnnotationName = ( (TypeElement) mirror.getAnnotationType().asElement() ).getQualifiedName();
if ( mirrorAnnotationName.contentEquals( annotationTypeName ) ) {
return true;
}
}
return false;
}
/** /**
* Whether this type is assignable to the given other type. * Whether this type is assignable to the given other type.
* *

View File

@ -147,7 +147,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
MapperPrism mapperPrism = MapperPrism.getInstanceOn( element ); MapperPrism mapperPrism = MapperPrism.getInstanceOn( element );
for ( TypeMirror usedMapper : mapperPrism.uses() ) { for ( TypeMirror usedMapper : mapperPrism.uses() ) {
mapperReferences.add( new DefaultMapperReference( typeFactory.getType( usedMapper ) ) ); mapperReferences.add( new DefaultMapperReference( typeFactory.getType( usedMapper ), typeFactory ) );
} }
return mapperReferences; return mapperReferences;

View File

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