There is a solution actually, by applying the "anonymous class" trick and the ideas from the Super Type Tokens:
public final class Voodoo {
public static void chill(final List<?> aListWithSomeType) {
// Here I'd like to get the Class-Object 'SpiderMan'
System.out.println(aListWithSomeType.getClass().getGenericSuperclass());
System.out.println(((ParameterizedType) aListWithSomeType
.getClass()
.getGenericSuperclass()).getActualTypeArguments()[0]);
}
public static void main(String... args) {
chill(new ArrayList<SpiderMan>() {});
}
}
class SpiderMan {
}
The trick lies in the creation of an anonymous class, new ArrayList<SpiderMan>() {}
, in the place of the original (simple) new ArrayList<SpiderMan>()
. The use of an anoymous class (if possible) ensures that the compiler retains information about the type argument SpiderMan
given to the type parameter List<?>
. VoilĂ !