Java获取泛型T的类型 T.class

Java获取泛型T的类型 T.class

https://blog.csdn.net/hellozhxy/article/details/82024712

 
  1. import java.lang.reflect.ParameterizedType;

  2. import java.lang.reflect.Type;

  3.  
  4. public class Main{

  5. public static void main(String[] args)

  6. {

  7. Foo<String> foo = new Foo<String>(){};

  8. // 在类的外部这样获取

  9. Type type = ((ParameterizedType)foo.getClass().getGenericSuperclass()).getActualTypeArguments()[0];

  10. System.out.println(type);

  11. // 在类的内部这样获取

  12. System.out.println(foo.getTClass());

  13. }

  14. }

  15.  
  16. abstract class Foo<T>{

  17. public Class<T> getTClass()

  18. {

  19. Class<T> tClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];

  20. return tClass;

  21. }

  22. }

输出:

 
  1. class java.lang.String

  2. class java.lang.String

上面的代码不是万能的,只有实例化T的子类才能按上述方法获得T的实际类型, 
如果子类没有实例化T,则无法获取T的实际类型; 
比如,class Child 并没有实例化T,所以得不到String.class;

 
  1. import java.lang.reflect.ParameterizedType;

  2. import java.lang.reflect.Type;

  3.  
  4. public class Main{

  5. public static void main(String[] args){

  6. //区别在new Child<String>()没有{}匿名类

  7. Foo<String> foo = new Child<String>();

  8. // 在类的外部这样获取

  9. Type type = ((ParameterizedType)foo.getClass().getGenericSuperclass()).getActualTypeArguments()[0];

  10. System.out.println(type);

  11. // 在类的内部这样获取

  12. System.out.println(foo.getTClass());

  13. }

  14. }

  15.  
  16. abstract class Foo<T>{

  17. public Class<T> getTClass()

  18. {

  19. Class<T> tClass = (Class<T>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];

  20. return tClass;

  21. }

  22. }

  23.  
  24. class Child<T> extends Foo<T>{

  25. }

输出:

 
  1. Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

  2. at com.hankcs.Main.main(Main.java:9)

  3. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  4. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

  5. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  6. at java.lang.reflect.Method.invoke(Method.java:606)

有一种解决方式,父类本身不获取泛型的具体类型,仅提供抽象方法,由子类来提供具体的类型

 
  1. public abstract class Foo<T> {

  2. public abstract Class getEntityClass();

  3. }

  4.  
  5. public class Child extends Foo<String> {

  6. public Class getEntityClass() {

  7. return String.class;

  8. }

  9. }

对于获取泛型的方法,比较完整的代码如下:

 
  1. import java.lang.reflect.ParameterizedType;

  2. import java.lang.reflect.Type;

  3.  
  4. public class GenericsUtils {

  5. /**

  6. * 通过反射,获得定义Class时声明的父类的范型参数的类型. 如public BookManager extends

  7. * GenricManager<Book>

  8. *

  9. * @param clazz The class to introspect

  10. * @return the first generic declaration, or <code>Object.class</code> if cannot be determined

  11. */

  12. public static Class getSuperClassGenricType(Class clazz) {

  13. return getSuperClassGenricType(clazz, 0);

  14. }

  15.  
  16. /**

  17. * 通过反射,获得定义Class时声明的父类的范型参数的类型. 如public BookManager extends GenricManager<Book>

  18. *

  19. * @param clazz clazz The class to introspect

  20. * @param index the Index of the generic ddeclaration,start from 0.

  21. */

  22. public static Class getSuperClassGenricType(Class clazz, int index)

  23. throws IndexOutOfBoundsException {

  24. Type genType = clazz.getGenericSuperclass();

  25. if (!(genType instanceof ParameterizedType)) {

  26. return Object.class;

  27. }

  28. Type[] params = ((ParameterizedType) genType).getActualTypeArguments();

  29. if (index >= params.length || index < 0) {

  30. return Object.class;

  31. }

  32. if (!(params[index] instanceof Class)) {

  33. return Object.class;

  34. }

  35. return (Class) params[index];

  36. }

  37. }

转载来源: 
http://blog.csdn.net/wangjunjun2008/article/details/43970217 
http://www.hankcs.com/program/t-class.html 
https://www.cnblogs.com/sirab415/p/6133533.html

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java获取T的Class通常有两种方式。 第一种方式是使用类的getClass()方法来获取T的Class信息。这种方式的前提是必须要创建对象。具体实现方法如下: ``` public class GenericClass<T>{ public Class<T> getGenericClass() { Class<T> cls = (Class<T>) ((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0]; return cls; } } public class Main { public static void main(String[] args) { GenericClass<String> gc = new GenericClass<String>(){}; System.out.println(gc.getGenericClass()); } } ``` 第二种方式是使用TypeReference类。这是由Jackson库提供的一个工具类,可以避免在运行时由于擦除而导致的Class信息丢失的问题。具体实现方法如下: ``` public abstract class TypeReference<T> { private final Type type; protected TypeReference() { Type superClass = getClass().getGenericSuperclass(); if (superClass instanceof Class<?>) { throw new IllegalArgumentException("Internal error: TypeReference constructed without actual type information"); } type = ((ParameterizedType) superClass).getActualTypeArguments()[0]; } public Type getType() { return type; } } public class Main { public static void main(String[] args) { TypeReference<List<String>> typeReference = new TypeReference<List<String>>() {}; System.out.println(typeReference.getType()); } } ``` 无论是哪种方式,获取T的Class都可以方便地进行类型检查和类型转换操作。但需要注意的是,在获取T的Class时,必须要保证T已经被实例化。如果T没有被实例化,则获取Class信息可能不准确,无法保证正确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值