前情提要

本文章用于运行时获取泛型的具体类型,有一些情况下可以获取到真实类型,有一些情况下获取不到的情况。
Class 类的两个方法

 /*** Returns the {@code Class} representing the superclass of the entity* (class, interface, primitive type or void) represented by this* {@code Class}.  If this {@code Class} represents either the* {@code Object} class, an interface, a primitive type, or void, then* null is returned.  If this object represents an array class then the* {@code Class} object representing the {@code Object} class is* returned.** @return the superclass of the class represented by this object.* 用于获取this指代的对象的父类的类型,如果this指代的对象类型是Object,接口,基本类型,或void,方法返回null* 如果this指代的是一个数组,则返回Object类型* 这个方法会做泛型擦除,我们获取不到真实声明的泛型,我们要用下面的那个方法*/public native Class<? super T> getSuperclass();/*** Returns the {@code Type} representing the direct superclass of* the entity (class, interface, primitive type or void) represented by* this {@code Class}.** <p>If the superclass is a parameterized type, the {@code Type}* object returned must accurately reflect the actual type* parameters used in the source code. The parameterized type* representing the superclass is created if it had not been* created before. See the declaration of {@link* java.lang.reflect.ParameterizedType ParameterizedType} for the* semantics of the creation process for parameterized types.  If* this {@code Class} represents either the {@code Object}* class, an interface, a primitive type, or void, then null is* returned.  If this object represents an array class then the* {@code Class} object representing the {@code Object} class is* returned.** @throws java.lang.reflect.GenericSignatureFormatError if the generic*     class signature does not conform to the format specified in*     <cite>The Java&trade; Virtual Machine Specification</cite>* @throws TypeNotPresentException if the generic superclass*     refers to a non-existent type declaration* @throws java.lang.reflect.MalformedParameterizedTypeException if the*     generic superclass refers to a parameterized type that cannot be*     instantiated  for any reason* @return the superclass of the class represented by this object* @since 1.5* 获取this对象的源代码中声明的父类类型,包括声明的泛型,即泛型未被擦除* 我们的测试需要使用这个方法*/public Type getGenericSuperclass() {ClassRepository info = getGenericInfo();if (info == null) {return getSuperclass();}// Historical irregularity:// Generic signature marks interfaces with superclass = Object// but this API returns null for interfacesif (isInterface()) {return null;}return info.getSuperclass();}

测试代码

public class Main {public static void main(String[] args) {// TODO: 普通类继承抽象类  1=10//带body实现和泛型指定Type list1 = new ArrayList<String>() {}.getClass().getGenericSuperclass();//带泛型不带bodyType list2 = new ArrayList<String>().getClass().getGenericSuperclass();//带body不带泛型,编译报错 Cannot use "<>"with anonymous inner classes//Type list3 = new ArrayList<>() {//}.getClass().getGenericSuperclass();//什么都不带Type list4 = new ArrayList<>().getClass().getGenericSuperclass();// TODO: 接口  2=3//带body实现和泛型指定Type a1 = new Aoo<Double>() {}.getClass().getGenericSuperclass();//带泛型不带body 编译报错 'Aoo' is abstract; cannot be instantiated//Type a2 = new Aoo<Double>().getClass().getGenericSuperclass();//带body不带泛型 编译报错 Cannot use "<>"with anonymous inner classes//Type a3 = new Aoo<>() {//}.getClass().getGenericSuperclass();// TODO: 接口继承接口  3=2//带body实现和泛型指定Type c1 = new Coo<String>() {}.getClass().getGenericSuperclass();//带泛型不带body 编译报错 'Coo' is abstract; cannot be instantiated//Type c2 = new Coo<Double>().getClass().getGenericSuperclass();//带body不带泛型 编译报错 Cannot use "<>"with anonymous inner classes//Type c3 = new Coo<>() {//}.getClass().getGenericSuperclass();// TODO: 普通类实现接口  4=9//带body实现和泛型指定Type b1 = new Boo<Double>() {}.getClass().getGenericSuperclass();//带泛型不带bodyType b2 = new Boo<String>().getClass().getGenericSuperclass();//带body不带泛型,编译报错 Cannot use "<>"with anonymous inner classes//Type b3 = new Boo<>(){}.getClass().getGenericSuperclass();//什么都不带Type b4 = new Boo<>().getClass().getGenericSuperclass();// TODO: 抽象类  5=6=7=8//带body实现和泛型指定Type foo1 = new Foo<String>() {}.getClass().getGenericSuperclass();//带泛型不带body 编译报错  'Foo' is abstract; cannot be instantiated//Type foo2=new Foo<String>().getClass().getGenericSuperclass();//带body不带泛型 编译报错 Cannot use "<>"with anonymous inner classes//Type foo3=new Foo<>(){}.getClass().getGenericSuperclass();//TODO: 抽象类继承普通类  6=5=7=8//带body实现和泛型指定Type t1 = new Too<String>() {}.getClass().getGenericSuperclass();//带泛型不带body 'Too' is abstract; cannot be instantiated//Type t2 = new Too<String>().getClass().getGenericSuperclass();//带body不带泛型,编译报错 Cannot use "<>"with anonymous inner classes//Type t3 = new Too<>() {//}.getClass().getGenericSuperclass();// TODO: 抽象类继承抽象类  7=5=6=8//带body实现和泛型指定Type rpc1 = new QueueRpcSupport<String>() {}.getClass().getGenericSuperclass();//带泛型不带body 编译报错  'QueueRpcSupport' is abstract; cannot be instantiated//Type rpc2 = new QueueRpcSupport<String>().getClass().getGenericSuperclass();//带body不带泛型 编译报错 Cannot use "<>"with anonymous inner classes//Type rpc3 = new QueueRpcSupport<>() {//}.getClass().getGenericSuperclass();// TODO: 抽象类实现接口  8=5=6=7//带body实现和泛型指定Type d1 = new Doo<Double>() {}.getClass().getGenericSuperclass();//带泛型不带body 'Doo' is abstract; cannot be instantiated//Type d2 = new Doo<String>().getClass().getGenericSuperclass();//带body不带泛型,编译报错 Cannot use "<>"with anonymous inner classes//Type d3 = new Doo<>(){}.getClass().getGenericSuperclass();// TODO: 普通类  9=4//带body实现和泛型指定Type z1 = new Zoo<String>() {}.getClass().getGenericSuperclass();//带泛型不带bodyType z2 = new Zoo<String>().getClass().getGenericSuperclass();//带body不带泛型,编译报错 Cannot use "<>"with anonymous inner classes//Type z3 = new Zoo<>() {//}.getClass().getGenericSuperclass();//什么都不带Type z4 = new Zoo<>().getClass().getGenericSuperclass();// TODO: 2022/3/18 普通类继承普通类  10=1//带body实现和泛型指定Type x1 = new Xoo<String>() {}.getClass().getGenericSuperclass();//什么都不带Type x2 = new Xoo<>().getClass().getGenericSuperclass();//带body不带泛型,编译报错 Cannot use "<>"with anonymous inner classes//Type x3 = new Xoo<>() {//}.getClass().getGenericSuperclass();//带泛型不带bodyType x4 = new Xoo<String>().getClass().getGenericSuperclass();System.out.println("test");}
}//接口
interface Aoo<E> {}//接口继承接口
interface Coo<E> extends Aoo<E> {}//抽象类
abstract class Foo<T> {}//抽象类实现接口
abstract class Doo<E> implements Coo<E> {}//抽象类继承抽象类
abstract class QueueRpcSupport<T> extends Foo<T> {}//抽象类继承普通类
abstract class Too<E> extends Boo<E> {}//普通类
class Zoo<E> {}//普通类实现接口
class Boo<E> implements Aoo<E> {}//普通类继承普通类
class Xoo<E> extends Zoo<E> {}

测试结果

结论

  1. 接口/普通类/抽象类的组合
接口 抽象类 普通类
接口 接口继承接口 / /
抽象类 抽象类实现接口 抽象类继承抽象类 抽象类继承普通类
普通类 普通类实现接口 普通类继承抽象类 普通类继承普通类
  1. 结果分析

java 运行时获取泛型真实类型相关推荐

  1. java中的类如何选取,java里如何获取泛型的类型

    jdk1.5开始支持泛型,所以我们有时需要把泛型里定义的对象的类型拿到,研究了一下sample代码 可以这样来做 比如现在我定义了三个类Account, AccountItem和Product类. A ...

  2. java获取泛型的类型_Java反射获取泛型类型

    public class Person{ }importjava.lang.reflect.ParameterizedType;importjava.lang.reflect.Type;public ...

  3. java 运行时类型_Java基础之RTTI 运行时类型识别

    运行时类型识别(RTTI, Run-Time Type Identification)是Java中非常有用的机制,在Java运行时,RTTI维护类的相关信息. 多态(polymorphism)是基于R ...

  4. java 获取运行时参数,Java8增强反射可以在运行时获取参数名

    技术公众号:Java In Mind(Java_In_Mind),欢迎关注! 原文:Java8增强反射可以在运行时获取参数名 介绍 在JDK增强意见:JPE 118:Access to Paramet ...

  5. java hibernate方言_java – 如何在运行时获取Hibernate方言

    在我的应用程序中,我使用Hibernate与SQL Server数据库,所以我设置 在我的persistence.xml中. 在某些情况下,我想用NULL包括排序记录,我使用关键字NULLS FIRS ...

  6. Kotlin 知识梳理(13) 运行时的泛型

    一.本文概要 本文是对<<Kotlin in Action>>的学习笔记,如果需要运行相应的代码可以访问在线环境 try.kotlinlang.org,这部分的思维导图为: 二 ...

  7. java运行时参数_运行时的Java 8参数名称

    java运行时参数 Java 8将引入一种更容易的方法来发现方法和构造函数的参数名称. 在Java 8之前,找到参数名称的方法是在编译阶段打开调试符号,这会在生成的类文件中添加有关参数名称的元信息,然 ...

  8. 泛型系列3:获取泛型的类型

    4.3 获取泛型的类型 问题 您需要在运行时获得一个泛型类型实例的Type对象. 解决方案 在使用typeof操作符时提供类型参数:使用类型参数实例化的泛型类型,用GetType()方法. 声明一个一 ...

  9. 什么是引发?Java运行时系统引发的异常如何处理?

    到目前为止,你只是获取了被Java运行时系统引发的异常.然而,程序可以用throw语句引发明确的异常.Throw语句的通常形式如下: throw ThrowableInstance; 这里,Throw ...

最新文章

  1. 2018年中国城市用电量30强
  2. 通过修改Tomcat配置,解决乱码问题
  3. mysql聚合函数count用法_MySQL中聚合函数count的使用和性能优化技巧
  4. Android与服务器端数据交互(基于SOAP协议整合android+webservice)
  5. Unity --- MeshRenderer之网格合并
  6. shell两个时间字符串插值_Shell 脚本速成
  7. 数据结构python吕云翔_《数据结构》吕云翔编著第1章绪论习题解答
  8. mac双击文件打不开——设置鼠标连按速度
  9. JQuery-UI dialog hide属性的取值范围
  10. checkbox选中与取消选择
  11. 【渝粤教育】国家开放大学2018年春季 7215-21T电气传动与调速系统 参考试题
  12. ScreenCapture API – QTP截屏工具
  13. python自动保存图片_python抓取豆瓣图片并自动保存示例学习
  14. haosou属于搜索引擎的_中国的搜索引擎有哪些?
  15. PyTorch代码学习-ImageNET训练
  16. 使用Python与Sharepoint进行交互——第1部分
  17. 高速数据采集卡之FMC子板丨FMC接口AD/DA子卡丨坤驰科技
  18. python read()读取图片_可以python sitk.ReadImage读取列表/系列图像吗?
  19. 程序员约架事件中,薛非为何不应战?
  20. clickhouse源码:函数分析和自定义函数UDF

热门文章

  1. win10解决 netstart -ano|findstr “8080“出现netstart不适内部或外部命令
  2. 【美团滑块】猫眼下单、点评
  3. 【一览表】macOS代号\macOS版本\苹果电脑各型号对应的macOS版本列表
  4. spyder配置文件位置及使用说明
  5. 中国假期、A股日历及八字算命——ChnCal日历小工具介绍
  6. java怎么注释多行注释_java的注释格式和多行注释和单行注释
  7. 电脑能正常上网百度,但是网络显示无Internet
  8. POJ 1753 Flip Game(递归枚举)
  9. 特斯拉召回43万辆国产车/ 苹果头显最早明年发布/ 网易将在暴雪游戏停运后退款… 今日更多新鲜事在此...
  10. Mac上更新Go版本|Go Mod使用|避坑指南|