mirror of
https://github.com/corda/corda.git
synced 2025-01-09 06:23:04 +00:00
Add Class#isEnum and improve error handling in Enum.valueOf
This commit is contained in:
parent
b1eb4b9718
commit
1db67e463f
@ -37,7 +37,8 @@ import java.security.Permissions;
|
|||||||
import java.security.AllPermission;
|
import java.security.AllPermission;
|
||||||
|
|
||||||
public final class Class <T> implements Type, AnnotatedElement {
|
public final class Class <T> implements Type, AnnotatedElement {
|
||||||
private static final int PrimitiveFlag = 1 << 5;
|
private static final int PrimitiveFlag = 1 << 5;
|
||||||
|
private static final int EnumFlag = 1 << 14;
|
||||||
|
|
||||||
public final VMClass vmClass;
|
public final VMClass vmClass;
|
||||||
|
|
||||||
@ -555,6 +556,10 @@ public final class Class <T> implements Type, AnnotatedElement {
|
|||||||
return (vmClass.vmFlags & PrimitiveFlag) != 0;
|
return (vmClass.vmFlags & PrimitiveFlag) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isEnum() {
|
||||||
|
return getSuperclass() == Enum.class && (vmClass.flags & EnumFlag) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
public URL getResource(String path) {
|
public URL getResource(String path) {
|
||||||
if (! path.startsWith("/")) {
|
if (! path.startsWith("/")) {
|
||||||
String name = new String
|
String name = new String
|
||||||
|
@ -30,7 +30,9 @@ public abstract class Enum<E extends Enum<E>> implements Comparable<E> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) {
|
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) {
|
||||||
if (name == null) throw new NullPointerException();
|
if (name == null) throw new NullPointerException("name");
|
||||||
|
if (!enumType.isEnum())
|
||||||
|
throw new IllegalArgumentException(enumType.getCanonicalName() + " is not an enum.");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Method method = enumType.getMethod("values");
|
Method method = enumType.getMethod("values");
|
||||||
@ -41,10 +43,11 @@ public abstract class Enum<E extends Enum<E>> implements Comparable<E> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new RuntimeException(ex);
|
// Cannot happen
|
||||||
|
throw new Error(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IllegalArgumentException(name);
|
throw new IllegalArgumentException(enumType.getCanonicalName() + "." + name + " is not an enum constant.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public int ordinal() {
|
public int ordinal() {
|
||||||
|
Loading…
Reference in New Issue
Block a user