org.mapstruct.ap.internal.prism.MapMappingPrism Maven / Gradle / Ivy
package org.mapstruct.ap.internal.prism;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.VariableElement;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.type.TypeMirror;
import net.java.dev.hickory.prism.internal.*;
import java.util.HashMap;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;
/** A Prism representing an {@code @org.mapstruct.MapMapping} annotation.
*/
public class MapMappingPrism {
/** store prism value of keyDateFormat */
private String _keyDateFormat;
/** store prism value of valueDateFormat */
private String _valueDateFormat;
/** store prism value of keyQualifiedBy */
private List _keyQualifiedBy;
/** store prism value of keyQualifiedByName */
private List _keyQualifiedByName;
/** store prism value of valueQualifiedBy */
private List _valueQualifiedBy;
/** store prism value of valueQualifiedByName */
private List _valueQualifiedByName;
/** store prism value of keyTargetType */
private TypeMirror _keyTargetType;
/** store prism value of valueTargetType */
private TypeMirror _valueTargetType;
/** store prism value of nullValueMappingStrategy */
private String _nullValueMappingStrategy;
/**
* An instance of the Values inner class whose
* methods return the AnnotationValues used to build this prism.
* Primarily intended to support using Messager.
*/
public final Values values;
/** Return a prism representing the {@code @org.mapstruct.MapMapping} annotation on 'e'.
* similar to {@code e.getAnnotation(org.mapstruct.MapMapping.class)} except that
* an instance of this class rather than an instance of {@code org.mapstruct.MapMapping}
* is returned.
*/
public static MapMappingPrism getInstanceOn(Element e) {
AnnotationMirror m = getMirror("org.mapstruct.MapMapping",e);
if(m == null) return null;
return getInstance(m);
}
/** Return a prism of the {@code @org.mapstruct.MapMapping} annotation whose mirror is mirror.
*/
public static MapMappingPrism getInstance(AnnotationMirror mirror) {
return new MapMappingPrism(mirror);
}
private MapMappingPrism(AnnotationMirror mirror) {
for(ExecutableElement key : mirror.getElementValues().keySet()) {
memberValues.put(key.getSimpleName().toString(),mirror.getElementValues().get(key));
}
for(ExecutableElement member : ElementFilter.methodsIn(mirror.getAnnotationType().asElement().getEnclosedElements())) {
defaults.put(member.getSimpleName().toString(),member.getDefaultValue());
}
_keyDateFormat = getValue("keyDateFormat",String.class);
_valueDateFormat = getValue("valueDateFormat",String.class);
_keyQualifiedBy = getArrayValues("keyQualifiedBy",TypeMirror.class);
_keyQualifiedByName = getArrayValues("keyQualifiedByName",String.class);
_valueQualifiedBy = getArrayValues("valueQualifiedBy",TypeMirror.class);
_valueQualifiedByName = getArrayValues("valueQualifiedByName",String.class);
_keyTargetType = getValue("keyTargetType",TypeMirror.class);
_valueTargetType = getValue("valueTargetType",TypeMirror.class);
VariableElement nullValueMappingStrategyMirror = getValue("nullValueMappingStrategy",VariableElement.class);
valid = valid && nullValueMappingStrategyMirror != null;
_nullValueMappingStrategy = nullValueMappingStrategyMirror == null ? null : nullValueMappingStrategyMirror.getSimpleName().toString();
this.values = new Values(memberValues);
this.mirror = mirror;
this.isValid = valid;
}
/**
* Returns a String representing the value of the {@code java.lang.String keyDateFormat()} member of the Annotation.
* @see org.mapstruct.MapMapping#keyDateFormat()
*/
public String keyDateFormat() { return _keyDateFormat; }
/**
* Returns a String representing the value of the {@code java.lang.String valueDateFormat()} member of the Annotation.
* @see org.mapstruct.MapMapping#valueDateFormat()
*/
public String valueDateFormat() { return _valueDateFormat; }
/**
* Returns a List representing the value of the {@code keyQualifiedBy()} member of the Annotation.
* @see org.mapstruct.MapMapping#keyQualifiedBy()
*/
public List keyQualifiedBy() { return _keyQualifiedBy; }
/**
* Returns a List representing the value of the {@code keyQualifiedByName()} member of the Annotation.
* @see org.mapstruct.MapMapping#keyQualifiedByName()
*/
public List keyQualifiedByName() { return _keyQualifiedByName; }
/**
* Returns a List representing the value of the {@code valueQualifiedBy()} member of the Annotation.
* @see org.mapstruct.MapMapping#valueQualifiedBy()
*/
public List valueQualifiedBy() { return _valueQualifiedBy; }
/**
* Returns a List representing the value of the {@code valueQualifiedByName()} member of the Annotation.
* @see org.mapstruct.MapMapping#valueQualifiedByName()
*/
public List valueQualifiedByName() { return _valueQualifiedByName; }
/**
* Returns a TypeMirror representing the value of the {@code java.lang.Class> keyTargetType()} member of the Annotation.
* @see org.mapstruct.MapMapping#keyTargetType()
*/
public TypeMirror keyTargetType() { return _keyTargetType; }
/**
* Returns a TypeMirror representing the value of the {@code java.lang.Class> valueTargetType()} member of the Annotation.
* @see org.mapstruct.MapMapping#valueTargetType()
*/
public TypeMirror valueTargetType() { return _valueTargetType; }
/**
* Returns a String representing the value of the {@code org.mapstruct.NullValueMappingStrategy nullValueMappingStrategy()} member of the Annotation.
* @see org.mapstruct.MapMapping#nullValueMappingStrategy()
*/
public String nullValueMappingStrategy() { return _nullValueMappingStrategy; }
/**
* Determine whether the underlying AnnotationMirror has no errors.
* True if the underlying AnnotationMirror has no errors.
* When true is returned, none of the methods will return null.
* When false is returned, a least one member will either return null, or another
* prism that is not valid.
*/
public final boolean isValid;
/**
* The underlying AnnotationMirror of the annotation
* represented by this Prism.
* Primarily intended to support using Messager.
*/
public final AnnotationMirror mirror;
/**
* A class whose members corespond to those of org.mapstruct.MapMapping
* but which each return the AnnotationValue corresponding to
* that member in the model of the annotations. Returns null for
* defaulted members. Used for Messager, so default values are not useful.
*/
public static class Values {
private Map values;
private Values(Map values) {
this.values = values;
}
/** Return the AnnotationValue corresponding to the keyDateFormat()
* member of the annotation, or null when the default value is implied.
*/
public AnnotationValue keyDateFormat(){ return values.get("keyDateFormat");}
/** Return the AnnotationValue corresponding to the valueDateFormat()
* member of the annotation, or null when the default value is implied.
*/
public AnnotationValue valueDateFormat(){ return values.get("valueDateFormat");}
/** Return the AnnotationValue corresponding to the keyQualifiedBy()
* member of the annotation, or null when the default value is implied.
*/
public AnnotationValue keyQualifiedBy(){ return values.get("keyQualifiedBy");}
/** Return the AnnotationValue corresponding to the keyQualifiedByName()
* member of the annotation, or null when the default value is implied.
*/
public AnnotationValue keyQualifiedByName(){ return values.get("keyQualifiedByName");}
/** Return the AnnotationValue corresponding to the valueQualifiedBy()
* member of the annotation, or null when the default value is implied.
*/
public AnnotationValue valueQualifiedBy(){ return values.get("valueQualifiedBy");}
/** Return the AnnotationValue corresponding to the valueQualifiedByName()
* member of the annotation, or null when the default value is implied.
*/
public AnnotationValue valueQualifiedByName(){ return values.get("valueQualifiedByName");}
/** Return the AnnotationValue corresponding to the keyTargetType()
* member of the annotation, or null when the default value is implied.
*/
public AnnotationValue keyTargetType(){ return values.get("keyTargetType");}
/** Return the AnnotationValue corresponding to the valueTargetType()
* member of the annotation, or null when the default value is implied.
*/
public AnnotationValue valueTargetType(){ return values.get("valueTargetType");}
/** Return the AnnotationValue corresponding to the nullValueMappingStrategy()
* member of the annotation, or null when the default value is implied.
*/
public AnnotationValue nullValueMappingStrategy(){ return values.get("nullValueMappingStrategy");}
}
private Map defaults = new HashMap(10);
private Map memberValues = new HashMap(10);
private boolean valid = true;
private T getValue(String name, Class clazz) {
T result = MapMappingPrism.getValue(memberValues,defaults,name,clazz);
if(result == null) valid = false;
return result;
}
private List getArrayValues(String name, final Class clazz) {
List result = MapMappingPrism.getArrayValues(memberValues,defaults,name,clazz);
if(result == null) valid = false;
return result;
}
private static AnnotationMirror getMirror(String fqn, Element target) {
for (AnnotationMirror m :target.getAnnotationMirrors()) {
CharSequence mfqn = ((TypeElement)m.getAnnotationType().asElement()).getQualifiedName();
if(fqn.contentEquals(mfqn)) return m;
}
return null;
}
private static T getValue(Map memberValues, Map defaults, String name, Class clazz) {
AnnotationValue av = memberValues.get(name);
if(av == null) av = defaults.get(name);
if(av == null) {
return null;
}
if(clazz.isInstance(av.getValue())) return clazz.cast(av.getValue());
return null;
}
private static List getArrayValues(Map memberValues, Map defaults, String name, final Class clazz) {
AnnotationValue av = memberValues.get(name);
if(av == null) av = defaults.get(name);
if(av == null) {
return null;
}
if(av.getValue() instanceof List) {
List result = new ArrayList();
for(AnnotationValue v : getValueAsList(av)) {
if(clazz.isInstance(v.getValue())) {
result.add(clazz.cast(v.getValue()));
} else{
return null;
}
}
return result;
} else {
return null;
}
}
@SuppressWarnings("unchecked")
private static List getValueAsList(AnnotationValue av) {
return (List)av.getValue();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy