diff --git a/processor/src/main/java/org/mapstruct/ap/model/DefaultMapperReference.java b/processor/src/main/java/org/mapstruct/ap/model/DefaultMapperReference.java index ae2da9a58..a74bc4cd0 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/DefaultMapperReference.java +++ b/processor/src/main/java/org/mapstruct/ap/model/DefaultMapperReference.java @@ -23,6 +23,7 @@ import java.util.Set; import org.mapstruct.ap.util.Collections; 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 @@ -33,9 +34,18 @@ import org.mapstruct.ap.util.Strings; public class DefaultMapperReference extends AbstractModelElement implements MapperReference { private final Type type; + private final boolean isAnnotatedMapper; + private final Set importTypes; - public DefaultMapperReference(Type type) { + public DefaultMapperReference(Type type, TypeFactory typeFactory) { 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 @@ -45,10 +55,14 @@ public class DefaultMapperReference extends AbstractModelElement implements Mapp @Override public Set getImportTypes() { - return Collections.asSet( type ); + return importTypes; } public String getVariableName() { return Strings.getSaveVariableName( Introspector.decapitalize( type.getName() ) ); } + + public boolean isAnnotatedMapper() { + return isAnnotatedMapper; + } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/Type.java b/processor/src/main/java/org/mapstruct/ap/model/Type.java index 6749eab92..92fd8cf42 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/Type.java +++ b/processor/src/main/java/org/mapstruct/ap/model/Type.java @@ -21,7 +21,10 @@ package org.mapstruct.ap.model; import java.util.Collections; import java.util.List; import java.util.Set; + +import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.ElementKind; +import javax.lang.model.element.Name; import javax.lang.model.element.TypeElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeKind; @@ -161,6 +164,23 @@ public class Type extends AbstractModelElement implements Comparable { Collections.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 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. * diff --git a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java index 0be79e8bc..ec028d7d0 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java @@ -147,7 +147,7 @@ public class MapperCreationProcessor implements ModelElementProcessor -private final ${mapperType.name} ${variableName} = new ${mapperType.name}(); \ No newline at end of file +private final ${mapperType.name} ${variableName} = <#if annotatedMapper>Mappers.getMapper( ${mapperType.name}.class );<#else>new ${mapperType.name}(); \ No newline at end of file