I have a generics class, Foo
. In a method of Foo
, I want to get the class instance of type T
, but I just can\'t call T.
Many poeple don't know this trick! Actually, I just found it today! It works like a dream! Just check this example out:
public static void main(String[] args) throws NoSuchMethodException {
Date d=new Date(); //Or anything you want!
printMethods(i);
}
public static void printMethods(T t){
Class clazz= (Class) t.getClass(); // There you go!
for ( Method m : clazz.getMethods()){
System.out.println( m.getName() );
}
}