• Java 反射机制:(十)获取运行时类的父类及父类信息


    一、获取运行时类的父类

    public Class<? super T> getSuperclass():返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的超类的 Class。

        如果此 Class  表示 Object 类、一个接口、一个基本类型或 void,则返回 null。

        如果此对象表示一个数组类,则返回表示该 Object 类的 Class 对象。

       根据 Class 对象获取运行时类的父类信息:

    1     @Test
    2     public void test2(){
    3         Class clazz = Person.class;
    4 
    5         Class superclass = clazz.getSuperclass();
    6         System.out.println(superclass);
    7     }

       运行结果:

      

       Demo2:

    1     @Test
    2     public void test3(){
    3         System.out.println(Integer.class.getSuperclass());//Number
    4         System.out.println(int.class.getSuperclass());//null
    5         System.out.println(Runnable.class.getSuperclass());//null
    6         System.out.println(int[].class.getSuperclass());//Object
    7         System.out.println(String[].class.getSuperclass());//Object
    8     }

    二、获取运行时类的带泛型的父类

        JDK1.5 引入的泛型,为了通过反射操作这些泛型,新增了 ParameterType,GenericArrayType,TypeVariable 和 WildcardType 几种类型来代表不能被归一到 Class中的类型但是又和原始类型齐名的类型。

        

        而在 Class 类、Field 类、Method 类等 API 中增加了很多关于泛型信息的方法,例如在 Class 类中就有很多,其中有一个获取泛型父类的方法:

    public Type getGenericSuperclass():返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的直接超类的 Type。
    

      

        根据 Class 对象获取运行时类的带泛型的父类

    1     @Test
    2     public void test3(){
    3         Class clazz = Person.class;
    4 
    5         Type genericSuperclass = clazz.getGenericSuperclass();
    6         System.out.println(genericSuperclass);
    7     }

        运行结果:

        

        Demo:

     1 import java.lang.reflect.ParameterizedType;
     2 import java.lang.reflect.Type;
     3 import java.lang.reflect.TypeVariable;
     4 
     5 public class TestGenericSuperClass {
     6     public static void main(String[] args) {
     7         Class<?> c = Base.class;
     8         TypeVariable<?>[] typeParameters = c.getTypeParameters();
     9         for (TypeVariable<?> typeVariable : typeParameters) {
    10             System.out.println(typeVariable + ",上限:" + typeVariable.getBounds()[0]);
    11         }
    12         
    13         Class<Sub> clazz = Sub.class;
    14         Type gs = clazz.getGenericSuperclass();
    15         
    16         ParameterizedType gt = (ParameterizedType)gs;
    17         Type[] types = gt.getActualTypeArguments();
    18         for (Type type : types) {
    19             System.out.println(type);
    20         }
    21     }
    22 }
    23 class Base<T extends Number>{
    24     
    25 }
    26 class Sub extends Base<Integer>{
    27     
    28 }

    三、获取运行时类的带泛型的父类的泛型

        根据Class对象获取运行时类的带泛型的父类的泛型(获取父类的泛型)

     1     @Test
     2     public void test14(){
     3         Class clazz = Person.class;
     4 
     5         Type genericSuperclass = clazz.getGenericSuperclass();
     6         ParameterizedType paramType = (ParameterizedType) genericSuperclass;
     7         //获取泛型类型
     8         Type[] actualTypeArguments = paramType.getActualTypeArguments();
     9         //System.out.println(actualTypeArguments[0].getTypeName());
    10         System.out.println(((Class)actualTypeArguments[0]).getName());
    11     }

        运行结果:

        

    四、

    五、

  • 相关阅读:
    500 cannot be cast to javax.xml.registry.infomodel
    mybatis
    [Nest] 02.nest之控制器
    [React] react-interview-01
    [JavaScript] es6规则总结
    [JavaScript] Date对象
    [Vue] vuex-interview
    [Vue] vue-router-interview
    [Vue] vue的一些面试题4
    [Vue] vue的一些面试题3
  • 原文地址:https://www.cnblogs.com/niujifei/p/14887445.html
Copyright © 2020-2023  润新知