Calling Enum.values() on a generic type

Viewed 2824

The goal is to implement a generic Enum translator. I know Enum1 and Enum2 have the same values and in the same order.

The goal is to implement something like this:(this works)

private static Enum1 translateEnum(Enum2 originalEnum) {
    return Enum1.values()[originalEnum.ordinal()];
}

But I have several enums, so I want to make a generic method: (this doesn't work)

private static < T extends Enum<T>,G extends Enum<G>> T translateEnum(G originalEnum) {
    return T.values()[originalEnum.ordinal()];        
}

My problem is that at T.values() the compiler tells me :

The method values() is undefined for the type T

Do you guys have an idea of how I could solve this issue or do you have any alternative idea to my problem?

1 Answers
Related