zoukankan      html  css  js  c++  java
  • Java 反射总结

    一、反射的定义

      程序可以访问,检测和修改它本身状态或行为的一种能力,并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义。

    二、反射的类

    1)反射类

    java.lang.Class         

    java.lang.reflect.Constructor

    java.lang.reflect.Field      

    java.lang.reflect.Method

    java.lang.reflect.Modifier

     2)相关类

    AccessibleObject

    Array

    Proxy

    三、Class

    (1)获取Class对象的三种方式

    • 类名.class
    • Class.forName()
    • 对象.getClass()

    (2)Class的成员方法

    1)构造方法

    getConstructor

    getConstructors

    getDeclaredConstructor

    getDeclaredConstructors

    2)变量域

    getField

    getFields

    getDeclaredField

    getDeclaredFields

    3)方法

    getMethod

    getMethods

    getDeclaredMethod

    getDeclaredMethods

    4)成员类

    getClasses

    getDeclaredClasses

    5)类注解

    getAnnnotation

    getAnnnotations

    getAnnnotationsByTpe

    6)父类

    getSuperClass

    getGenericSuperClass    获取包含泛型的父类

    getAnnotatedSuperclass   获取extends父类时的标注的注解和父类,如class  B extends @Anno A {  }

    7)接口

    getInterfaces

    getGenericInterfaces      获取包含泛型的接口

    getAnnotatedInterfaces  获取implements接口时的标注的注解和接口,如class  C implements @AnnoA A,@AnnoB B {  }

    8)立即封闭类

    getDeclaringClass  返回Class对象,表示this类在return类中被声明,this类对象所表示的类或接口是return类的成员。

    9)封闭

    getEnclosingClass

    getEnclosingConstructor

    getEnclosingMethod

    10)类型判断

    isInstance

    isPrimitive

    isArray

    isMemberClass              成员类

    isLocalClass                  局部类

    isAnnonymousClass      匿名类

    isInterface

    isEnum

    isAnnotation

    11)类名

    getName                     内部名          

    getSimpleName          缩写名

    getCanonicalName    全称名

    12)其他方法

    newInstance                     无参构造方法创建对象,没有无参构造方法拋异常InstantiationException

    getClassLoader

    getTypeParameters         泛型参数

    getModifiers                     修饰符

    getPackage

    getResource                         获取资源返回url

    getResourceAsStream         获取资源返回InputStream

    四、constructor

    1)特殊

    getName

    getModifiers

    isVarArgs      可变参

    isSynthetic             由编译器自动添加,在非静态成员类用于传人外部类对象的构造方法是复合构造方法

    newInstance

    2)参数

    getGenericParameterTypes       形参泛型参数类型

    getParameterTypes                    形参参数类型

    getTypeParameters                    泛型参数占位符 

    3)异常

    getExceptionTypes

    getGenericExceptionTypes

    4)注解

    getAnnotation

    getDeclaredAnnotations

    getParameterAnnotations    注解在形参

    五、Field

    1)特殊

    getName

    getDeclaringClass   获取Field声明所在的类

    getModifiers

    isEnumConstant    是否枚举

    isSynthetic             由编译器自动添加,在非静态成员类用于访问外部类的变量是复合变量

    2)get基本类型与Object

    getXXX

    get

    3)set基本类型与Object

    setXXX

    set

    4)类型

    getType

    getGenericType

    5)注解

    getAnnotation

    getDeclaredAnnotations

    六、Method

     1)特殊

    getName

    invoke        异步执行

    getDeclaringClass   获取Method声明所在的类

    getModifiers

    isVarArgs        可变参

    getDefaultValue

    isSynthetic     由编译器自动添加,在非静态成员类用于访问外部类的方法是复合方法

    2)参数

    getGenericParameterTypes       形参泛型参数类型

    getParameterTypes                    形参参数类型

    3)返回值

    getReturnType

    getGenericReturnType

    4)注解

    getAnnotation

    getDeclaredAnnotations

    getParameterAnnotations

    5)异常

    getExceptionTypes

    getGenericExceptionTypes

    七、Modifier

    1)静态字段

    PUBLIC

    FINAL

    STRICT strictfp

    VOLATILE

    ...

    2)方法

    isPublic

    isFinal

    isStrict

    isVolatile

    ...

    八、AccessibleObject

    getAnnotation

    getAnnotations

    getDeclaredAnnotations

    isAccessible

    isAnnotationPresent            如果指定类型的注释存在于此元素上,则返回 true,否则返回 false。

    setAccessible                      静态方法

    setAccessible

    九、Array

    静态方法

    get

    getXXX

    getLength

    newInstance             有指定数组维度

    set

    setXXX

    十、Proxy

    静态方法

    getInvocationHandler

    getProxyClass

    isProxyClass

    newProxyInstance

  • 相关阅读:
    windows7使用(2)软件安装及系统优化
    Lucene.net搜索——初识lucene
    只好代码不好色,嫁人当嫁IT男!
    数据库生成word说明文档
    搬家到cnblogs,请多关照
    轻量级的数据交换格式——初识Json(上)
    Flex之Hello world
    Flex之Hello world
    Flex之Hello world
    在 Visual C++ 中控制全局对象的初始化顺序,#pragma init_seg(compiler)
  • 原文地址:https://www.cnblogs.com/maokun/p/7550660.html
Copyright © 2011-2022 走看看