private XmlToEnumMapper(T[] enumValues) { ImmutableMap.Builder<String, T> mapBuilder = new ImmutableMap.Builder<>(); for (T value : enumValues) { try { XmlEnumValue xmlAnnotation = value .getDeclaringClass() .getField(value.name()) .getAnnotation(XmlEnumValue.class); checkArgumentNotNull(xmlAnnotation, "Cannot map enum value to xml name: " + value); String xmlName = xmlAnnotation.value(); mapBuilder = mapBuilder.put(xmlName, value); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } } map = mapBuilder.build(); }
public static <E extends Enum<E>> String toString(final E value) { final Class<E> enumType = value.getDeclaringClass(); try { final XmlEnumValue xmlEnumValue = enumType.getField(value.name()) .getAnnotation(XmlEnumValue.class); if (xmlEnumValue != null && xmlEnumValue.value() != null) { return xmlEnumValue.value(); } else { return value.name(); } } catch (NoSuchFieldException | SecurityException e1) { throw new RuntimeException(e1); } }
public static <E extends Enum<E>> E valueOf(final Class<E> enumType, final String name) { if (!isNotNullOrEmpty(name)) { return null; } try { for (final E e : enumType.getEnumConstants()) { String ename = e.name(); final XmlEnumValue xmlEnumValue = enumType.getField(ename) .getAnnotation(XmlEnumValue.class); if (xmlEnumValue != null && xmlEnumValue.value() != null) { ename = xmlEnumValue.value(); } if (name.equals(ename)) { return e; } } } catch (NoSuchFieldException | SecurityException e1) { throw new RuntimeException(e1); } throw new IllegalArgumentException("unable to find the value " + name + " in enum " + enumType); }
/** * Build {@link EnumConstant}s and discover/report any error in it. */ protected void calcConstants() { EnumConstantImpl<T,C,F,M> last = null; F[] constants = nav().getEnumConstants(clazz); for( int i=constants.length-1; i>=0; i-- ) { F constant = constants[i]; String name = nav().getFieldName(constant); XmlEnumValue xev = builder.reader.getFieldAnnotation(XmlEnumValue.class, constant, this); String literal; if(xev==null) literal = name; else literal = xev.value(); last = createEnumConstant(name,literal,constant,last); } this.firstConstant = last; }
static public String getEnumValue(Enum<?> value){ Class<?> clazz = value.getClass(); Field field; try { field = clazz.getField(value.name()); } catch(NoSuchFieldException nsfe){ throw new RuntimeException(nsfe); } XmlEnumValue enumValue = field.getAnnotation(XmlEnumValue.class); if(enumValue != null){ return enumValue.value(); } throw new IllegalArgumentException(); }
private <T> String findEnumFieldNameUncached(Class classType, T primValue){ for (Field field: classType.getDeclaredFields()) { XmlEnumValue xmlEnumValue = field.getAnnotation(XmlEnumValue.class); if (xmlEnumValue != null && xmlEnumValue.value().equals(primValue)) { return field.getName(); } } return null; }
public static String findEnumFieldValueUncached(Class classType, String toStringValue){ for (Field field: classType.getDeclaredFields()) { XmlEnumValue xmlEnumValue = field.getAnnotation(XmlEnumValue.class); if (xmlEnumValue != null && field.getName().equals(toStringValue)) { return xmlEnumValue.value(); } } return null; }
/** Read the {@link XmlEnumValue} string off of an enum. */ public static String enumToXml(Enum<?> input) { try { return input.getClass().getField(input.name()).getAnnotation(XmlEnumValue.class).value(); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } }
@XmlEnumValue("2008") BIS_APRIL_2014("2008"), @XmlEnumValue("2014") AB_MAI_2014("2014"), @XmlEnumValue("ohne") OHNE("ohne"), @XmlEnumValue("nicht_noetig") NICHT_NOETIG("nicht_noetig"), @XmlEnumValue("bei_besichtigung") BEI_BESICHTIGUNG("bei_besichtigung");
@XmlEnumValue("for-sale") FOR_SALE("for-sale"), @XmlEnumValue("sale-agreed") SALE_AGREED("sale-agreed"), @XmlEnumValue("sold") SOLD("sold");
/** * 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; }
public void setSubjectTypesSupported(final SubjectIdentifierType... subjectTypesSupported) { this.subjectTypesSupported = new HashSet<>(subjectTypesSupported.length); for (final SubjectIdentifierType subjectType : subjectTypesSupported) { try { this.subjectTypesSupported.add(subjectType.getClass().getField(((Enum<SubjectIdentifierType>) subjectType).name()).getAnnotation(XmlEnumValue.class).value()); } catch (NoSuchFieldException | SecurityException e) { throw new IllegalArgumentException(e); } } }
private <T> Collection<? extends DisplayableValue<T>> parseEnumAllowedValues(QName typeName, ComplexTypeDefinition ctd, XSType xsType) { if (xsType.isSimpleType()) { if (xsType.asSimpleType().isRestriction()) { XSRestrictionSimpleType restriction = xsType.asSimpleType().asRestriction(); List<XSFacet> enumerations = restriction.getDeclaredFacets(XSFacet.FACET_ENUMERATION); List<DisplayableValueImpl<T>> enumValues = new ArrayList<DisplayableValueImpl<T>>( enumerations.size()); for (XSFacet facet : enumerations) { String value = facet.getValue().value; Element descriptionE = SchemaProcessorUtil.getAnnotationElement(facet.getAnnotation(), SCHEMA_DOCUMENTATION); Element appInfo = SchemaProcessorUtil.getAnnotationElement(facet.getAnnotation(), SCHEMA_APP_INFO); Element valueE = null; if (appInfo != null) { NodeList list = appInfo.getElementsByTagNameNS( PrismConstants.A_LABEL.getNamespaceURI(), PrismConstants.A_LABEL.getLocalPart()); if (list.getLength() != 0) { valueE = (Element) list.item(0); } } String label = null; if (valueE != null) { label = valueE.getTextContent(); } else { label = value; } DisplayableValueImpl<T> edv = null; Class compileTimeClass = prismContext.getSchemaRegistry().getCompileTimeClass(typeName); if (ctd != null && !ctd.isRuntimeSchema() && compileTimeClass != null) { String fieldName = null; for (Field field : compileTimeClass.getDeclaredFields()) { XmlEnumValue xmlEnumValue = field.getAnnotation(XmlEnumValue.class); if (xmlEnumValue != null && xmlEnumValue.value() != null && xmlEnumValue.value().equals(value)) { fieldName = field.getName(); } } if (fieldName != null) { T enumValue = (T) Enum.valueOf((Class<Enum>) compileTimeClass, fieldName); edv = new DisplayableValueImpl(enumValue, label, descriptionE != null ? descriptionE.getTextContent() : null); } else { edv = new DisplayableValueImpl(value, label, descriptionE != null ? descriptionE.getTextContent() : null); } } else { edv = new DisplayableValueImpl(value, label, descriptionE != null ? descriptionE.getTextContent() : null); } enumValues.add(edv); } if (enumValues != null && !enumValues.isEmpty()) { return enumValues; } } } return null; }
@XmlEnumValue("wohn") WOHN("wohn"), @XmlEnumValue("nichtwohn") NICHTWOHN("nichtwohn");
@XmlEnumValue("weekly") WEEKLY("weekly"), @XmlEnumValue("monthly") MONTHLY("monthly");
/** * Query the Class type to find a WITH setter method for the indicated field. * <p> * @param type the class type * @param field the field * @return * @throws ClassNotFoundException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException */ private static Object getEnumEntry(Class<?> type, Field field) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { /** * Inspect the WITH setter to determine if the field expects an enumerated * input. */ Method with = SSRFUtility.findWithEnumMethod(type, field); if (with != null) { for (Class<?> parameterType : with.getParameterTypes()) { if (parameterType.isEnum()) { String fieldClassName = parameterType.toString().replace("class ", "").trim(); Class<?> fieldClass = Class.forName(fieldClassName); // throws ClassNotFoundException /** * Get a random instance of the enumerated class type. */ Enum fieldInstance = (Enum) fieldClass.getEnumConstants()[new Random().nextInt(fieldClass.getEnumConstants().length)]; /** * If the instance has an XmlEnumValue annotation then return the XML * value. */ XmlEnumValue xmlEnumValue = fieldInstance.getClass().getAnnotation(XmlEnumValue.class); if (xmlEnumValue != null) { return xmlEnumValue.value(); } /** * Otherwise try to call the value getter. */ for (Method method : fieldClass.getDeclaredMethods()) { if (method.getName().equals("value")) { return method.invoke(fieldInstance); } } /** * Finally just return the numerated instance name. */ return fieldInstance.name(); } } } /** * No enumerated instance found. Return null. */ return null; }
public static JsonObject convertToJson(final Object obj) { final JsonObjectBuilder b = Json.createObjectBuilder(); try { final Class<?> objClass = obj.getClass(); for (final Field field : objClass.getDeclaredFields()) { String name = field.getName(); if (field.getAnnotation(XmlTransient.class) != null) { continue; } final XmlElement xmlElement = field.getAnnotation(XmlElement.class); if (xmlElement != null && xmlElement.name() != null) { name = xmlElement.name(); } field.setAccessible(true); final Object value = field.get(obj); if (value == null) { continue; } if (field.getType() .isEnum()) { final String enumValue = ((Enum<?>) value).name(); final XmlEnumValue xmlEnumValue = ((Enum<?>) value).getDeclaringClass() .getField(enumValue) .getAnnotation(XmlEnumValue.class); if (xmlEnumValue != null && xmlEnumValue.value() != null) { b.add(name, xmlEnumValue.value()); } else { b.add(name, enumValue); } } else if (field.getType() == String.class) { b.add(name, (String) value); } else if (field.getType() == Integer.class) { b.add(name, (Integer) value); } else if (field.getType() == BigInteger.class) { b.add(name, (BigInteger) value); } } return b.build(); } catch (IllegalAccessException | SecurityException | NoSuchFieldException e1) { throw new RuntimeException(e1); } }