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

 

转载于:https://www.cnblogs.com/maokun/p/7550660.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值