Skip to content

Get type of a generic parameter in Java with reflection

Is it possible to get the type of a generic parameter?

An example:

public final class Voodoo {
    public static void chill(List<?> aListWithTypeSpiderMan) {
        // Here I'd like to get the Class-Object 'SpiderMan'
        Class typeOfTheList = ???;

    public static void main(String... args) {
        chill(new ArrayList<SpiderMan>());



One construct, I once stumbled upon looked like

Class<T> persistentClass = (Class<T>)

So there seems to be some reflection-magic around that I unfortunetly don’t fully understand… Sorry.

7 People found this is helpful