/** * Finds an instance of an Enum constant on a class. Useful for safely * getting the value of an enum constant without an exception being thrown * like the Enum.valueOf() method causes. Searches for enum constant * where case is sensitive. */ public static Object findEnumConstant(Class<?> type, String constantName) { return findEnumConstant(type, constantName, true); }
/** * Finds an instance of an Enum constant on a class. Useful for safely * getting the value of an enum constant without an exception being thrown * like the Enum.valueOf() method causes. Searches for enum constant * where case is sensitive. */ public static Object findEnumConstant(Class<?> type, String constantName) { return findEnumConstant(type, constantName, true); }
/** * Converts the string value into an Object of the Class type. Will either * delegate conversion to a PropertyConverter or will handle creating enums * directly. * * @param string0 The string value to convert * @param type The Class type to convert it into * @return A new Object converted from the String value */ static public <E> E convert(String s, Class<E> type) throws ConversionException { // if enum, handle differently if (type.isEnum()) { Object obj = ClassUtil.findEnumConstant(type, s); if (obj == null) { throw new ConversionException("Invalid constant [" + s + "] used, supported values [" + toListString(type.getEnumConstants()) + "]"); } return (E)obj; // else, handle normally } else { TypeConverter converter = REGISTRY.get(type); if (converter == null) { throw new ConversionException("The type [" + type.getSimpleName() + "] is not supported"); } return (E)converter.convert(s); } }