Search in sources :

Example 1 with XmlEnumValue

use of javax.xml.bind.annotation.XmlEnumValue in project enunciate by stoicflame.

the class EnumTypeDefinition method loadEnumValues.

protected List<EnumValue> loadEnumValues() {
    List<VariableElement> enumConstants = enumValues();
    List<EnumValue> enumValueMap = new ArrayList<EnumValue>();
    HashSet<String> enumValues = new HashSet<String>(enumConstants.size());
    for (VariableElement enumConstant : enumConstants) {
        if (isIgnored(enumConstant)) {
            continue;
        }
        String value = enumConstant.getSimpleName().toString();
        if (context.isHonorJaxb()) {
            XmlEnumValue enumValue = enumConstant.getAnnotation(XmlEnumValue.class);
            if (enumValue != null) {
                value = enumValue.value();
            }
        }
        if (context.isHonorGson()) {
            AnnotationValue av = JacksonUtil.findAnnotationValueByName(JacksonUtil.findAnnotationByClassName(enumConstant.getAnnotationMirrors(), JacksonUtil.GSON_SERIALIZED_NAME_CLASS), "value");
            if (av != null) {
                value = (String) av.getValue();
            }
        }
        JsonProperty jsonProperty = enumConstant.getAnnotation(JsonProperty.class);
        if (jsonProperty != null) {
            value = jsonProperty.value();
        }
        if (!enumValues.add(value)) {
            throw new EnunciateException(getQualifiedName() + ": duplicate enum value: " + value);
        }
        enumValueMap.add(new EnumValue(this, enumConstant, enumConstant.getSimpleName().toString(), value));
    }
    return enumValueMap;
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) EnunciateException(com.webcohesion.enunciate.EnunciateException) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue) AnnotationValue(javax.lang.model.element.AnnotationValue) VariableElement(javax.lang.model.element.VariableElement) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue)

Example 2 with XmlEnumValue

use of javax.xml.bind.annotation.XmlEnumValue in project enunciate by stoicflame.

the class EnumTypeDefinition method loadEnumValues.

protected List<EnumValue> loadEnumValues() {
    List<VariableElement> enumConstants = enumValues();
    List<EnumValue> enumValues = new ArrayList<EnumValue>();
    HashSet<String> enumValueNames = new HashSet<String>(enumConstants.size());
    for (VariableElement enumConstant : enumConstants) {
        if (isIgnored(enumConstant)) {
            continue;
        }
        String value = enumConstant.getSimpleName().toString();
        XmlEnumValue enumValue = enumConstant.getAnnotation(XmlEnumValue.class);
        if (enumValue != null) {
            value = enumValue.value();
        }
        if (!enumValueNames.add(value)) {
            throw new EnunciateException(getQualifiedName() + ": duplicate enum value: " + value);
        }
        enumValues.add(new EnumValue(this, enumConstant, enumConstant.getSimpleName().toString(), value));
    }
    return enumValues;
}
Also used : EnunciateException(com.webcohesion.enunciate.EnunciateException) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue) VariableElement(javax.lang.model.element.VariableElement) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue)

Example 3 with XmlEnumValue

use of javax.xml.bind.annotation.XmlEnumValue in project zm-mailbox by Zimbra.

the class ZmPairAnnotationIntrospector method findEnumValues.

// since 2.7
@Override
public String[] findEnumValues(Class<?> enumType, Enum<?>[] enumValues, String[] names) {
    HashMap<String, String> expl = null;
    for (Field f : ClassUtil.getDeclaredFields(enumType)) {
        if (!f.isEnumConstant()) {
            continue;
        }
        XmlEnumValue enumValue = f.getAnnotation(XmlEnumValue.class);
        if (enumValue == null) {
            continue;
        }
        String n = enumValue.value();
        if (expl == null) {
            expl = new HashMap<String, String>();
        }
        expl.put(f.getName(), n);
    }
    // and then stitch them together if and as necessary
    if (expl != null) {
        int end = enumValues.length;
        for (int i = 0; i < end; ++i) {
            String defName = enumValues[i].name();
            String explValue = expl.get(defName);
            if (explValue != null) {
                names[i] = explValue;
            }
        }
    }
    return names;
}
Also used : Field(java.lang.reflect.Field) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue)

Example 4 with XmlEnumValue

use of javax.xml.bind.annotation.XmlEnumValue in project cxf by apache.

the class AnnotationReader method getEnumValue.

public static String getEnumValue(Enum<?> enumConstant) {
    @SuppressWarnings("rawtypes") Class<? extends Enum> enumClass = enumConstant.getClass();
    try {
        Field constantField = enumClass.getDeclaredField(enumConstant.name());
        XmlEnumValue constantValueAnnotation = constantField.getAnnotation(XmlEnumValue.class);
        if (constantValueAnnotation == null) {
            return null;
        }
        return constantValueAnnotation.value();
    } catch (NoSuchFieldException e) {
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue)

Example 5 with XmlEnumValue

use of javax.xml.bind.annotation.XmlEnumValue in project axis-axis2-java-core by apache.

the class XMLRootElementUtil method getEnumValue.

/**
 * @param clazz
 * @return namespace of root element qname or null if this is object does not represent a root element
 */
public static String getEnumValue(Enum myEnum) {
    Field f;
    String value;
    try {
        f = myEnum.getClass().getField(myEnum.name());
        f.setAccessible(true);
        XmlEnumValue xev = (XmlEnumValue) getAnnotation(f, XmlEnumValue.class);
        if (xev == null) {
            value = f.getName();
        } else {
            value = xev.value();
        }
    } catch (SecurityException e) {
        value = null;
    } catch (NoSuchFieldException e) {
        value = null;
    }
    return value;
}
Also used : Field(java.lang.reflect.Field) XmlEnumValue(javax.xml.bind.annotation.XmlEnumValue)

Aggregations

XmlEnumValue (javax.xml.bind.annotation.XmlEnumValue)9 Field (java.lang.reflect.Field)6 EnunciateException (com.webcohesion.enunciate.EnunciateException)3 VariableElement (javax.lang.model.element.VariableElement)3 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 ArrayList (java.util.ArrayList)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1