Java's generics system uses an approach called "type erasure". This means that the type parameters, such as T, only exist in the source code, not the actual compiled program. As such, there is no way to know what type T represents when the method is called since the whole notion of T doesn't even exist during runtime.
Unfortunately, the only way for the method to get a Class<T> instance is to actually pass it as a parameter:
MyEnum e = getValue(MyEnum.class, "MY_VALUE");
Once you have the Class<T> instance, you can call the getEnumConstants() method on it to get an array of all the enum values. You can then search through it for the value with the desired name:
T[] constants = enumType.getEnumConstants();
for (T c : constants) {
if (c.name().equals(value)) {
return c;
}
}
throw new IllegalArgumentException("No such value: " + value);
Class<T>parameter.Enum.valueOf(clazz, value)directly.<T extends Enum<T>>