java.lang.reflect(Java反射包)

23 篇文章 0 订阅

Java反射包是我们日常经常接触并且也是Java的基础内容,这篇文章就是为了方便学习记录Java反射包下相关的内容,巩固精进Java基础。

一、简介

java.lang.reflect包提供了用于获取类和对象的反射信息的类和接口。反射API允许对程序访问有关加载类的字段,方法和构造函数的信息进行编程访问。它允许在安全限制内使用反射的字段,方法和构造函数对其底层对等进行操作

二、Java反射包关系图

在这里插入图片描述

三、Java反射包下类和接口简介

我的目的是为了与字节码对象Class一起关联介绍(可以通过Class获取这里的工具类对象),具体介绍后续有时间再补充,这里的内容就是来自源码中的官方介绍,大家可以自行查阅源码。
这里暂是介绍了部分常见的接口或类,后续有时间深入研究介绍reflect包下的所有接口和类。
(1)AccessibleObject

The AccessibleObject class is the base class for Field, Method and Constructor objects. It provides the ability to flag a reflected object as suppressing default Java language access control checks when it is used. The access checks--for public, default (package) access, protected, and private members--are performed when Fields, Methods or Constructors are used to set or get fields, to invoke methods, or to create and initialize new instances of classes, respectively.
Setting the accessible flag in a reflected object permits sophisticated applications with sufficient privilege, such as Java Object Serialization or other persistence mechanisms, to manipulate objects in a manner that would normally be prohibited.
By default, a reflected object is not accessible.
自从:
1.2
请参阅:
Field, Method, Constructor, ReflectPermission

翻译:
AccessibleObject类是字段、方法和构造函数对象的基类。它提供了在使用反射对象时将其标记为抑制默认Java语言访问控制检查的能力。当Fields、Methods或constructor分别用于设置或获取字段、调用方法或创建和初始化类的新实例时,执行访问检查(对于公共、默认()访问、保护和私有成员)。
在反射对象中设置可访问标志允许具有足够特权的复杂应用程序(如Java对象序列化或其他持久性机制)以通常禁止的方式操作对象。
默认情况下,反射对象是不可访问的。

(2)AnnotatedArrayType

AnnotatedArrayType represents the potentially annotated use of an array type, whose component type may itself represent the annotated use of a type.
自从:
1.8

翻译:
AnnotatedArrayType表示对数组类型的潜在注释使用,该数组类型的组件类型本身可能表示对某个类型的注释使用。

(3)AnnotatedElement

Represents an annotated element of the program currently running in this VM. This interface allows annotations to be read reflectively. All annotations returned by methods in this interface are immutable and serializable. The arrays returned by methods of this interface may be modified by callers without affecting the arrays returned to other callers.

翻译:
表示当前在此VM中运行的程序的注释元素。这个接口允许以反射方式读取注释。该接口中方法返回的所有注释都是不可变和可序列化的。调用方可以修改该接口的方法返回的数组,而不会影响返回给其他调用方的数组。

(4)AnnotatedParameterizedType

(5)AnnotatedType

(6)AnnotatedTypeVariable

(7)AnnotatedWildcardType

(8)Array

The Array class provides static methods to dynamically create and access Java arrays.
Array permits widening conversions to occur during a get or set operation, but throws an IllegalArgumentException if a narrowing conversion would occur.
作者:
Nakul Saraiya

翻译:
Array类提供了动态创建和访问Java数组的静态方法。
Array允许在get或set操作期间发生扩展转换,但如果发生收缩转换,则抛出IllegalArgumentException。

(9)Constructor

Constructor provides information about, and access to, a single constructor for a class.
Constructor permits widening conversions to occur when matching the actual parameters to newInstance() with the underlying constructor's formal parameters, but throws an IllegalArgumentException if a narrowing conversion would occur.
请参阅:
Member, Class, Class.getConstructors(), Class.getConstructor(Class[]), Class.getDeclaredConstructors()
作者:
Kenneth Russell, Nakul Saraiya
类型参数:
<T> – the class in which the constructor is declared

翻译:
构造函数提供关于类的单个构造函数的信息和访问。
当将newInstance()的实际形参与底层构造函数的形参匹配时,构造函数允许进行扩展转换,但如果发生收缩转换,则抛出IllegalArgumentException。

注意:
Java.lang.Class或者java.lang.reflect.Constructor类的newInstance()都能实例化对象。

(10)Executable

A shared superclass for the common functionality of Method and Constructor.
自从:
1.8

翻译:
一个用于方法和构造函数的公共功能的共享超类。

(11)Field

A Field provides information about, and dynamic access to, a single field of a class or an interface. The reflected field may be a class (static) field or an instance field.
A Field permits widening conversions to occur during a get or set access operation, but throws an IllegalArgumentException if a narrowing conversion would occur.
请参阅:
Member, Class, Class.getFields(), Class.getField(String), Class.getDeclaredFields(), Class.getDeclaredField(String)
作者:
Kenneth Russell, Nakul Saraiya

翻译:
Field提供关于类或接口的单个字段的信息和动态访问。反射的字段可以是类(静态)字段或实例字段。
Field允许在获取或设置访问操作期间发生扩展转换,但如果发生收缩转换,则抛出IllegalArgumentException。

(12)GenericArrayType

(13)GenericDeclaration

(14)GenericSignatureFormatError

(15)InvocationHandler

(16)InvocationTargetException

(17)MalformedParameterizedTypeException

(18)MalformedParametersException

(19)Member

Member is an interface that reflects identifying information about a single member (a field or a method) or a constructor.
请参阅:
Class, Field, Method, Constructor
作者:
Nakul Saraiya

翻译:
成员是反映关于单个成员(字段或方法)或构造函数的标识信息的接口。

(20)Method

A Method provides information about, and access to, a single method on a class or interface. The reflected method may be a class method or an instance method (including an abstract method).
A Method permits widening conversions to occur when matching the actual parameters to invoke with the underlying method's formal parameters, but it throws an IllegalArgumentException if a narrowing conversion would occur.
请参阅:
Member, Class, Class.getMethods(), Class.getMethod(String, Class[]), Class.getDeclaredMethods(), Class.getDeclaredMethod(String, Class[])
作者:
Kenneth Russell, Nakul Saraiya

翻译:
方法提供关于类或接口上单个方法的信息和访问。反射的方法可以是一个类方法或一个实例方法(包括一个抽象方法)。
当将要调用的实际参数与底层方法的形式参数匹配时,方法允许进行扩展转换,但如果发生收缩转换,则抛出IllegalArgumentException。

(21)Modifier

The Modifier class provides static methods and constants to decode class and member access modifiers. The sets of modifiers are represented as integers with distinct bit positions representing different modifiers. The values for the constants representing the modifiers are taken from the tables in sections 4.1, 4.4, 4.5, and 4.7 of The Java™ Virtual Machine Specification.
请参阅:
Class.getModifiers(), Member.getModifiers()
作者:
Nakul Saraiya, Kenneth Russell

翻译:
Modifier类提供了静态方法和常量来解码类和成员访问修饰符。修饰符的集合被表示为整数,具有代表不同修饰符的不同位位。表示修饰符的常量的值取自Java™虚拟机规范的4.14.44.54.7节中的表。

(22)Parameter

Information about method parameters. A Parameter provides information about method parameters, including its name and modifiers. It also provides an alternate means of obtaining attributes for the parameter.
自从:
1.8

翻译:
关于方法参数的信息。参数提供关于方法参数的信息,包括它的名称和修饰符。它还提供了获取参数属性的另一种方法。

(23)ParameterizedType

(24)Proxy

Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.

翻译:
Proxy提供了创建动态代理类和实例的静态方法,它也是由这些方法创建的所有动态代理类的超类。

(25)ReflectAccess

(26)ReflectPermission

(27)Type

(28)TypeVariable

(29)UndeclaredThrowableException

(30)WeakCache

(31)WildcardType

附录:

我们可以通过反射获取字节码对象(java.lang.Class)进而得到的Java反射包下的工具类对象,从而对字节码对象进行操作。
后续会另写博客针对Class对象中的一些列常用操作,与本文相关联。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值