Get enum value from enum type and ordinal
field.getType().getEnumConstants()[ordinal]
suffices. One line; straightforward enough.
ExampleTypeEnum value = ExampleTypeEnum.values()[ordinal]
To get what you want you need to invoke YourEnum.values()[ordinal]
. You can do it with reflection like this:
public static <E extends Enum<E>> E decode(Field field, int ordinal) {
try {
Class<?> myEnum = field.getType();
Method valuesMethod = myEnum.getMethod("values");
Object arrayWithEnumValies = valuesMethod.invoke(myEnum);
return (E) Array.get(arrayWithEnumValies, ordinal);
} catch (NoSuchMethodException | SecurityException
| IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
UPDATE
As @LouisWasserman pointed in his comment there is much simpler way
public static <E extends Enum<E>> E decode(Field field, int ordinal) {
return (E) field.getType().getEnumConstants()[ordinal];
}