目录

反射类型继承关系图

Type

ParameterizedType

TypeVariable

WildcardType

GenericArrayType

Annotation

AnnotatedElement

GenericDeclaration

AnnotatedType

Parameter


反射类型继承关系图

Type

default String getTypeName() {
        return toString();
    }

ParameterizedType

表达一个泛型类型。

Code:

public interface ParameterizedType extends Type {
     //返回泛型类型的实际类型。如果是泛型类型的嵌套非泛型类型,则返回length=0的数组
     //返回的类型是第一层嵌套类型,而不是递归的最基本类型。
     //例如List<Map<String,String>> 返回的是Map<String,String>
    Type[] getActualTypeArguments();

//返回原始类型,List<String>返回List
    Type getRawType();

返回类型的拥有者,可以理解嵌套类的外部类。
    Type getOwnerType();
}

Sample

public class GenericTest {

public List<Map<String, String>> f =new ArrayList<Map<String, String>>();
    public Map<String, String> m =new  HashMap<String, String> ();
    
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException {
        test2();

}
   
    public static void test2() throws NoSuchMethodException, SecurityException, NoSuchFieldException
    {
        List<Map<String, String>> list =new ArrayList<Map<String, String>>();
        list.add(new HashMap<String, String>());
        
        Class clazz = list.getClass();
        
        ParameterizedType t = (ParameterizedType) clazz.getGenericSuperclass();
        
        Type[] t2 = t.getActualTypeArguments();
        Type t3 = t.getRawType();
        Type t4 = t.getOwnerType();
        
        System.out.println("clazz : " + clazz.getTypeName());
        for(Type t0 : t2 )
        {
            System.out.println("t2 : " + t0.getTypeName()  + " imp:" +   t0.getClass().toString()  );
        }
        
        System.out.println("t3 : " + t3.toString());
        System.out.println("t4 is " + (t4 == null ? " ": " not ") + "null");
        
        
        Field f = GenericTest.class.getDeclaredField("f");
         Type tt = f.getGenericType();
         
         ParameterizedType pt = (ParameterizedType) tt;
        Type[] pt2 = pt.getActualTypeArguments();
        Type pt3 = pt.getRawType();
        Type pt4 = pt.getOwnerType();
        
        System.out.println("clazz : " + pt.getTypeName());
        for(Type t0 : pt2 )
        {
            System.out.println("pt2 : " + t0.getTypeName()  + " imp:" +   t0.getClass().toString()  );
        }
        
        System.out.println("pt3 : " + pt3.toString());
        System.out.println("pt4 is " + (pt4 == null ? " ": " not ") + "null");
        
        
        Field m = GenericTest.class.getDeclaredField("m");
         Type tm = m.getGenericType();
         
         ParameterizedType mpt = (ParameterizedType) tm;
        Type[] mpt2 = mpt.getActualTypeArguments();
        Type mpt3 = mpt.getRawType();
        Type mpt4 = mpt.getOwnerType();
        
        System.out.println("clazz : " + mpt.getTypeName());
        for(Type t0 : mpt2 )
        {
            System.out.println("mpt2 : " + t0.getTypeName() + " imp:" +   t0.getClass().toString()  );
        }
        
        System.out.println("mpt3 : " + mpt3.toString());
        System.out.println("mpt4 is " + (mpt4 == null ? " ": " not ") + "null");
    }
}

Result:

clazz : java.util.ArrayList
t2 : E imp:class sun.reflect.generics.reflectiveObjects.TypeVariableImpl
t3 : class java.util.AbstractList
t4 is  null
clazz : java.util.List<java.util.Map<java.lang.String, java.lang.String>>
pt2 : java.util.Map<java.lang.String, java.lang.String> imp:class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl
pt3 : interface java.util.List
pt4 is  null
clazz : java.util.Map<java.lang.String, java.lang.String>
mpt2 : java.lang.String imp:class java.lang.Class
mpt2 : java.lang.String imp:class java.lang.Class
mpt3 : interface java.util.Map
mpt4 is  null

TypeVariable

表示类型变量或者又叫类型参数,泛型定义中,<>内的即为类型变量。

Code

public interface TypeVariable<D extends GenericDeclaration> extends Type, AnnotatedElement {
    //返回表示此类型变量上边界的 Type 对象的数组
    //如果未显式声明上边界,则上边界为 Object
    //对于每个上边界 B:

//如果 B 是一个参数化类型(ParameterizedType) 或一个类型变量(TypeVariable),则会创建它
    //否则,B 将被解析。

Type[] getBounds();

// 返回 GenericDeclaration 对象,该对象表示声明此类型变量的一般声明
    D getGenericDeclaration();

//返回此类型变量的名称,它出现在源代码中
    String getName();

//Jdk1.8新增的方法,用于获得注解类型的上限,若未明确声明上边界则默认为长度为0的数组。
     AnnotatedType[] getAnnotatedBounds();
}

Sample

public class GenericTest {

public List<Map<String, String>> f =new ArrayList<Map<String, String>>();
    public Map<String, String> m =new  HashMap<String, String> ();
    
    public <T extends Map<String, Integer> & Serializable & Cloneable & Runnable,U extends List<String>> T m2()
    {
        return null;
    }
    
    public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException {
        test3();

}
   public static void test3() throws NoSuchMethodException, SecurityException
    {
        Method m = GenericTest.class.getDeclaredMethod("m2");
       TypeVariable<Method>[] vlist =      m.getTypeParameters();
       
      for(TypeVariable<Method> v : vlist)
      {
          System.out.println("getName:" +v.getName());
         Method vm =  v.getGenericDeclaration();
         System.out.println("getGenericDeclaration:" + vm.getName());
        
         Type[] bs = v.getBounds() ;
         for( Type b : bs )
         {
             System.out.println("Bound:" + b.getTypeName());
         }
        
        AnnotatedType[] alist = v.getAnnotatedBounds();
        for(AnnotatedType a : alist )
        {
            java.lang.annotation.Annotation[] annotations = a.getAnnotations();
            for (java.lang.annotation.Annotation annotation : annotations) {
                System.out.println("AnnotatedType" + annotation.toString());
            }
            
            
        }
      }

Result:

getName:T
getGenericDeclaration:m2
Bound:java.util.Map<java.lang.String, java.lang.Integer>
Bound:java.io.Serializable
Bound:java.lang.Cloneable
Bound:java.lang.Runnable
getName:U
getGenericDeclaration:m2
Bound:java.util.List<java.lang.String>

WildcardType

Code

public interface WildcardType extends Type {

Type[] getUpperBounds();
    Type[] getLowerBounds();
}

Sample

public  <T,U>  Map<T, U> m3(List<? super T> r1,List<? extends U> r2)
    {
        return null;
    }

Method m = GenericTest.class.getDeclaredMethod("m3",List.class,List.class);
       TypeVariable<Method>[] vlist =      m.getTypeParameters();

for(TypeVariable<Method> v : vlist)
      {
          System.out.println("getName:" +v.getName());
         Method vm =  v.getGenericDeclaration();
         System.out.println("getGenericDeclaration:" + vm.getName());
        
         Type[] bs = v.getBounds() ;
         for( Type b : bs )
         {
             System.out.println("Bound:" + b.getTypeName());
         }
        
      }
      
      
       Type[] genericParameterTypes = m.getGenericParameterTypes();
       for (Type type : genericParameterTypes) {
           if (type instanceof ParameterizedType) {
               ParameterizedType parameterizedType = (ParameterizedType) type;
               Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
               for (Type actualType : actualTypeArguments) {
                   if (actualType instanceof WildcardType) {
                       WildcardType wildcardType = (WildcardType) actualType;
                       System.out.println("WildcardType --> " + wildcardType + " getUpperBounds--> "
                               + Arrays.toString(wildcardType.getUpperBounds()) + " getLowerBounds--> " + Arrays.toString(wildcardType.getLowerBounds()));
                   } else {
                       System.out.println("Not WildcardType --> " + actualType);
                   }
               }

}
       }

Result:

getName:T
getGenericDeclaration:m3
Bound:java.lang.Object
getName:U
getGenericDeclaration:m3
Bound:java.lang.Object
WildcardType --> ? super T getUpperBounds--> [class java.lang.Object] getLowerBounds--> [T]
WildcardType --> ? extends U getUpperBounds--> [U] getLowerBounds--> []

GenericArrayType

也就是泛型数组,也就是元素类型为泛型类型的数组实现了该接口。它要求元素的类型是ParameterizedType或TypeVariable(实际中发现元素是GenericArrayType也是允许的)

//返回元素类型

Type getGenericComponentType();

Annotation

//返回注解的Class

Class<? extends Annotation> annotationType();

AnnotatedElement

public interface AnnotatedElement {

default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
         Objects.requireNonNull(annotationClass);
         // Loop over all directly-present annotations looking for a matching one
         for (Annotation annotation : getDeclaredAnnotations()) {
             if (annotationClass.equals(annotation.annotationType())) {
                 // 做一次强制转化确保确实是同一个Class。
                 return annotationClass.cast(annotation);
             }
         }
         return null;
     }

default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
        Objects.requireNonNull(annotationClass);
        return AnnotationSupport.
            getDirectlyAndIndirectlyPresent(Arrays.stream(getDeclaredAnnotations()).
                                            collect(Collectors.toMap(Annotation::annotationType,
                                                                     Function.identity(),
                                                                     ((first,second) -> first),
                                                                     LinkedHashMap::new)),
                                            annotationClass);
    }

//返回定义在类上的注解,包括直接和间接
    Annotation[] getAnnotations();
        
    //返回直接定义在类上的注解
    Annotation[] getDeclaredAnnotations();
    
    <T extends Annotation> T getAnnotation(Class<T> annotationClass);
    
    default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
        return getAnnotation(annotationClass) != null;
    }

default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
     T[] result = getDeclaredAnnotationsByType(annotationClass);

if (result.length == 0 && // Neither directly nor indirectly present
         this instanceof Class && // the element is a class
         AnnotationType.getInstance(annotationClass).isInherited()) { // Inheritable
         Class<?> superClass = ((Class<?>) this).getSuperclass();
         if (superClass != null) {
             // Determine if the annotation is associated with the
             // superclass
             result = superClass.getAnnotationsByType(annotationClass);
         }
     }

return result;
 }
}

GenericDeclaration

表明可以声明泛型,派生类:Class,Method,Constructor

//返回泛型声明中声明的类型变量

public TypeVariable<?>[] getTypeParameters();

AnnotatedType

泛型相关的类型

public interface AnnotatedType extends AnnotatedElement {
    public Type getType();
}

Sample

public <T, U> Map<T, U> m4(List<? super T> r1, List<? extends U> r2) {
        return null;
    }

public <T, U> Map<String, String> m5(List<? super T> r1, List<? extends U> r2) {
        return null;
    }

public static void test5() throws NoSuchMethodException, SecurityException {
        Method m4 = GenericTest.class.getDeclaredMethod("m4", List.class, List.class);

AnnotatedType xx = m4.getAnnotatedReturnType();
        Type yyy = xx.getType();

Type ttt = m4.getReturnType();

System.out.println("getAnnotatedReturnType:" + xx.toString()  );
        System.out.println("getAnnotatedReturnType.getType:" + yyy.toString()  );
        System.out.println("getReturnType:" + ttt.toString()  );
        
        Method m5 = GenericTest.class.getDeclaredMethod("m5", List.class, List.class);

AnnotatedType xx5 = m5.getAnnotatedReturnType();
        Type yyy5 = xx5.getType();

Type ttt5 = m5.getReturnType();
        
        System.out.println("getAnnotatedReturnType:" + xx5.toString()  );
        System.out.println("getAnnotatedReturnType.getType:" + yyy5.toString()  );
        System.out.println("getReturnType:" + ttt5.toString()  );
        
        
    }

Result

getAnnotatedReturnType:sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl@6d06d69c
getAnnotatedReturnType.getType:java.util.Map<T, U>
getReturnType:interface java.util.Map
getAnnotatedReturnType:sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl@7852e922
getAnnotatedReturnType.getType:java.util.Map<java.lang.String, java.lang.String>
getReturnType:interface java.util.Map

Parameter

定义Executable中的参数

private final String name;
    private final int modifiers;
    private final Executable executable;
    private final int index;

Java反射-继承关系相关推荐

  1. java中parent结构_详解java中继承关系类加载顺序问题

    详解java中继承关系类加载顺序问题 实例代码: /** * Created by fei on 2017/5/31. */ public class SonClass extends ParentC ...

  2. 使用Java的继承关系来描述动物世界的特征和关系。

    使用Java的继承关系来描述动物世界的特征和关系. (1 ) 抽象出项目问题中的对象:动物.老鼠.熊猫. ( 2 ) 抽象出每种对象所具有的性质:名字和食物. ( 3 )抽象出每种对象所具有的行为,动 ...

  3. 编程题2使用Java的继承关系来描述动物世界的特征和关系。

    编程题2使用Java的继承关系来描述动物世界的特征和关系. ( 1 )抽象出项目问题中的对象:动物.老鼠.熊猫. (2 )抽象出每种对象所具有的性质:名字和食物. ( 3)抽象出每种对象所具有的行为, ...

  4. java集合类继承关系图_java集合继承关系图

    面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式. 数组虽然也可以存储对象,但长度是固定的:集合长度是可变的,数组中可以存储基 ...

  5. 使用 Java的继承关系来描述动物世界的特征和关系

    实验目的: (1) 抽象出项目问题中的对象:动物.老鼠.熊猫. (2) 抽象出每种对象所具有的性质:名字和食物. (3) 抽象出每种对象所具有的行为,动物具有吃的行为和睡觉的行为,老鼠除具有动物行为外 ...

  6. java的继承关系linkedlist_LinkedList——JAVA成长之路

    1.单向链表: element:用来存放元素 next:用来指向下一个节点元素 通过每个结点的指针指向下一个结点从而链接起来的结构,最后一个节点的next指向null. 2.单向循环链表:elemen ...

  7. Java中继承关系的构造函数的调用顺序

    情况一:在JAVA中,我们通常把C中的函数叫做方法.对于子类调用父类的构造方法可以做出如下解释: 子类无条件地继承父类的不含参数的构造方法.如果一个类中没有构造函数的话编译器会默认为该类创建一个无参空 ...

  8. java的继承实例_java继承(实例讲解一)

    Java继承(Java inheritance) Java继承是使用已存在的类的定义作为基础建立新类的技术,新类的定义可以增加新的数据或新的功能,也可以用父类的功能,但不能选择性地继承父类.这种技术使 ...

  9. java继承 映射_hibernate继承关系映射和java反射机制的运用

    转:http://blog.csdn.net/derpvailzhangfan/article/details/1957946 ,感谢博主分享 Notes:hibernate元数据的运用:uuid的概 ...

最新文章

  1. 【C++】C/C++ 中default/delete特性
  2. 参考文献中杂志名字格式混乱问题一次解决
  3. 二叉树 BinaryTree (先序、中序、后序遍历 节点查找、插入、删除 完整类) Java数据结构与算法
  4. 旧板与IO板之间的连接
  5. 网络计算机应急处理,网络安全应急响应
  6. CImageList-CBitmap-Usage
  7. 计算机四级c语言题库及答案,计算机四级考试题库及答案
  8. 如何获取网页logo(favicon.ico)
  9. 张正友标定算法原理详解(一)
  10. infopath2007_好吧,很好,所以我服用了该死的红色药丸……行动中的InfoPath(以及小号WinSock的反省)...
  11. C#以管理员身份运行程序
  12. HEG工具处理MOD02的产品,输出多波段
  13. pfamscan 的使用_科学网—[转载]InterProScan的使用教程 - 黄顺谋的博文
  14. 在C语言中使用bool
  15. 【博学谷学习记录】超强总结,用心分享|js语法基础(一)
  16. python阶梯图_不会你还不懂怎么用Python制图吧?师兄教你如何学会绘制漂亮的阶梯图...
  17. 二叉树叶子结点,非叶子节点以及深度的计算
  18. NOI 1797:金银岛(C++)
  19. 代码中的下划线_是什么意思呢?
  20. 【Java 工具类】通过出生日期获取年龄

热门文章

  1. 基于BPMN2.0的工单系统架构设计(上)
  2. Google发布新的TensorFlow物体检测API
  3. HttpServletRequest常用获取URL的方法
  4. 红帽企业虚拟化平台RHEV中WINDOWS 虚拟机如何安装 GUEST代理和驱动
  5. linux常用命令总结
  6. 抓糗百数据和图片的Python爬虫
  7. HTML5按钮的点击态问题
  8. 浏览器是如何工作的?(转载)
  9. 经典正则表达式——常用的正则表达式
  10. groovy:gradle