Maybe this have been answered before, but I did not find it here. Consider the following line of code:
public static <T> T getAs() { ... }
It is possible to obtain the object Class<T>
, by reflection ?
For clarification, im not looking the generic type of a class, like in
class Foo<T> {
public T get() { ...}
}
This can be done in c# at it is really handy, for example
class Foo {
private List<A> items;
public <T extends A> T first() {
Class<T> clazz = //Magic??
foreach (A a : items) {
if (clazz.isInstance(a)) return (T)a;
}
return null;
}
}
Used like this:
SubClass s = fooInst.<SubClass>first();