There are other related questions e.g. 6624113, 3403909, 4516891 but my question is simpler and more specific.
I want to know at runtime what type my class was parameterized with - I want a Class object of the type of the type parameter.  Because of type erasure, the expression T.class doesn't work, and there is no function like typeof(T) in C# to get it.
However, there is some "uber-reflection" available via ParameterizedType and related classes, which gets me almost all of the way there.
import java.lang.reflect.ParameterizedType;
public class MyClass<T> {
    public static void main( String[] args ) {
        new MyClass<Integer>().printTypeParam();
    }
    public void printTypeParam() {
        class DummyT extends MyClass<T> {}
        class DummyString extends MyClass<String> {}
        ParameterizedType ptT =
            ((ParameterizedType) DummyT.class.getGenericSuperclass() );
        ParameterizedType ptString =
            ((ParameterizedType) DummyString.class.getGenericSuperclass() );
        System.out.println( "DummyT: " + ptT + " " + ptT.getActualTypeArguments()[0] );
        System.out.println( "DummyString: " + ptString + " " + ptString.getActualTypeArguments()[0] );
    }
}
When I run the above I see this output:
DummyT: MyClass<T> T
DummyString: MyClass<java.lang.String> class java.lang.String
As you can see, this works for when the type arguments are known at compile-time at the line of code containing the call to getGenericSuperClass, but where that call is itself dealing with generics it simply prints the name of the type parameter.
Is there any way I can get the first line to print java.lang.Integer instead of T?
 
     
    