Java反射包的结构与重点api的讲解

本文深入解析Java反射API,涵盖Class、Constructor、Method、Field等核心对象的功能与使用,以及泛型、注解、修饰符的处理技巧,是理解反射机制、进行动态编程的全面指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

反射包的继承与实现图如下:

 

接口方法作用

AnnotatedElement

(可以添加注解的元素,包括类 接口 枚举 注解 ,构造函数,field,方法,局部变量)

<T extends Annotation> T getAnnotation(Class<T> annotationClass)如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null。
default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass)返回直接存在于此元素上的指定类型的注释。忽略继承的注释。
Annotation[] getAnnotations()返回此元素上存在的所有注释。
Annotation[] getDeclaredAnnotations()返回此元素上存在的所有注释。忽略继承的注释。
default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass)返回重复注解的类型,被同注解注解的元素返回该类型注解的数组。参考@Repeatable
default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> anotationClass)返回重复注解的类型,被同注解注解的元素返回注解的数组。忽略继承的注释。
default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass)如果指定类型的注释存在于此元素上,则返回 true,否则返回 false。

AnnotatedType

(可以带注解的类型)

Type getType()返回表示此注释类型的基础类型。

GenericDeclaration

(类 接口 枚举或者方法或者构造函数的泛型声明)

TypeVariable<?>[] getTypeParameters()返回声明顺序的泛型参数类型的数组

TypeVariable<D extends GenericDeclaration>

(泛型变量类型)

AnnotatedType[] getAnnotatedBounds() 
Type[] getBounds()  返回表示此泛型参数上边界的 Type 对象的数组。
D getGenericDeclaration()  获取声明此泛型参数的类或者接口或者方法
String getName()  返回此类型参数的名称,它出现在源代码中。

InvocationHandler

(动态代理的切面类)

Object invoke(Object proxy, Method method, Object[] args) 代理实例的调用处理程序 实现的接口。在代理实例上处理方法调用并返回结果。

Member

(构造函数,方法和field的总称)

Class<?> getDeclaringClass()返回表示声明由此 Member 表示的成员或构造方法的类或接口的 Class 对象。
int getModifiers() 作为整数返回由此 Member 所表示的成员或构造方法的 Java 语言修饰符。
String getName()  返回此 Member 表示的底层成员或构造方法的简单名称。
boolean isSynthetic()  如果此成员是编译器引入的,则返回 true;否则,返回 false。

Type

(所有使用到的数据类型的总称)

default String getTypeName()获取Type的名称

WildcardType

(泛型 <? extends Object> )

 

Type[] getLowerBounds() 返回表示此类型变量下边界的 Type 对象的数组。
Type[] getUpperBounds() 返回表示此类型变量上边界的 Type 对象的数组。

GenericArrayType

(泛型数组类型 T[] )

Type getGenericComponentType()  返回表示此数组的组件类型的 Type 对象。此方法创建数组的组件类型。请参见 ParameterizedType 的声明获得参数化类型创建过程的语义,参见 TypeVariable 获得创建类型变量的过程。

ParameterizedType  

(泛型参数类型,例如:List<T>、Map<K,V>等带有泛型参数的对象)

Type[] getActualTypeArguments()返回表示此类型实际类型参数的Type对象的数组,即返回一个类声明时的泛型类型
Type getOwnerType()  返回Type对象,表示此类型是其成员之一的类型。
Type getRawType()返回Type对象,表示声明此类型的类或接口。

 

反射之class 类学习

Class对象的所有方法:

<U> Class<? extends U>asSubclass(Class<U> clazz)

class 之间没有继承关系,所以如果一个方法需要的参数是 Class<? extends Father> 

而我只有一个Class<Son>,可以直接传递参数,也可以通过Son.class.asSubclass(Father.class) 转化成

Class<? extends Father>, 然后做参数传递。

Tcast(Object obj)强制类型转化,把Object 类型对象 强转成当前class 类型
booleandesiredAssertionStatus()

测试该类的断言功能是否已打开

static Class<?>forName(String className)返回与带有给定字符串名的类或接口相关联的 Class 对象。
static Class<?>forName(String name, boolean initialize, ClassLoader loader)使用给定的类加载器,返回与带有给定字符串名的类或接口相关联的 Class 对象。
AnnotatedType[]getAnnotatedInterfaces()返回带注解的父接口数组
AnnotatedTypegetAnnotatedSuperclass()返回带注解的父类
<A extends Annotation> AgetAnnotation(Class<A> annotationClass)获取指定类型的注解(包括当前类和父类)
Annotation[]getAnnotations()返回当前类上的所有注释。(包括当前类和父类)
<A extends Annotation> A[]getAnnotationsByType(Class<A> annotationClass)获取指定类型的可重复注解(包括当前类和父类)
StringgetCanonicalName()返回类的规范化名称。
Class<?>[]getClasses()返回当前类定义的公共的内部类,以及从父类、父接口那里继承来的内部类
ClassLoadergetClassLoader()返回该类的类加载器。
Class<?>getComponentType()如果此类表示数组类,返回表示此类组件类型的 Class。否则返回null。 
Constructor<T>getConstructor(Class<?>... parameterTypes)返回当前类声明的指定public 构造函数
Constructor<?>[]getConstructors()返回当前类声明的public 构造函数数组
<A extends Annotation> AgetDeclaredAnnotation(Class<A> annotationClass)返回当前类声明的指定注解
Annotation[]getDeclaredAnnotations()返回当前类声明的注解数组
<A extends Annotation> A[]getDeclaredAnnotationsByType(Class<A> annotationClass)返回当前类声明的指定可重复注解
Class<?>[]getDeclaredClasses()返回当前类声明的内部类数组
Constructor<T>getDeclaredConstructor(Class<?>... parameterTypes)放回当前类声明的指定构造函数
Constructor<?>[]getDeclaredConstructors()返回当前类声明的构造函数数组
FieldgetDeclaredField(String name)返回当前类声明的指定字段
Field[]getDeclaredFields()返回当前类声明 的字段数组
MethodgetDeclaredMethod(String name, Class<?>... parameterTypes)返回当前类声明的指定方法
Method[]getDeclaredMethods()返回 当前类声明的所有方法数组
Class<?>getDeclaringClass()如果当前类是成员内部类,返回外部类
Class<?>getEnclosingClass()如果当前类代表的实体是内部类,则返回外部类class。
Constructor<?>getEnclosingConstructor()如果该 Class 对象表示构造方法中的一个本地或匿名类,则返回 Constructor 对象
MethodgetEnclosingMethod()如果此 Class 对象表示某一方法中的一个本地或匿名类,则返回 Method 对象
T[]getEnumConstants()如果此 Class 对象不表示枚举类型,则返回null ,否则返回枚举类的元素数组
FieldgetField(String name)获取当前Class 对象所表示的类或接口的指定公共成员字段。
Field[]getFields()当前 Class所表示的类或接口的所有可访问公共字段数组。
Type[]getGenericInterfaces()获取当前class代表的实体的 带泛型参数的父接口数组
TypegetGenericSuperclass()获取当前class代表的实体的 带泛型参数的父类类型
Class<?>[]getInterfaces()获取实现的接口数组
MethodgetMethod(String name, Class<?>... parameterTypes)获取指定方法。
Method[]getMethods()当前类或接口声明的以及从超类和超接口继承的  方法数组
intgetModifiers()返回此类或接口以整数编码的 Java 语言修饰符。
StringgetName()以 String 的形式返回此 Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称。
PackagegetPackage()获取当前类所属的包。
ProtectionDomaingetProtectionDomain()返回该类的 ProtectionDomain。
URLgetResource(String name)查找带有给定名称的资源。
InputStreamgetResourceAsStream(String name)查找具有给定名称的资源。
Object[]getSigners()获取此类的标记。
StringgetSimpleName()实体(类、接口、基本类型或 void)简称
Class<? super T>getSuperclass()返回表示此 Class 所表示的实体(类、接口、基本类型或 void)的超类的 Class。
StringgetTypeName()返回该类型的名称
TypeVariable<Class<T>>[]getTypeParameters()按声明顺序返回泛型参数变量数组
booleanisAnnotation()如果此 Class 对象是否表示一个注解
booleanisAnnotationPresent(Class<? extends Annotation> annotationClass)如果class上存在指定类型的注解,则返回 true,否则返回 false。
booleanisAnonymousClass()是否是匿名内部类。
booleanisArray()判定当前 Class 对象是否表示一个数组的class。
booleanisAssignableFrom(Class<?> cls)判定当前Class 对象所表示的类或接口与指定的 Class 参数所表示的类或接口是否相同,或是否是其超类或超接口。
booleanisEnum()是否是枚举
booleanisInstance(Object obj)判断Object 对象的class 是否是 当前class 
booleanisInterface()是否是一个接口类型。
booleanisLocalClass()是否是本地类(局部内部类)
booleanisMemberClass()是否是成员类(成员内部类)
booleanisPrimitive()是否表示一个基本类型(int)
booleanisSynthetic()如果此类是编译器自己生成的,则返回 true,否则 false。有匿名类部类的类可以称作为复合类。
TnewInstance()调用默认构造方法创建此 Class 对象所表示的类的一个新实例。
StringtoGenericString()返回该对象的描述,包含标识符等。

  比较重要的方法:newInstance(),创建一个该类的实例,调用默认构造方法。

  特殊的:

  1、获取一个类声明时的确切泛型类型:

  1. class Parameterized<T,V,Z>{

  2. }

  3.  
  4. Type classType = abstractTransfer.getClass().getGenericSuperclass();

  5. Type[] ret = ((ParameterizedType)classType).getActualTypeArguments();

  6.  
  7. T = (Class) ret[0];

  8. V = (Class) ret[1];

  9. Z = (Class) ret[2]

  2、运行时获取一个被代理的类的真实类对象

  TrueClass proxyClass = proxy;
  Class trueClass = proxyClass.getClass().getSuperclass();

 

反射之 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()获取方法或构造函数的声明类
abstract Class<?>[]getExceptionTypes()返回方法或者构造函数声明抛出的异常类数组
Type[]getGenericExceptionTypes()返回方法或者构造函数声明抛出的泛型数组
Type[]getGenericParameterTypes()返回方法或者构造函数声明的泛型参数数组
abstract intgetModifiers()返回方法或者构造函数声明的修饰符
abstract StringgetName()返回方法或者构造函数的名称
 getParameterAnnotations()返回方法或者构造函数声明的参数的注解数组
intgetParameterCount()返回参数个数
Parameter[]getParameters()返回参数对象数组
abstract Class<?>[]getParameterTypes()返回方法或者构造函数声明的参数类型数组
abstract TypeVariable<?>[]getTypeParameters()

返回方法或者构造函数声明的泛型变量数组

:方法开头<> 内部定义的变量的  数组

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()返回类型
TypeVariable<Method>[]getTypeParameters() 
inthashCode() 
Objectinvoke(Object obj, Object... args)触发obj 对象的当前方法,参数就是args
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://blog.youkuaiyun.com/xiaoliuliu2050/article/details/88567142

反射之泛型相关接口学习

https://blog.youkuaiyun.com/xiaoliuliu2050/article/details/108322985

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值