public EnumProperty(Class<E> e, JAXBContextImpl context) throws JAXBException { _enum = e; _typeName = JAXBUtil.getXmlSchemaDatatype(_enum, context); try { XmlEnum xmlEnum = _enum.getAnnotation(XmlEnum.class); _base = xmlEnum.value(); _baseName = JAXBUtil.getXmlSchemaDatatype(_base, context); // XXX check that base is an XML simple type Field[] fields = _enum.getFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; // We only care about actual enum fields if (!fields[i].isEnumConstant()) continue; XmlEnumValue xmlEnumValue = f.getAnnotation(XmlEnumValue.class); E value = Enum.valueOf(_enum, f.getName()); if (xmlEnumValue != null) { _valueMap.put(xmlEnumValue.value(), value); _nameMap.put(value, xmlEnumValue.value()); } else { _valueMap.put(f.getName(), value); _nameMap.put(value, f.getName()); } } } catch (Exception ex) { throw new JAXBException(L.l("Error while introspecting enum {0}", e.getName()), ex); } }
/** * Extract from an Enum the {@link javax.xml.bind.annotation.XmlEnumValue} that are associated * with its fields. * * @param clazz The class that is to be introspected * @return A map that maps {@link javax.xml.bind.annotation.XmlEnumValue#value()} to the enum name * itself. */ protected Map<String, String> extractXmlValueFromEnumAnnotations(Class clazz) { Map<String, String> annotationToName = Maps.newHashMap(); Field[] fields = clazz.getFields(); for (Field field : fields) { if (field.isAnnotationPresent(XmlEnumValue.class)) { XmlEnumValue annotation = field.getAnnotation(XmlEnumValue.class); annotationToName.put(annotation.value(), field.getName()); } } return annotationToName; }
protected Map<String, String> loadEnumValues() { List<VariableElement> enumConstants = getEnumConstants(); Map<String, String> enumValueMap = new LinkedHashMap<String, String>(); HashSet<String> enumValues = new HashSet<String>(enumConstants.size()); for (VariableElement enumConstant : enumConstants) { String value = enumConstant.getSimpleName().toString(); if (context.isHonorJaxb()) { XmlEnumValue enumValue = enumConstant.getAnnotation(XmlEnumValue.class); if (enumValue != null) { value = enumValue.value(); } } if (!enumValues.add(value)) { throw new EnunciateException(getQualifiedName() + ": duplicate enum value: " + value); } enumValueMap.put(enumConstant.getSimpleName().toString(), value); } return enumValueMap; }