io.github.linpeilie.processor.gem.MapperConfigGem Maven / Gradle / Ivy
The newest version!
package io.github.linpeilie.processor.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 MapperConfigGem implements Gem {
private final GemValue mapperPackage;
private final GemValue unmappedSourcePolicy;
private final GemValue unmappedTargetPolicy;
private final GemValue typeConversionPolicy;
private final GemValue collectionMappingStrategy;
private final GemValue nullValueMappingStrategy;
private final GemValue nullValueIterableMappingStrategy;
private final GemValue nullValueMapMappingStrategy;
private final GemValue nullValuePropertyMappingStrategy;
private final GemValue nullValueCheckStrategy;
private final GemValue mappingControl;
private final GemValue unexpectedValueMappingException;
private final GemValue suppressTimestampInGenerated;
private final GemValue builder;
private final GemValue adapterPackage;
private final GemValue adapterClassName;
private final GemValue mapAdapterClassName;
private final GemValue autoConfigPackage;
private final GemValue autoMapperConfigClassName;
private final GemValue autoMapMapperConfigClassName;
private final boolean isValid;
private final AnnotationMirror mirror;
private MapperConfigGem( BuilderImpl builder ) {
this.mapperPackage = builder.mapperPackage;
this.unmappedSourcePolicy = builder.unmappedSourcePolicy;
this.unmappedTargetPolicy = builder.unmappedTargetPolicy;
this.typeConversionPolicy = builder.typeConversionPolicy;
this.collectionMappingStrategy = builder.collectionMappingStrategy;
this.nullValueMappingStrategy = builder.nullValueMappingStrategy;
this.nullValueIterableMappingStrategy = builder.nullValueIterableMappingStrategy;
this.nullValueMapMappingStrategy = builder.nullValueMapMappingStrategy;
this.nullValuePropertyMappingStrategy = builder.nullValuePropertyMappingStrategy;
this.nullValueCheckStrategy = builder.nullValueCheckStrategy;
this.mappingControl = builder.mappingControl;
this.unexpectedValueMappingException = builder.unexpectedValueMappingException;
this.suppressTimestampInGenerated = builder.suppressTimestampInGenerated;
this.builder = builder.builder;
this.adapterPackage = builder.adapterPackage;
this.adapterClassName = builder.adapterClassName;
this.mapAdapterClassName = builder.mapAdapterClassName;
this.autoConfigPackage = builder.autoConfigPackage;
this.autoMapperConfigClassName = builder.autoMapperConfigClassName;
this.autoMapMapperConfigClassName = builder.autoMapMapperConfigClassName;
isValid = ( this.mapperPackage != null ? this.mapperPackage.isValid() : false )
&& ( this.unmappedSourcePolicy != null ? this.unmappedSourcePolicy.isValid() : false )
&& ( this.unmappedTargetPolicy != null ? this.unmappedTargetPolicy.isValid() : false )
&& ( this.typeConversionPolicy != null ? this.typeConversionPolicy.isValid() : false )
&& ( this.collectionMappingStrategy != null ? this.collectionMappingStrategy.isValid() : false )
&& ( this.nullValueMappingStrategy != null ? this.nullValueMappingStrategy.isValid() : false )
&& ( this.nullValueIterableMappingStrategy != null ? this.nullValueIterableMappingStrategy.isValid() : false )
&& ( this.nullValueMapMappingStrategy != null ? this.nullValueMapMappingStrategy.isValid() : false )
&& ( this.nullValuePropertyMappingStrategy != null ? this.nullValuePropertyMappingStrategy.isValid() : false )
&& ( this.nullValueCheckStrategy != null ? this.nullValueCheckStrategy.isValid() : false )
&& ( this.mappingControl != null ? this.mappingControl.isValid() : false )
&& ( this.unexpectedValueMappingException != null ? this.unexpectedValueMappingException.isValid() : false )
&& ( this.suppressTimestampInGenerated != null ? this.suppressTimestampInGenerated.isValid() : false )
&& ( this.builder != null ? this.builder.isValid() : false )
&& ( this.adapterPackage != null ? this.adapterPackage.isValid() : false )
&& ( this.adapterClassName != null ? this.adapterClassName.isValid() : false )
&& ( this.mapAdapterClassName != null ? this.mapAdapterClassName.isValid() : false )
&& ( this.autoConfigPackage != null ? this.autoConfigPackage.isValid() : false )
&& ( this.autoMapperConfigClassName != null ? this.autoMapperConfigClassName.isValid() : false )
&& ( this.autoMapMapperConfigClassName != null ? this.autoMapMapperConfigClassName.isValid() : false );
mirror = builder.mirror;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#mapperPackage}
*/
public GemValue mapperPackage( ) {
return mapperPackage;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#unmappedSourcePolicy}
*/
public GemValue unmappedSourcePolicy( ) {
return unmappedSourcePolicy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#unmappedTargetPolicy}
*/
public GemValue unmappedTargetPolicy( ) {
return unmappedTargetPolicy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#typeConversionPolicy}
*/
public GemValue typeConversionPolicy( ) {
return typeConversionPolicy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#collectionMappingStrategy}
*/
public GemValue collectionMappingStrategy( ) {
return collectionMappingStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#nullValueMappingStrategy}
*/
public GemValue nullValueMappingStrategy( ) {
return nullValueMappingStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#nullValueIterableMappingStrategy}
*/
public GemValue nullValueIterableMappingStrategy( ) {
return nullValueIterableMappingStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#nullValueMapMappingStrategy}
*/
public GemValue nullValueMapMappingStrategy( ) {
return nullValueMapMappingStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#nullValuePropertyMappingStrategy}
*/
public GemValue nullValuePropertyMappingStrategy( ) {
return nullValuePropertyMappingStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#nullValueCheckStrategy}
*/
public GemValue nullValueCheckStrategy( ) {
return nullValueCheckStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#mappingControl}
*/
public GemValue mappingControl( ) {
return mappingControl;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#unexpectedValueMappingException}
*/
public GemValue unexpectedValueMappingException( ) {
return unexpectedValueMappingException;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#suppressTimestampInGenerated}
*/
public GemValue suppressTimestampInGenerated( ) {
return suppressTimestampInGenerated;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#builder}
*/
public GemValue builder( ) {
return builder;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#adapterPackage}
*/
public GemValue adapterPackage( ) {
return adapterPackage;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#adapterClassName}
*/
public GemValue adapterClassName( ) {
return adapterClassName;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#mapAdapterClassName}
*/
public GemValue mapAdapterClassName( ) {
return mapAdapterClassName;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#autoConfigPackage}
*/
public GemValue autoConfigPackage( ) {
return autoConfigPackage;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#autoMapperConfigClassName}
*/
public GemValue autoMapperConfigClassName( ) {
return autoMapperConfigClassName;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapperConfigGem#autoMapMapperConfigClassName}
*/
public GemValue autoMapMapperConfigClassName( ) {
return autoMapMapperConfigClassName;
}
@Override
public AnnotationMirror mirror( ) {
return mirror;
}
@Override
public boolean isValid( ) {
return isValid;
}
public static MapperConfigGem instanceOn(Element element) {
return build( element, new BuilderImpl() );
}
public static MapperConfigGem instanceOn(AnnotationMirror mirror ) {
return build( mirror, new BuilderImpl() );
}
public static T build(Element element, Builder builder) {
AnnotationMirror mirror = element.getAnnotationMirrors().stream()
.filter( a -> "io.github.linpeilie.annotations.MapperConfig".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 ( "mapperPackage".equals( methodName ) ) {
builder.setMapperpackage( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.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 ( "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 ( "nullValueIterableMappingStrategy".equals( methodName ) ) {
builder.setNullvalueiterablemappingstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
}
else if ( "nullValueMapMappingStrategy".equals( methodName ) ) {
builder.setNullvaluemapmappingstrategy( 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 ( "nullValueCheckStrategy".equals( methodName ) ) {
builder.setNullvaluecheckstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
}
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 ) );
}
else if ( "suppressTimestampInGenerated".equals( methodName ) ) {
builder.setSuppresstimestampingenerated( 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 ( "adapterPackage".equals( methodName ) ) {
builder.setAdapterpackage( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "adapterClassName".equals( methodName ) ) {
builder.setAdapterclassname( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "mapAdapterClassName".equals( methodName ) ) {
builder.setMapadapterclassname( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "autoConfigPackage".equals( methodName ) ) {
builder.setAutoconfigpackage( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "autoMapperConfigClassName".equals( methodName ) ) {
builder.setAutomapperconfigclassname( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "autoMapMapperConfigClassName".equals( methodName ) ) {
builder.setAutomapmapperconfigclassname( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.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 MapperConfigGem#mapperPackage}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setMapperpackage(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#unmappedSourcePolicy}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setUnmappedsourcepolicy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#unmappedTargetPolicy}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setUnmappedtargetpolicy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#typeConversionPolicy}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setTypeconversionpolicy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#collectionMappingStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setCollectionmappingstrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#nullValueMappingStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setNullvaluemappingstrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#nullValueIterableMappingStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setNullvalueiterablemappingstrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#nullValueMapMappingStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setNullvaluemapmappingstrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#nullValuePropertyMappingStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setNullvaluepropertymappingstrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#nullValueCheckStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setNullvaluecheckstrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#mappingControl}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setMappingcontrol(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#unexpectedValueMappingException}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setUnexpectedvaluemappingexception(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#suppressTimestampInGenerated}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setSuppresstimestampingenerated(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#builder}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setBuilder(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#adapterPackage}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setAdapterpackage(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#adapterClassName}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setAdapterclassname(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#mapAdapterClassName}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setMapadapterclassname(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#autoConfigPackage}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setAutoconfigpackage(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#autoMapperConfigClassName}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setAutomapperconfigclassname(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapperConfigGem#autoMapMapperConfigClassName}
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
Builder setAutomapmapperconfigclassname(GemValue methodName );
/**
* Sets the annotation mirror
*
* @param mirror the mirror which this gem represents
*
* @return the {@link Builder} for this gem, representing {@link MapperConfigGem}
*/
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 mapperPackage;
private GemValue unmappedSourcePolicy;
private GemValue unmappedTargetPolicy;
private GemValue typeConversionPolicy;
private GemValue collectionMappingStrategy;
private GemValue nullValueMappingStrategy;
private GemValue nullValueIterableMappingStrategy;
private GemValue nullValueMapMappingStrategy;
private GemValue nullValuePropertyMappingStrategy;
private GemValue nullValueCheckStrategy;
private GemValue mappingControl;
private GemValue unexpectedValueMappingException;
private GemValue suppressTimestampInGenerated;
private GemValue builder;
private GemValue adapterPackage;
private GemValue adapterClassName;
private GemValue mapAdapterClassName;
private GemValue autoConfigPackage;
private GemValue autoMapperConfigClassName;
private GemValue autoMapMapperConfigClassName;
private AnnotationMirror mirror;
public Builder setMapperpackage(GemValue mapperPackage ) {
this.mapperPackage = mapperPackage;
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 setCollectionmappingstrategy(GemValue collectionMappingStrategy ) {
this.collectionMappingStrategy = collectionMappingStrategy;
return this;
}
public Builder setNullvaluemappingstrategy(GemValue nullValueMappingStrategy ) {
this.nullValueMappingStrategy = nullValueMappingStrategy;
return this;
}
public Builder setNullvalueiterablemappingstrategy(GemValue nullValueIterableMappingStrategy ) {
this.nullValueIterableMappingStrategy = nullValueIterableMappingStrategy;
return this;
}
public Builder setNullvaluemapmappingstrategy(GemValue nullValueMapMappingStrategy ) {
this.nullValueMapMappingStrategy = nullValueMapMappingStrategy;
return this;
}
public Builder setNullvaluepropertymappingstrategy(GemValue nullValuePropertyMappingStrategy ) {
this.nullValuePropertyMappingStrategy = nullValuePropertyMappingStrategy;
return this;
}
public Builder setNullvaluecheckstrategy(GemValue nullValueCheckStrategy ) {
this.nullValueCheckStrategy = nullValueCheckStrategy;
return this;
}
public Builder setMappingcontrol(GemValue mappingControl ) {
this.mappingControl = mappingControl;
return this;
}
public Builder setUnexpectedvaluemappingexception(GemValue unexpectedValueMappingException ) {
this.unexpectedValueMappingException = unexpectedValueMappingException;
return this;
}
public Builder setSuppresstimestampingenerated(GemValue suppressTimestampInGenerated ) {
this.suppressTimestampInGenerated = suppressTimestampInGenerated;
return this;
}
public Builder setBuilder(GemValue builder ) {
this.builder = builder;
return this;
}
public Builder setAdapterpackage(GemValue adapterPackage ) {
this.adapterPackage = adapterPackage;
return this;
}
public Builder setAdapterclassname(GemValue adapterClassName ) {
this.adapterClassName = adapterClassName;
return this;
}
public Builder setMapadapterclassname(GemValue mapAdapterClassName ) {
this.mapAdapterClassName = mapAdapterClassName;
return this;
}
public Builder setAutoconfigpackage(GemValue autoConfigPackage ) {
this.autoConfigPackage = autoConfigPackage;
return this;
}
public Builder setAutomapperconfigclassname(GemValue autoMapperConfigClassName ) {
this.autoMapperConfigClassName = autoMapperConfigClassName;
return this;
}
public Builder setAutomapmapperconfigclassname(GemValue autoMapMapperConfigClassName ) {
this.autoMapMapperConfigClassName = autoMapMapperConfigClassName;
return this;
}
public Builder setMirror( AnnotationMirror mirror ) {
this.mirror = mirror;
return this;
}
public MapperConfigGem build() {
return new MapperConfigGem( this );
}
}
}