org.mapstruct.ap.internal.gem.MapperGem Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapstruct-processor Show documentation
Show all versions of mapstruct-processor Show documentation
An annotation processor for generating type-safe bean mappers
package org.mapstruct.ap.internal.gem;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.AbstractAnnotationValueVisitor8;
import javax.lang.model.util.ElementFilter;
import org.mapstruct.tools.gem.Gem;
import org.mapstruct.tools.gem.GemValue;
import javax.lang.model.type.TypeMirror;
public class MapperGem implements Gem {
private final GemValue> uses;
private final GemValue> imports;
private final GemValue unmappedSourcePolicy;
private final GemValue unmappedTargetPolicy;
private final GemValue typeConversionPolicy;
private final GemValue componentModel;
private final GemValue implementationName;
private final GemValue implementationPackage;
private final GemValue config;
private final GemValue collectionMappingStrategy;
private final GemValue nullValueMappingStrategy;
private final GemValue nullValuePropertyMappingStrategy;
private final GemValue mappingInheritanceStrategy;
private final GemValue nullValueCheckStrategy;
private final GemValue injectionStrategy;
private final GemValue disableSubMappingMethodsGeneration;
private final GemValue builder;
private final GemValue mappingControl;
private final GemValue unexpectedValueMappingException;
private final boolean isValid;
private final AnnotationMirror mirror;
private MapperGem( BuilderImpl builder ) {
this.uses = builder.uses;
this.imports = builder.imports;
this.unmappedSourcePolicy = builder.unmappedSourcePolicy;
this.unmappedTargetPolicy = builder.unmappedTargetPolicy;
this.typeConversionPolicy = builder.typeConversionPolicy;
this.componentModel = builder.componentModel;
this.implementationName = builder.implementationName;
this.implementationPackage = builder.implementationPackage;
this.config = builder.config;
this.collectionMappingStrategy = builder.collectionMappingStrategy;
this.nullValueMappingStrategy = builder.nullValueMappingStrategy;
this.nullValuePropertyMappingStrategy = builder.nullValuePropertyMappingStrategy;
this.mappingInheritanceStrategy = builder.mappingInheritanceStrategy;
this.nullValueCheckStrategy = builder.nullValueCheckStrategy;
this.injectionStrategy = builder.injectionStrategy;
this.disableSubMappingMethodsGeneration = builder.disableSubMappingMethodsGeneration;
this.builder = builder.builder;
this.mappingControl = builder.mappingControl;
this.unexpectedValueMappingException = builder.unexpectedValueMappingException;
isValid = ( this.uses != null ? this.uses.isValid() : false )
&& ( this.imports != null ? this.imports.isValid() : false )
&& ( this.unmappedSourcePolicy != null ? this.unmappedSourcePolicy.isValid() : false )
&& ( this.unmappedTargetPolicy != null ? this.unmappedTargetPolicy.isValid() : false )
&& ( this.typeConversionPolicy != null ? this.typeConversionPolicy.isValid() : false )
&& ( this.componentModel != null ? this.componentModel.isValid() : false )
&& ( this.implementationName != null ? this.implementationName.isValid() : false )
&& ( this.implementationPackage != null ? this.implementationPackage.isValid() : false )
&& ( this.config != null ? this.config.isValid() : false )
&& ( this.collectionMappingStrategy != null ? this.collectionMappingStrategy.isValid() : false )
&& ( this.nullValueMappingStrategy != null ? this.nullValueMappingStrategy.isValid() : false )
&& ( this.nullValuePropertyMappingStrategy != null ? this.nullValuePropertyMappingStrategy.isValid() : false )
&& ( this.mappingInheritanceStrategy != null ? this.mappingInheritanceStrategy.isValid() : false )
&& ( this.nullValueCheckStrategy != null ? this.nullValueCheckStrategy.isValid() : false )
&& ( this.injectionStrategy != null ? this.injectionStrategy.isValid() : false )
&& ( this.disableSubMappingMethodsGeneration != null ? this.disableSubMappingMethodsGeneration.isValid() : false )
&& ( this.builder != null ? this.builder.isValid() : false )
&& ( this.mappingControl != null ? this.mappingControl.isValid() : false )
&& ( this.unexpectedValueMappingException != null ? this.unexpectedValueMappingException.isValid() : false );
mirror = builder.mirror;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#uses}
*/
public GemValue> uses( ) {
return uses;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#imports}
*/
public GemValue> imports( ) {
return imports;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#unmappedSourcePolicy}
*/
public GemValue unmappedSourcePolicy( ) {
return unmappedSourcePolicy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#unmappedTargetPolicy}
*/
public GemValue unmappedTargetPolicy( ) {
return unmappedTargetPolicy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#typeConversionPolicy}
*/
public GemValue typeConversionPolicy( ) {
return typeConversionPolicy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#componentModel}
*/
public GemValue componentModel( ) {
return componentModel;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#implementationName}
*/
public GemValue implementationName( ) {
return implementationName;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#implementationPackage}
*/
public GemValue implementationPackage( ) {
return implementationPackage;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#config}
*/
public GemValue config( ) {
return config;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#collectionMappingStrategy}
*/
public GemValue collectionMappingStrategy( ) {
return collectionMappingStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#nullValueMappingStrategy}
*/
public GemValue nullValueMappingStrategy( ) {
return nullValueMappingStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#nullValuePropertyMappingStrategy}
*/
public GemValue nullValuePropertyMappingStrategy( ) {
return nullValuePropertyMappingStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#mappingInheritanceStrategy}
*/
public GemValue mappingInheritanceStrategy( ) {
return mappingInheritanceStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#nullValueCheckStrategy}
*/
public GemValue nullValueCheckStrategy( ) {
return nullValueCheckStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#injectionStrategy}
*/
public GemValue injectionStrategy( ) {
return injectionStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#disableSubMappingMethodsGeneration}
*/
public GemValue disableSubMappingMethodsGeneration( ) {
return disableSubMappingMethodsGeneration;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#builder}
*/
public GemValue builder( ) {
return builder;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#mappingControl}
*/
public GemValue mappingControl( ) {
return mappingControl;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperGem#unexpectedValueMappingException}
*/
public GemValue unexpectedValueMappingException( ) {
return unexpectedValueMappingException;
}
@Override
public AnnotationMirror mirror( ) {
return mirror;
}
@Override
public boolean isValid( ) {
return isValid;
}
public static MapperGem instanceOn(Element element) {
return build( element, new BuilderImpl() );
}
public static MapperGem instanceOn(AnnotationMirror mirror ) {
return build( mirror, new BuilderImpl() );
}
public static T build(Element element, Builder builder) {
AnnotationMirror mirror = element.getAnnotationMirrors().stream()
.filter( a -> "org.mapstruct.Mapper".contentEquals( ( ( TypeElement )a.getAnnotationType().asElement() ).getQualifiedName() ) )
.findAny()
.orElse( null );
return build( mirror, builder );
}
public static T build(AnnotationMirror mirror, Builder builder ) {
// return fast
if ( mirror == null || builder == null ) {
return null;
}
// fetch defaults from all defined values in the annotation type
List enclosed = ElementFilter.methodsIn( mirror.getAnnotationType().asElement().getEnclosedElements() );
Map defaultValues = new HashMap<>( enclosed.size() );
enclosed.forEach( e -> defaultValues.put( e.getSimpleName().toString(), e.getDefaultValue() ) );
// fetch all explicitely set annotation values in the annotation instance
Map values = new HashMap<>( enclosed.size() );
mirror.getElementValues().entrySet().forEach( e -> values.put( e.getKey().getSimpleName().toString(), e.getValue() ) );
// iterate and populate builder
for ( String methodName : defaultValues.keySet() ) {
if ( "uses".equals( methodName ) ) {
builder.setUses( GemValue.createArray( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
}
else if ( "imports".equals( methodName ) ) {
builder.setImports( GemValue.createArray( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
}
else if ( "unmappedSourcePolicy".equals( methodName ) ) {
builder.setUnmappedsourcepolicy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
}
else if ( "unmappedTargetPolicy".equals( methodName ) ) {
builder.setUnmappedtargetpolicy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
}
else if ( "typeConversionPolicy".equals( methodName ) ) {
builder.setTypeconversionpolicy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
}
else if ( "componentModel".equals( methodName ) ) {
builder.setComponentmodel( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "implementationName".equals( methodName ) ) {
builder.setImplementationname( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "implementationPackage".equals( methodName ) ) {
builder.setImplementationpackage( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "config".equals( methodName ) ) {
builder.setConfig( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
}
else if ( "collectionMappingStrategy".equals( methodName ) ) {
builder.setCollectionmappingstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
}
else if ( "nullValueMappingStrategy".equals( methodName ) ) {
builder.setNullvaluemappingstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
}
else if ( "nullValuePropertyMappingStrategy".equals( methodName ) ) {
builder.setNullvaluepropertymappingstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
}
else if ( "mappingInheritanceStrategy".equals( methodName ) ) {
builder.setMappinginheritancestrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
}
else if ( "nullValueCheckStrategy".equals( methodName ) ) {
builder.setNullvaluecheckstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
}
else if ( "injectionStrategy".equals( methodName ) ) {
builder.setInjectionstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
}
else if ( "disableSubMappingMethodsGeneration".equals( methodName ) ) {
builder.setDisablesubmappingmethodsgeneration( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), Boolean.class ) );
}
else if ( "builder".equals( methodName ) ) {
builder.setBuilder( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), BuilderGem::instanceOn ) );
}
else if ( "mappingControl".equals( methodName ) ) {
builder.setMappingcontrol( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
}
else if ( "unexpectedValueMappingException".equals( methodName ) ) {
builder.setUnexpectedvaluemappingexception( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
}
}
builder.setMirror( mirror );
return builder.build();
}
/**
* A builder that can be implemented by the user to define custom logic e.g. in the
* build method, prior to creating the annotation gem.
*/
public interface Builder {
/**
* Sets the {@link GemValue} for {@link MapperGem#uses}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setUses(GemValue> methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#imports}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setImports(GemValue> methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#unmappedSourcePolicy}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setUnmappedsourcepolicy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#unmappedTargetPolicy}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setUnmappedtargetpolicy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#typeConversionPolicy}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setTypeconversionpolicy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#componentModel}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setComponentmodel(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#implementationName}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setImplementationname(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#implementationPackage}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setImplementationpackage(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#config}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setConfig(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#collectionMappingStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setCollectionmappingstrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#nullValueMappingStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setNullvaluemappingstrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#nullValuePropertyMappingStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setNullvaluepropertymappingstrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#mappingInheritanceStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setMappinginheritancestrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#nullValueCheckStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setNullvaluecheckstrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#injectionStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setInjectionstrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#disableSubMappingMethodsGeneration}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setDisablesubmappingmethodsgeneration(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#builder}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setBuilder(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#mappingControl}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setMappingcontrol(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperGem#unexpectedValueMappingException}
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setUnexpectedvaluemappingexception(GemValue methodName );
/**
* Sets the annotation mirror
*
* @param mirror the mirror which this gem represents
*
* @return the {@link Builder} for this gem, representing {@link MapperGem}
*/
Builder setMirror( AnnotationMirror mirror );
/**
* The build method can be overriden in a custom custom implementation, which allows
* the user to define his own custom validation on the annotation.
*
* @return the representation of the annotation
*/
T build();
}
private static class BuilderImpl implements Builder {
private GemValue> uses;
private GemValue> imports;
private GemValue unmappedSourcePolicy;
private GemValue unmappedTargetPolicy;
private GemValue typeConversionPolicy;
private GemValue componentModel;
private GemValue implementationName;
private GemValue implementationPackage;
private GemValue config;
private GemValue collectionMappingStrategy;
private GemValue nullValueMappingStrategy;
private GemValue nullValuePropertyMappingStrategy;
private GemValue mappingInheritanceStrategy;
private GemValue nullValueCheckStrategy;
private GemValue injectionStrategy;
private GemValue disableSubMappingMethodsGeneration;
private GemValue builder;
private GemValue mappingControl;
private GemValue unexpectedValueMappingException;
private AnnotationMirror mirror;
public Builder setUses(GemValue> uses ) {
this.uses = uses;
return this;
}
public Builder setImports(GemValue> imports ) {
this.imports = imports;
return this;
}
public Builder setUnmappedsourcepolicy(GemValue unmappedSourcePolicy ) {
this.unmappedSourcePolicy = unmappedSourcePolicy;
return this;
}
public Builder setUnmappedtargetpolicy(GemValue unmappedTargetPolicy ) {
this.unmappedTargetPolicy = unmappedTargetPolicy;
return this;
}
public Builder setTypeconversionpolicy(GemValue typeConversionPolicy ) {
this.typeConversionPolicy = typeConversionPolicy;
return this;
}
public Builder setComponentmodel(GemValue componentModel ) {
this.componentModel = componentModel;
return this;
}
public Builder setImplementationname(GemValue implementationName ) {
this.implementationName = implementationName;
return this;
}
public Builder setImplementationpackage(GemValue implementationPackage ) {
this.implementationPackage = implementationPackage;
return this;
}
public Builder setConfig(GemValue config ) {
this.config = config;
return this;
}
public Builder setCollectionmappingstrategy(GemValue collectionMappingStrategy ) {
this.collectionMappingStrategy = collectionMappingStrategy;
return this;
}
public Builder setNullvaluemappingstrategy(GemValue nullValueMappingStrategy ) {
this.nullValueMappingStrategy = nullValueMappingStrategy;
return this;
}
public Builder setNullvaluepropertymappingstrategy(GemValue nullValuePropertyMappingStrategy ) {
this.nullValuePropertyMappingStrategy = nullValuePropertyMappingStrategy;
return this;
}
public Builder setMappinginheritancestrategy(GemValue mappingInheritanceStrategy ) {
this.mappingInheritanceStrategy = mappingInheritanceStrategy;
return this;
}
public Builder setNullvaluecheckstrategy(GemValue nullValueCheckStrategy ) {
this.nullValueCheckStrategy = nullValueCheckStrategy;
return this;
}
public Builder setInjectionstrategy(GemValue injectionStrategy ) {
this.injectionStrategy = injectionStrategy;
return this;
}
public Builder setDisablesubmappingmethodsgeneration(GemValue disableSubMappingMethodsGeneration ) {
this.disableSubMappingMethodsGeneration = disableSubMappingMethodsGeneration;
return this;
}
public Builder setBuilder(GemValue builder ) {
this.builder = builder;
return this;
}
public Builder setMappingcontrol(GemValue mappingControl ) {
this.mappingControl = mappingControl;
return this;
}
public Builder setUnexpectedvaluemappingexception(GemValue unexpectedValueMappingException ) {
this.unexpectedValueMappingException = unexpectedValueMappingException;
return this;
}
public Builder setMirror( AnnotationMirror mirror ) {
this.mirror = mirror;
return this;
}
public MapperGem build() {
return new MapperGem( this );
}
}
}