reflection - reflection - detail freak

Definition of Reflection

The reflection mechanism of java is that in the running state, for any class, you can know all the properties and methods of this class; for any object, you can call any of its methods and properties, since you can get it, Then, we can modify part of the type information; this function of dynamically obtaining information and dynamically calling object methods is called the reflection mechanism of the java language.


Uses of reflection - may be asked in an interview

1. In the daily third-party application development process, it is often encountered that a member variable, method or attribute of a certain class is private , or only open to system applications. At this time, the reflection mechanism of java can be used. Obtain the required private members or methods through reflection. 2. The most important use
of reflection is to develop various general frameworks . For example, in spring, we hand over all class beans to the spring container for management, whether it is XML configuration bean or annotation configuration, when we obtain beans from the container to rely on When injecting, the container will read the configuration, and the configuration will give the class information. According to this information, spring needs to create those beans, and spring will dynamically common these classes.


Basic information on reflection

Many objects in java programs have two types at runtime: runtime type (RTTI) and compile-time type, for example: Person p = new Student(); in this code, the type of p at compile time is Person, and the runtime type for Student.
The program needs to discover the real information of objects and classes at runtime, and by using reflection, it can determine which classes the objects and classes belong to.


Reflection related classes (important)

class name use
Class An entity representing a class, representing classes and interfaces in a running Java application
Field class Represents a member variable of a class/property of a class
Method class method of class
Constructor class Delegate class constructor

Class (the origin of the reflection mechanism)

The Class help document represents the entity of the class, and represents the class and interface in the running Java application.
insert image description here

After the Java file is compiled, a .class file is generated. At this time, the JVM will interpret the .class file and the compiled Java file. The class is also parsed by the JVM as an object, which is java.lang.Class. In this way, when the program is running, each class file eventually becomes an instance of the Class class object.

By applying Java's reflection mechanism to this instance, we can obtain and even add and change the properties and actions of this class, making this class a dynamic class.


Related methods in the Class class

(Important) Commonly used methods to obtain class-related

method use
getClassLoader() get the class loader
getDeclaredClasses() Returns an array containing objects of all classes and interface classes in this class (including private ones)
forName (String className) Returns an object of a class based on the class name
newInstance() Create an instance of the class
getName() Get the full path name of the class

(Important) Commonly used methods for obtaining attributes in classes (the return values ​​of the following methods are related to Field)

Field Documentation
insert image description here

method use
getField(String name) Get a public property object
getFields() Get all public property objects
getDeclaredField(String name) get a property object
getDeclaredFields() get all property objects

(Understand) Get the methods related to annotations in the class

method use
getAnnotation(Class annotationClass) Returns the public annotation object in this class that matches the parameter type
getAnnotations() Returns all public annotation objects of this class
getDeclaredAnnotation(Class annotationClass) Returns all annotation objects in this class that match the parameter type
getDeclaredAnnotations() Returns all annotation objects of this class

(Important) Get the constructor-related methods in the class

以下方法返回值为Constructor相关
insert image description here

方法 用途
getConstructor(Class…<?> parameterTypes) 获得该类中与参数类型匹配的公有构造方法
getConstructors() 获得该类的所有公有构造方法
getDeclaredConstructor(Class…<?> parameterTypes) 获得该类中与参数类型匹配的构造方法
getDeclaredConstructors() 获得该类所有构造方法

(重要)获得类中方法相关的方法

以下方法返回值为Method相关
insert image description here

方法 用途
getMethod(String name, Class…<?> parameterTypes) 获得该类某个公有的方法
getMethods() 获得该类所有公有的方法
getDeclaredMethod(String name, Class…<?> parameterTypes) 获得该类某个方法
getDeclaredMethods() 获得该类所有方法

反射示例

获得Class对象的三种方式

在反射之前,我们需要做的第一步就是先拿到当前需要反射的类的Class对象,然后通过Class对象的核心方法,达到反射的目的,
即:在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;
对于任意一个对象,都能够调用它的任意方法和属性,既然能拿到那么,我们就可以修改部分类型信息


第一种,使用 Class.forName(“类的全路径名”); 静态方法。 - 用的最多

前提:已明确类的全路径名。
知识铺垫:Exception - 异常 - Java - 细节狂魔

insert image description here


第二种,使用 .class 方法。

说明:仅适合在编译前就已经明确要操作的 Class
insert image description here


第三种,使用类对象的 getClass() 方法 - 用的最少

insert image description here


三种方法的测试 与 比较

测试一:三种方法获取的对象 是否是同一个?

insert image description here

不管使用哪种方式,来获取Class 对象,此时这个对象只有一个。


反射的使用

接下来我们开始使用反射,我们依旧反射上面的Student类,把反射的逻辑写到另外的类当中进行理解
注意:所有和反射相关的包都在 import java.lang.reflect 包下面。


实例化 通过 反射获取的对象。

insert image description here

效果图

insert image description here


反射私有的构造方法

insert image description here

效果图

insert image description here


结论

记住!!! 在利用反射访问 私有的 属性/方法 之前,先把获取的 私有的 属性/方法 的权限“打开”【 ().setAccessible(true)】,要不然无法访问。


反射私有属性

insert image description here


效果图

insert image description here


反射私有方法

insert image description here


效果图

insert image description here


反射优点和缺点

优点:

1、 对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法
2、 增加程序的灵活性和扩展性,降低耦合性,提高自适应能力
3、反射已经运用在了很多流行框架如:Struts、Hibernate、Spring 等等。


缺点

1、 使用反射会有效率问题。会导致程序效率降低。参考资料链接
【其实很容易理解,你随便往上翻一个例子,获取属性或者方法都可以,你发现 调用的方法非常多。调用这些方法也是需要时间和空间的 啊!那就很自然的就能联想到会降低程序的效率嘛。】

2、 反射技术绕过了源代码的技术,因而会带来维护问题。反射代码比相应的直接代码维护更复杂
【总结:你这用了反射,你让我来维护????我想说:能带着大家一起吗?】


这些重点部分一定要熟悉

1. 反射的意义

For any class, we can know all the properties and methods of this class; for any object, we can call any of its methods and properties. Since we can get it, then we can modify some type information; this dynamic The ability to get information and dynamically call object methods is what we want.

2. Several important classes of reflection: Class class, Field class, Method class, Constructor class - as long as reflection is stabilized in the interview, 85% probability is this
insert image description here

3. Learn to use reflection reasonably, and use it in a safe environment
.

Guess you like

Origin blog.csdn.net/DarkAndGrey/article/details/123187015