Skip to main content

Getting generic parameters class type at runtime

No replies
abc3d
Offline
Joined: 2007-02-05
Points: 0

HI All,
Please see code below...

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class Test<M extends Model> {
    public void printGenericParameterClassType() {       
        Type type = Test.class.getGenericSuperclass();
           System.out.println("GenericSuperclass : " + type);
             System.out.println("GenericSuperclass class : " + type.getClass());
             if(type instanceof ParameterizedType){
                 ParameterizedType pt = (ParameterizedType)type;
                 System.out.println("ParameterizedType : " + pt);
                 System.out.println("ParameterizedType OwnerType : " + pt.getOwnerType());
                 System.out.println("ParameterizedType RawType : " + pt.getRawType());
             }    }
    public static void main(String[] a) {

        Test<Book> t = new Test<Book>();
        t.printGenericParameterClassType();
    }}
class Model{}
class Book extends Model{}

Output is :

GenericInterface : class java.lang.Object
GenericInterface class : class java.lang.Class

What I want is system should print 'Book' class name but actual output is different. can anybody help me get generic class or type at run time.