org.mapstruct.ap.internal.gem.MapMappingGem 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 MapMappingGem implements Gem {
private final GemValue keyDateFormat;
private final GemValue valueDateFormat;
private final GemValue keyNumberFormat;
private final GemValue valueNumberFormat;
private final GemValue> keyQualifiedBy;
private final GemValue> keyQualifiedByName;
private final GemValue> valueQualifiedBy;
private final GemValue> valueQualifiedByName;
private final GemValue keyTargetType;
private final GemValue valueTargetType;
private final GemValue nullValueMappingStrategy;
private final GemValue keyMappingControl;
private final GemValue valueMappingControl;
private final boolean isValid;
private final AnnotationMirror mirror;
private MapMappingGem( BuilderImpl builder ) {
this.keyDateFormat = builder.keyDateFormat;
this.valueDateFormat = builder.valueDateFormat;
this.keyNumberFormat = builder.keyNumberFormat;
this.valueNumberFormat = builder.valueNumberFormat;
this.keyQualifiedBy = builder.keyQualifiedBy;
this.keyQualifiedByName = builder.keyQualifiedByName;
this.valueQualifiedBy = builder.valueQualifiedBy;
this.valueQualifiedByName = builder.valueQualifiedByName;
this.keyTargetType = builder.keyTargetType;
this.valueTargetType = builder.valueTargetType;
this.nullValueMappingStrategy = builder.nullValueMappingStrategy;
this.keyMappingControl = builder.keyMappingControl;
this.valueMappingControl = builder.valueMappingControl;
isValid = ( this.keyDateFormat != null ? this.keyDateFormat.isValid() : false )
&& ( this.valueDateFormat != null ? this.valueDateFormat.isValid() : false )
&& ( this.keyNumberFormat != null ? this.keyNumberFormat.isValid() : false )
&& ( this.valueNumberFormat != null ? this.valueNumberFormat.isValid() : false )
&& ( this.keyQualifiedBy != null ? this.keyQualifiedBy.isValid() : false )
&& ( this.keyQualifiedByName != null ? this.keyQualifiedByName.isValid() : false )
&& ( this.valueQualifiedBy != null ? this.valueQualifiedBy.isValid() : false )
&& ( this.valueQualifiedByName != null ? this.valueQualifiedByName.isValid() : false )
&& ( this.keyTargetType != null ? this.keyTargetType.isValid() : false )
&& ( this.valueTargetType != null ? this.valueTargetType.isValid() : false )
&& ( this.nullValueMappingStrategy != null ? this.nullValueMappingStrategy.isValid() : false )
&& ( this.keyMappingControl != null ? this.keyMappingControl.isValid() : false )
&& ( this.valueMappingControl != null ? this.valueMappingControl.isValid() : false );
mirror = builder.mirror;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#keyDateFormat}
*/
public GemValue keyDateFormat( ) {
return keyDateFormat;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#valueDateFormat}
*/
public GemValue valueDateFormat( ) {
return valueDateFormat;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#keyNumberFormat}
*/
public GemValue keyNumberFormat( ) {
return keyNumberFormat;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#valueNumberFormat}
*/
public GemValue valueNumberFormat( ) {
return valueNumberFormat;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#keyQualifiedBy}
*/
public GemValue> keyQualifiedBy( ) {
return keyQualifiedBy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#keyQualifiedByName}
*/
public GemValue> keyQualifiedByName( ) {
return keyQualifiedByName;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#valueQualifiedBy}
*/
public GemValue> valueQualifiedBy( ) {
return valueQualifiedBy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#valueQualifiedByName}
*/
public GemValue> valueQualifiedByName( ) {
return valueQualifiedByName;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#keyTargetType}
*/
public GemValue keyTargetType( ) {
return keyTargetType;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#valueTargetType}
*/
public GemValue valueTargetType( ) {
return valueTargetType;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#nullValueMappingStrategy}
*/
public GemValue nullValueMappingStrategy( ) {
return nullValueMappingStrategy;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#keyMappingControl}
*/
public GemValue keyMappingControl( ) {
return keyMappingControl;
}
/**
* accessor
*
* @return the {@link GemValue} for {@link MapMappingGem#valueMappingControl}
*/
public GemValue valueMappingControl( ) {
return valueMappingControl;
}
@Override
public AnnotationMirror mirror( ) {
return mirror;
}
@Override
public boolean isValid( ) {
return isValid;
}
public static MapMappingGem instanceOn(Element element) {
return build( element, new BuilderImpl() );
}
public static MapMappingGem 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.MapMapping".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 ( "keyDateFormat".equals( methodName ) ) {
builder.setKeydateformat( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "valueDateFormat".equals( methodName ) ) {
builder.setValuedateformat( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "keyNumberFormat".equals( methodName ) ) {
builder.setKeynumberformat( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "valueNumberFormat".equals( methodName ) ) {
builder.setValuenumberformat( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "keyQualifiedBy".equals( methodName ) ) {
builder.setKeyqualifiedby( GemValue.createArray( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
}
else if ( "keyQualifiedByName".equals( methodName ) ) {
builder.setKeyqualifiedbyname( GemValue.createArray( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "valueQualifiedBy".equals( methodName ) ) {
builder.setValuequalifiedby( GemValue.createArray( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
}
else if ( "valueQualifiedByName".equals( methodName ) ) {
builder.setValuequalifiedbyname( GemValue.createArray( values.get( methodName ), defaultValues.get( methodName ), String.class ) );
}
else if ( "keyTargetType".equals( methodName ) ) {
builder.setKeytargettype( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
}
else if ( "valueTargetType".equals( methodName ) ) {
builder.setValuetargettype( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
}
else if ( "nullValueMappingStrategy".equals( methodName ) ) {
builder.setNullvaluemappingstrategy( GemValue.createEnum( values.get( methodName ), defaultValues.get( methodName ) ) );
}
else if ( "keyMappingControl".equals( methodName ) ) {
builder.setKeymappingcontrol( GemValue.create( values.get( methodName ), defaultValues.get( methodName ), TypeMirror.class ) );
}
else if ( "valueMappingControl".equals( methodName ) ) {
builder.setValuemappingcontrol( 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 MapMappingGem#keyDateFormat}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setKeydateformat(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapMappingGem#valueDateFormat}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setValuedateformat(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapMappingGem#keyNumberFormat}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setKeynumberformat(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapMappingGem#valueNumberFormat}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setValuenumberformat(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapMappingGem#keyQualifiedBy}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setKeyqualifiedby(GemValue> methodName );
/**
* Sets the {@link GemValue} for {@link MapMappingGem#keyQualifiedByName}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setKeyqualifiedbyname(GemValue> methodName );
/**
* Sets the {@link GemValue} for {@link MapMappingGem#valueQualifiedBy}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setValuequalifiedby(GemValue> methodName );
/**
* Sets the {@link GemValue} for {@link MapMappingGem#valueQualifiedByName}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setValuequalifiedbyname(GemValue> methodName );
/**
* Sets the {@link GemValue} for {@link MapMappingGem#keyTargetType}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setKeytargettype(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapMappingGem#valueTargetType}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setValuetargettype(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapMappingGem#nullValueMappingStrategy}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setNullvaluemappingstrategy(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapMappingGem#keyMappingControl}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setKeymappingcontrol(GemValue methodName );
/**
* Sets the {@link GemValue} for {@link MapMappingGem#valueMappingControl}
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
Builder setValuemappingcontrol(GemValue methodName );
/**
* Sets the annotation mirror
*
* @param mirror the mirror which this gem represents
*
* @return the {@link Builder} for this gem, representing {@link MapMappingGem}
*/
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 keyDateFormat;
private GemValue valueDateFormat;
private GemValue keyNumberFormat;
private GemValue valueNumberFormat;
private GemValue> keyQualifiedBy;
private GemValue> keyQualifiedByName;
private GemValue> valueQualifiedBy;
private GemValue> valueQualifiedByName;
private GemValue keyTargetType;
private GemValue valueTargetType;
private GemValue nullValueMappingStrategy;
private GemValue keyMappingControl;
private GemValue valueMappingControl;
private AnnotationMirror mirror;
public Builder setKeydateformat(GemValue keyDateFormat ) {
this.keyDateFormat = keyDateFormat;
return this;
}
public Builder setValuedateformat(GemValue valueDateFormat ) {
this.valueDateFormat = valueDateFormat;
return this;
}
public Builder setKeynumberformat(GemValue keyNumberFormat ) {
this.keyNumberFormat = keyNumberFormat;
return this;
}
public Builder setValuenumberformat(GemValue valueNumberFormat ) {
this.valueNumberFormat = valueNumberFormat;
return this;
}
public Builder setKeyqualifiedby(GemValue> keyQualifiedBy ) {
this.keyQualifiedBy = keyQualifiedBy;
return this;
}
public Builder setKeyqualifiedbyname(GemValue> keyQualifiedByName ) {
this.keyQualifiedByName = keyQualifiedByName;
return this;
}
public Builder setValuequalifiedby(GemValue> valueQualifiedBy ) {
this.valueQualifiedBy = valueQualifiedBy;
return this;
}
public Builder setValuequalifiedbyname(GemValue> valueQualifiedByName ) {
this.valueQualifiedByName = valueQualifiedByName;
return this;
}
public Builder setKeytargettype(GemValue keyTargetType ) {
this.keyTargetType = keyTargetType;
return this;
}
public Builder setValuetargettype(GemValue valueTargetType ) {
this.valueTargetType = valueTargetType;
return this;
}
public Builder setNullvaluemappingstrategy(GemValue nullValueMappingStrategy ) {
this.nullValueMappingStrategy = nullValueMappingStrategy;
return this;
}
public Builder setKeymappingcontrol(GemValue keyMappingControl ) {
this.keyMappingControl = keyMappingControl;
return this;
}
public Builder setValuemappingcontrol(GemValue valueMappingControl ) {
this.valueMappingControl = valueMappingControl;
return this;
}
public Builder setMirror( AnnotationMirror mirror ) {
this.mirror = mirror;
return this;
}
public MapMappingGem build() {
return new MapMappingGem( this );
}
}
}