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

一、Constructor与Method的父类:Executable

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

AnnotatedType[] getAnnotatedExceptionTypes()  
AnnotatedType[] getAnnotatedParameterTypes()  
AnnotatedType getAnnotatedReceiverType()  
abstract AnnotatedType getAnnotatedReturnType()  
<T extends Annotation> T getAnnotation(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 int getModifiers() 以整数形式返回此Executable对象所表示构造方法的 Java 语言修饰符。
abstract String getName() 以字符串形式返回此构造方法的名称。
abstract Annotation[][] getParameterAnnotations() 按照声明顺序返回一组数组,这些数组表示通过此Executable对象表示的方法的形参上的注释。
int getParameterCount() 返回参数数量
Parameter[] getParameters() 返回该Executable对象上所有的参数对象
abstract Class<?>[] getParameterTypes() 按照声明顺序返回一组Class对象,这些对象表示此Executable对象所表示构造方法的形参类型。
abstract TypeVariable<?>[] getTypeParameters() 按照声明顺序返回一组 TypeVariable 对象,这些对象表示通过此 GenericDeclaration 对象所表示的一般声明来声明的类型变量。
boolean isSynthetic() 如果此Executable是一个复合方法,则返回 true;否则返回 false。
boolean isVarArgs() 如果声明此构造方法可以带可变数量的参数,则返回 true;否则返回 false。
abstract String toGenericString() 返回描述此Executable的字符串,其中包括类型参数。

二、Constructor<T>

Constructor<T>    
boolean equals(Object obj)  
AnnotatedType getAnnotatedReceiverType()  
AnnotatedType getAnnotatedReturnType()  
<T extends Annotation> T getAnnotation(Class<T> annotationClass)
Annotation[] getDeclaredAnnotations()  
Class<T> getDeclaringClass()  
Class<?>[] getExceptionTypes()  
Type[] getGenericExceptionTypes()  
Type[] getGenericParameterTypes()  
int getModifiers()  
String getName()  
Annotation[][] getParameterAnnotations()  
int getParameterCount()  
Class<?>[] getParameterTypes()  
TypeVariable<Constructor<T>>[] getTypeParameters()  
int hashCode()  
boolean isSynthetic()  
boolean isVarArgs()  
T newInstance(Object... initargs) 使用此 Constructor 对象表示的构造方法来创建该构造方法的声明类的新实例,并用指定的初始化参数初始化该实例。
String toGenericString() 返回描述此 Constructor 的字符串,其中包括类型参数。

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

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

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

 

三、Method

boolean equals(Object obj)  
AnnotatedType getAnnotatedReturnType()  
<T extends Annotation> T getAnnotation(Class<T> annotationClass)
Annotation[] getDeclaredAnnotations()  
Class<?> getDeclaringClass()  
Object getDefaultValue() 返回由此 Method 实例表示的注释成员的默认值。
Class<?>[] getExceptionTypes()  
Type[] getGenericExceptionTypes()  
Type[] getGenericParameterTypes()  
Type getGenericReturnType() 返回表示由此 Method 对象所表示方法的正式返回类型的 Type 对象。
int getModifiers()  
String getName()  
Annotation[][] getParameterAnnotations()  
int getParameterCount()  
Class<?>[] getParameterTypes()  
Class<?> getReturnType() 返回一个Class对象,该对象描述了此Method对象所表示的方法的正式返回类型。
TypeVariable<Method>[] getTypeParameters()  
int hashCode()  
Object invoke(Object obj, Object... args) 对带有指定参数的指定对象调用由此 Method 对象表示的底层方法。
boolean isBridge() 如果此方法是 bridge 方法,则返回 true;否则,返回 false。
boolean isDefault() 是否是默认方法,对应于接口中的默认方法。
boolean isSynthetic()  
boolean isVarArgs()  
String toGenericString()  

  关注其中的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对象

boolean equals(Object obj)  
AnnotatedType getAnnotatedType()  
<T extends Annotation> T getAnnotation(Class<T> annotationClass)
Annotation[] getAnnotations()  
<T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass)
<T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass)
Annotation[] getDeclaredAnnotations()  
<T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass)
Executable getDeclaringExecutable() 返回该参数所在的Executable对象
int getModifiers() 返回该参数的标识符
String getName() 返回该参数的名称
Type getParameterizedType() 返回参数化类型
Class<?> getType() 返回该参数的Class
int hashCode()  
boolean isImplicit() 如果该参数在代码中是隐式声明的,则返回true,否则false。
boolean isNamePresent() 该参数的名称是否保存在class文件中,需要编译时加参数-parameter
boolean isSynthetic()  
boolean isVarArgs()  

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

  

五、Field对象

  表示一个类中的属性

boolean equals(Object obj)  
Object get(Object obj) 返回指定对象上此 Field 表示的字段的值。
AnnotatedType getAnnotatedType()  
<T extends Annotation> T getAnnotation(Class<T> annotationClass)
<T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass)
T getT(Object obj) 获取一个静态或实例T类型的值。
Annotation[] getDeclaredAnnotations()  
Class<?> getDeclaringClass()  
Type getGenericType()  
int getModifiers()  
String getName()  
Class<?> getType() 返回一个 Class 对象,它标识了此 Field 对象所表示字段的声明类型。
int hashCode()  
boolean isEnumConstant() 如果此字段表示枚举类型的元素,则返回 true;否则返回 false。
boolean isSynthetic()  
void set(Object obj, Object value) 将指定对象变量上此 Field 对象表示的字段设置为指定的新值。
void setT(Object obj, T z) 将字段的值设置为指定对象上的一个T类型值。
String toGenericString()  

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

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

 

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

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

七、数组相关类型Array   

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

八、修饰符Modifier

Modifier 还有同名常量 Modifier 类提供了static方法和常量,对类和成员访问修饰符进行解码。
static boolean isAbstract(int mod) 整数参数是否包括 abstract 修饰符
static boolean isFinal(int mod) 整数参数是否包括 final 修饰符
static boolean isInterface(int mod) 整数参数是否包括 interface 修饰符
static boolean isNative(int mod) 整数参数是否包括 native 修饰符
static boolean isPrivate(int mod) 整数参数是否包括 private 修饰符
static boolean isProtected(int mod) 整数参数是否包括 protected 修饰符
static boolean isPublic(int mod) 整数参数是否包括 public 修饰符
static boolean isStatic(int mod) 整数参数是否包括 static 修饰符
static boolean isStrict(int mod) 整数参数是否包括 strictfp 修饰符
static boolean isSynchronized(int mod) 整数参数是否包括 synchronized 修饰符
static boolean isTransient(int mod) 整数参数是否包括 transient 修饰符
static boolean isVolatile(int mod) 整数参数是否包括 volatile 修饰符
static String toString(int mod) 返回描述指定修饰符中的访问修饰符标志的字符串。
posted @ 2015-10-20 00:08  光闪  阅读(1708)  评论(0编辑  收藏  举报