The Source for Java Technology Collaboration
User: Password:



Start New Message Delete Post a Reply

Article: 
 Reflection on Tiger
Subject:  How to get the current type of T in GenericsTest?
Date:  2005-11-20 05:49:53
From:  davidleal


Please could you explain how to get information about a the class T on the GenericsTest definition class?

For example in some way to do:
Class<T> clazz = T.class;

I know it doesn't compile. I have tested with:



public class GenericsTest<T extends Thread>
private Class classT;
GenericsTest () {
ParameterizedType paramType =
(ParameterizedType) (getClass().getGenericInterfaces()[0]);
this.classT = (Class) (paramType.getActualTypeArguments()[0]);
System.out.println("classT=" + classT);
}
public static void main(String[] args) {
GenericTests<Thread> b = new GenericTests<Thread>();
}
}


after execution, I get an java.lang.ClassCastException.

Please do you have any suggestion about getting the current value of T inside GenericsTest, for example: java.lang.Thread.class

Thank you,

David

 Feed java.net RSS Feeds