I would like to determine the class value of a generic type. Is it
possible?
Right now, I'm passing the type as a parameter to the class
constructor:
public class Example<T extends Enum<T>> {
private final Class<T> clazz;
protected Example(Class<T> clazz) {
this.clazz = clazz;
}
public T parse(String text) {
// Can I derive the value of clazz from T?
T value = T.valueOf(clazz, text);
return value;
}
}
But I'm wondering if I can derive the class value of T instead of
using a constructor parameter or is type erasure as play here?
/Martin
Daniel Pitts - 05 Nov 2006 22:17 GMT
> I would like to determine the class value of a generic type. Is it
> possible?
[quoted text clipped - 21 lines]
>
> /Martin
In fact, you cannot. That is the whole point(problem) of erasure.
Even the EnumMap and EnumSet classes require a Class object in their
construction.
Martin Lorentzson - 06 Nov 2006 21:02 GMT
> In fact, you cannot. That is the whole point(problem) of erasure.
OK, I thought I figured as much from the docs but I just wanted to
make sure. Thanks.