Java反射API研究(4)——Class中的重要对象

一、Constructor与Method的父类:Executable

  Executable表示一个可执行类,构造方法与普通方法都是Executable

AnnotatedType[]getAnnotatedExceptionTypes() 
AnnotatedType[]getAnnotatedParameterTypes() 
AnnotatedTypegetAnnotatedReceiverType() 
abstract AnnotatedTypegetAnnotatedReturnType() 
<T extends Annotation> TgetAnnotation(Class<T> annotationClass) 
<T extends Annotation> T[]getAnnotationsByType(Class<T> annotationClass) 
Annotation[]getDeclaredAnnotations() 
abstract Class<?>getDeclaringClass()返回 Class 对象,该对象表示声明由此Executable对象表示的类。
abstract Class<?>[]getExceptionTypes()返回一组表示声明要抛出的异常类型的 Class 对象,这些异常是由此Executable对象表示的底层方法抛出的。
Type[]getGenericExceptionTypes()返回一组 Type 对象,这些对象表示声明要由此Executable对象抛出的异常。
Type[]getGenericParameterTypes()按照声明顺序返回一组 Type 对象,这些对象表示此Executable对象所表示的方法的形参类型。
abstract intgetModifiers()以整数形式返回此Executable对象所表示构造方法的 Java 语言修饰符。
abstract StringgetName()以字符串形式返回此构造方法的名称。
abstract Annotation[][]getParameterAnnotations()按照声明顺序返回一组数组,这些数组表示通过此Executable对象表示的方法的形参上的注释。
intgetParameterCount()返回参数数量
Parameter[]getParameters()返回该Executable对象上所有的参数对象
abstract Class<?>[]getParameterTypes()按照声明顺序返回一组Class对象,这些对象表示此Executable对象所表示构造方法的形参类型。
abstract TypeVariable<?>[]getTypeParameters()按照声明顺序返回一组 TypeVariable 对象,这些对象表示通过此 GenericDeclaration 对象所表示的一般声明来声明的类型变量。
booleanisSynthetic()如果此Executable是一个复合方法,则返回 true;否则返回 false。
booleanisVarArgs()如果声明此构造方法可以带可变数量的参数,则返回 true;否则返回 false。
abstract StringtoGenericString()返回描述此Executable的字符串,其中包括类型参数。

二、Constructor<T>

Constructor<T>  
booleanequals(Object obj) 
AnnotatedTypegetAnnotatedReceiverType() 
AnnotatedTypegetAnnotatedReturnType() 
<T extends Annotation> TgetAnnotation(Class<T> annotationClass)
Annotation[]getDeclaredAnnotations() 
Class<T>getDeclaringClass() 
Class<?>[]getExceptionTypes() 
Type[]getGenericExceptionTypes() 
Type[]getGenericParameterTypes() 
intgetModifiers() 
StringgetName() 
Annotation[][]getParameterAnnotations() 
intgetParameterCount() 
Class<?>[]getParameterTypes() 
TypeVariable<Constructor<T>>[]getTypeParameters() 
inthashCode() 
booleanisSynthetic() 
booleanisVarArgs() 
TnewInstance(Object... initargs)使用此 Constructor 对象表示的构造方法来创建该构造方法的声明类的新实例,并用指定的初始化参数初始化该实例。
StringtoGenericString()返回描述此 Constructor 的字符串,其中包括类型参数。

  类的构造器对象,通过Class对象的getConstructors()、getConstructor(Class<?>... parameterTypes)(只返回public的构造方法),getDeclaredConstructor(Class<?>... parameterTypes)、getDeclaredConstructors()(可包括非public构造方法)获取。

  重要方法是newInstance(Object... initargs),这个可以用于调用非默认构造方法来创建该类的实例。

  对于不可访问的非public方法,可以调用父类AccessibleObject的setAccessible(boolean flag) 方法来强制访问。

 

三、Method

booleanequals(Object obj) 
AnnotatedTypegetAnnotatedReturnType() 
<T extends Annotation> TgetAnnotation(Class<T> annotationClass)
Annotation[]getDeclaredAnnotations() 
Class<?>getDeclaringClass() 
ObjectgetDefaultValue()返回由此 Method 实例表示的注释成员的默认值。
Class<?>[]getExceptionTypes() 
Type[]getGenericExceptionTypes() 
Type[]getGenericParameterTypes() 
TypegetGenericReturnType()返回表示由此 Method 对象所表示方法的正式返回类型的 Type 对象。
intgetModifiers() 
StringgetName() 
Annotation[][]getParameterAnnotations() 
intgetParameterCount() 
Class<?>[]getParameterTypes() 
Class<?>getReturnType()返回一个Class对象,该对象描述了此Method对象所表示的方法的正式返回类型。
TypeVariable<Method>[]getTypeParameters() 
inthashCode() 
Objectinvoke(Object obj, Object... args)对带有指定参数的指定对象调用由此 Method 对象表示的底层方法。
booleanisBridge()如果此方法是 bridge 方法,则返回 true;否则,返回 false。
booleanisDefault()是否是默认方法,对应于接口中的默认方法。
booleanisSynthetic() 
booleanisVarArgs() 
StringtoGenericString() 

  关注其中的invoke(Object obj, Object... args)方法,第一个是要调用这个方法的对象,剩下的方法的参数,返回值就是该函数的返回值。

  通过Class对象的getMethod(String name, Class<?>... parameterTypes)、getMethods()(包含继承的public方法)、getDeclaredMethod(String name, Class<?>... parameterTypes)、getDeclaredMethods()(不包含继承的方法,但包含非public方法)来返回该类的Method对象。

  同样可调用setAccessible(boolean flag)来强制调用非public方法。

 

四、函数的参数Parameter对象

booleanequals(Object obj) 
AnnotatedTypegetAnnotatedType() 
<T extends Annotation> TgetAnnotation(Class<T> annotationClass)
Annotation[]getAnnotations() 
<T extends Annotation> T[]getAnnotationsByType(Class<T> annotationClass)
<T extends Annotation> TgetDeclaredAnnotation(Class<T> annotationClass)
Annotation[]getDeclaredAnnotations() 
<T extends Annotation> T[]getDeclaredAnnotationsByType(Class<T> annotationClass)
ExecutablegetDeclaringExecutable()返回该参数所在的Executable对象
intgetModifiers()返回该参数的标识符
StringgetName()返回该参数的名称
TypegetParameterizedType()返回参数化类型
Class<?>getType()返回该参数的Class
inthashCode() 
booleanisImplicit()如果该参数在代码中是隐式声明的,则返回true,否则false。
booleanisNamePresent()该参数的名称是否保存在class文件中,需要编译时加参数-parameter
booleanisSynthetic() 
booleanisVarArgs() 

  调用Executable对象的getParameters()方法来返回该可执行对象上的所有参数列表。

  

五、Field对象

  表示一个类中的属性

booleanequals(Object obj) 
Objectget(Object obj)返回指定对象上此 Field 表示的字段的值。
AnnotatedTypegetAnnotatedType() 
<T extends Annotation> TgetAnnotation(Class<T> annotationClass)
<T extends Annotation> T[]getAnnotationsByType(Class<T> annotationClass)
TgetT(Object obj)获取一个静态或实例T类型的值。
Annotation[]getDeclaredAnnotations() 
Class<?>getDeclaringClass() 
TypegetGenericType() 
intgetModifiers() 
StringgetName() 
Class<?>getType()返回一个 Class 对象,它标识了此 Field 对象所表示字段的声明类型。
inthashCode() 
booleanisEnumConstant()如果此字段表示枚举类型的元素,则返回 true;否则返回 false。
booleanisSynthetic() 
voidset(Object obj, Object value)将指定对象变量上此 Field 对象表示的字段设置为指定的新值。
voidsetT(Object obj, T z)将字段的值设置为指定对象上的一个T类型值。
StringtoGenericString() 

  通过Class对象的getField(String name)、getFields()(包含继承的public属性)、getDeclaredField(String name)、getDeclaredFields()(本类的所有属性,包含非public的)来获取Field对象。

  可调用setAccessible(boolean flag)来强制修改非public属性。

 

六、补充上面除了Parameter外其他几个的父类:AccessibleObject

<T extends Annotation> TgetAnnotation(Class<T> annotationClass) 
<T extends Annotation> TgetDeclaredAnnotation(Class<T> annotationClass) 
Annotation[]getAnnotations() 
Annotation[]getDeclaredAnnotations() 
<T extends Annotation> T[]getAnnotationsByType(Class<T> annotationClass) 
<T extends Annotation> T[]getDeclaredAnnotationsByType(Class<T> annotationClass) 
booleanisAnnotationPresent(Class<? extends Annotation> annotationClass) 
booleanisAccessible()获取此对象的 accessible 标志的值。
static voidsetAccessible(AccessibleObject[] array, boolean flag)使用单一安全性检查(为了提高效率)为一组对象设置 accessible 标志的便捷方法。
voidsetAccessible(boolean flag)将此对象的 accessible 标志设置为指示的布尔值。

七、数组相关类型Array   

static Objectget(Object array, int index)返回指定数组对象中索引组件的值。
static TgetT(Object array, int index)以T形式返回指定数组对象中索引组件的值。如果array不是T类型的,则get时进行扩展转换,但如果发生收缩转换,则抛出 IllegalArgumentException。
static intgetLength(Object array)以int形式返回指定数组对象的长度。
static ObjectnewInstance(Class<?> componentType, int... dimensions)创建一个具有指定的组件类型和维度的新数组。
static ObjectnewInstance(Class<?> componentType, int length)创建一个具有指定的组件类型和长度的新数组。
static voidset(Object array, int index, Object value)将指定数组对象中索引组件的值设置为指定的新值。
static voidsetT(Object array, int index, T z)将指定数组对象中索引组件的值设置为指定的新T值。如果array不是T类型的,则set时进行扩展转换,但如果发生收缩转换,则抛出 IllegalArgumentException。 

八、修饰符Modifier

Modifier还有同名常量Modifier 类提供了static方法和常量,对类和成员访问修饰符进行解码。
static booleanisAbstract(int mod)整数参数是否包括 abstract 修饰符
static booleanisFinal(int mod)整数参数是否包括 final 修饰符
static booleanisInterface(int mod)整数参数是否包括 interface 修饰符
static booleanisNative(int mod)整数参数是否包括 native 修饰符
static booleanisPrivate(int mod)整数参数是否包括 private 修饰符
static booleanisProtected(int mod)整数参数是否包括 protected 修饰符
static booleanisPublic(int mod)整数参数是否包括 public 修饰符
static booleanisStatic(int mod)整数参数是否包括 static 修饰符
static booleanisStrict(int mod)整数参数是否包括 strictfp 修饰符
static booleanisSynchronized(int mod)整数参数是否包括 synchronized 修饰符
static booleanisTransient(int mod)整数参数是否包括 transient 修饰符
static booleanisVolatile(int mod)整数参数是否包括 volatile 修饰符
static StringtoString(int mod)返回描述指定修饰符中的访问修饰符标志的字符串。

转载于:https://www.cnblogs.com/guangshan/p/4893490.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值