[转载] Java获取泛型T的类型 T.class
參考鏈接: Java中的抽象
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
?
public class Main{
? ? public static void main(String[] args)
? ? {
? ? ? ? Foo<String> foo = new Foo<String>(){};
? ? ? ? // 在類的外部這樣獲取
? ? ? ? Type type = ((ParameterizedType)foo.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
? ? ? ? System.out.println(type);
? ? ? ? // 在類的內部這樣獲取
? ? ? ? System.out.println(foo.getTClass());
? ? }
}
?
abstract class Foo<T>{
? ? public Class<T> getTClass()
? ? {
? ? ? ? Class<T> tClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
? ? ? ? return tClass;
? ? }
}?
輸出:?
class java.lang.String
class java.lang.String?
上面的代碼不是萬能的,只有實例化T的子類才能按上述方法獲得T的實際類型,? 如果子類沒有實例化T,則無法獲取T的實際類型;? 比如,class Child 并沒有實例化T,所以得不到String.class;?
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
?
public class Main{
? ? public static void main(String[] args){
? ? ? ? //區別在new Child<String>()沒有{}匿名類
? ? ? ? Foo<String> foo = new Child<String>();
? ? ? ? // 在類的外部這樣獲取
? ? ? ? Type type = ((ParameterizedType)foo.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
? ? ? ? System.out.println(type);
? ? ? ? // 在類的內部這樣獲取
? ? ? ? System.out.println(foo.getTClass());
? ? }
}
?
abstract class Foo<T>{
? ? public Class<T> getTClass()
? ? {
? ? ? ? Class<T> tClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
? ? ? ? return tClass;
? ? }
}
?
class Child<T> extends Foo<T>{
}?
輸出:?
Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
? ? at com.hankcs.Main.main(Main.java:9)
? ? at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
? ? at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
? ? at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
? ? at java.lang.reflect.Method.invoke(Method.java:606)?
有一種解決方式,父類本身不獲取泛型的具體類型,僅提供抽象方法,由子類來提供具體的類型?
public abstract class Foo<T> {??
? ? public abstract Class getEntityClass();??
}??
?
public class Child extends Foo<String> {??
? ? public Class getEntityClass() {??
? ? ? ? return String.class;??
? ? }??
}? ?
對于獲取泛型的方法,比較完整的代碼如下:?
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
?
public class GenericsUtils {
? ? /**
? ? ?* 通過反射,獲得定義Class時聲明的父類的范型參數的類型. 如public BookManager extends
? ? ?* GenricManager<Book>
? ? ?*?
? ? ?* @param clazz The class to introspect
? ? ?* @return the first generic declaration, or <code>Object.class</code> if cannot be determined
? ? ?*/
? ? public static Class getSuperClassGenricType(Class clazz) {
? ? ? ? return getSuperClassGenricType(clazz, 0);
? ? }
?
? ? /**
? ? ?* 通過反射,獲得定義Class時聲明的父類的范型參數的類型. 如public BookManager extends GenricManager<Book>
? ? ?*?
? ? ?* @param clazz clazz The class to introspect
? ? ?* @param index the Index of the generic ddeclaration,start from 0.
? ? ?*/
? ? public static Class getSuperClassGenricType(Class clazz, int index)
? ? ? ? ? ? throws IndexOutOfBoundsException {
? ? ? ? Type genType = clazz.getGenericSuperclass();
? ? ? ? if (!(genType instanceof ParameterizedType)) {
? ? ? ? ? ? return Object.class;
? ? ? ? }
? ? ? ? Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
? ? ? ? if (index >= params.length || index < 0) {
? ? ? ? ? ? return Object.class;
? ? ? ? }
? ? ? ? if (!(params[index] instanceof Class)) {
? ? ? ? ? ? return Object.class;
? ? ? ? }
? ? ? ? return (Class) params[index];
? ? }
}?
轉載來源:? http://blog.csdn.net/wangjunjun2008/article/details/43970217? http://www.hankcs.com/program/t-class.html? https://www.cnblogs.com/sirab415/p/6133533.html
總結
以上是生活随笔為你收集整理的[转载] Java获取泛型T的类型 T.class的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gitlab ci mysql_php-
- 下一篇: [转载] Google Java代码规范