mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#82 Use Mappers factory to instanciate used mappers annotated with @Mapper
This commit is contained in:
parent
1c64a704d9
commit
7d9b42cf35
@ -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<Type> 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<Type> getImportTypes() {
|
||||
return Collections.asSet( type );
|
||||
return importTypes;
|
||||
}
|
||||
|
||||
public String getVariableName() {
|
||||
return Strings.getSaveVariableName( Introspector.decapitalize( type.getName() ) );
|
||||
}
|
||||
|
||||
public boolean isAnnotatedMapper() {
|
||||
return isAnnotatedMapper;
|
||||
}
|
||||
}
|
||||
|
@ -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<Type> {
|
||||
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.
|
||||
*
|
||||
|
@ -147,7 +147,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
MapperPrism mapperPrism = MapperPrism.getInstanceOn( element );
|
||||
|
||||
for ( TypeMirror usedMapper : mapperPrism.uses() ) {
|
||||
mapperReferences.add( new DefaultMapperReference( typeFactory.getType( usedMapper ) ) );
|
||||
mapperReferences.add( new DefaultMapperReference( typeFactory.getType( usedMapper ), typeFactory ) );
|
||||
}
|
||||
|
||||
return mapperReferences;
|
||||
|
@ -18,4 +18,4 @@
|
||||
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>
|
Loading…
x
Reference in New Issue
Block a user