throw IllegalArgumentException from Enum.valueOf if name does not match any value

This commit is contained in:
Joel Dice 2009-08-13 08:57:58 -06:00
parent fb37f48237
commit 864a28f2ce

View File

@ -26,20 +26,21 @@ public abstract class Enum<E extends Enum<E>> implements Comparable<E> {
}
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) {
if (name != null) {
try {
Method method = enumType.getMethod("values");
Enum values[] = (Enum[])(method.invoke(null));
for (Enum value : values) {
if (name.equals(value.name)) {
return (T) value;
}
if (name == null) throw new NullPointerException();
try {
Method method = enumType.getMethod("values");
Enum values[] = (Enum[]) (method.invoke(null));
for (Enum value: values) {
if (name.equals(value.name)) {
return (T) value;
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return null;
throw new IllegalArgumentException(name);
}
public int ordinal() {