Reflection in JAVA!

Insert picture description here

Overview

The JAVA reflection mechanism is to obtain the structure of any class, create objects, get methods, execution methods, and attributes in the running state. This function of dynamically acquiring information and dynamically calling object methods in the running state is called the reflection mechanism of the Java language. .

Class loader

Java类加载器(Java Classloader)是Java运行时环境(Java Runtime Environment)的一部分,
负责动态加载Java类到Java虚拟机的内存空间中。
java默认有三种类加载器,BootstrapClassLoader、ExtensionClassLoader、App
ClassLoader。
BootstrapClassLoader(引导启动类加载器):
嵌在JVM内核中的加载器,该加载器是用C++语言写的,主要负载加载JAVA_HOME/lib下的类库,引
导启动类加载器无法被应用程序直接使用。
ExtensionClassLoader(扩展类加载器):
ExtensionClassLoader是用JAVA编写,且它的父类加载器是Bootstrap。
是由sun.misc.Launcher$ExtClassLoader实现的,主要加载JAVA_HOME/lib/ext目录中的类
库。
它的父加载器是BootstrapClassLoader
App ClassLoader(应用类加载器):
App ClassLoader是应用程序类加载器

Insert picture description here

类通常是按需加载,即第一次使用该类时才加载。由于有了类加载器,Java运行时系统不需要知道文件与
文件系统。学习类加载器时,掌握Java的委派概念很重要。
双亲委派模型:如果一个类加载器收到了一个类加载请求,它不会自己去尝试加载这个类,而是把这个请求
转交给父类加载器去完成。每一个层次的类加载器都是如此。因此所有的类加载请求都应该传递到最顶层的
启动类加载器中,只有到父类加载器反馈自己无法完成这个加载请求(在它的搜索范围没有找到这个类)
时,子类加载器才会尝试自己去加载。委派的好处就是避免有些类被重复加载。

Several ways to obtain Class

1. 如果在编写代码时, 指导类的名称, 且类已经存在, 可以通过
包名.类名.class 得到一个类的 类对象
2. 如果拥有类的对象, 可以通过
Class 对象.getClass() 得到一个类的 类对象
3. 如果在编写代码时, 知道类的名称 , 可以通过
Class.forName(包名+类名): 得到一个类的 类对象

Get Constructor

Get the construction method of a class through the class object

1. 通过指定的参数类型, 获取指定的单个构造方法
getConstructor(参数类型的class对象数组)
例如:
构造方法如下: Person(String name,int age)
得到这个构造方法的代码如下:
Constructor c = p.getClass().getConstructor(String.class,int.class);
2. 获取构造方法数组
getConstructors();
3. 获取所有权限的单个构造方法
getDeclaredConstructor(参数类型的class对象数组)
4. 获取所有权限的构造方法数组
getDeclaredConstructors();

Constructor creates objects

常用方法:
newInstance(Object... para)
调用这个构造方法, 把对应的对象创建出来
参数: 是一个Object类型可变参数, 传递的参数顺序 必须匹配构造方法中形式参数列表的顺
序!
setAccessible(boolean flag)
如果flag为true 则表示忽略访问权限检查 !(可以访问任何权限的方法)```
## 获取Method
### 通过class对象 获取一个类的方法

```java
1. getMethod(String methodName , class.. clss)
根据参数列表的类型和方法名, 得到一个方法(public修饰的)
2. getMethods();
得到一个类的所有方法 (public修饰的)
3. getDeclaredMethod(String methodName , class.. clss)
根据参数列表的类型和方法名, 得到一个方法(除继承以外所有的:包含私有, 共有, 保护, 默认)
4. getDeclaredMethods();
得到一个类的所有方法 (除继承以外所有的:包含私有, 共有, 保护, 默认)

Method

invoke(Object o,Object... para) :
调用方法 ,
参数1. 要调用方法的对象
参数2. 要传递的参数列表
getName()
获取方法的方法名称
setAccessible(boolean flag)
如果flag为true 则表示忽略访问权限检查 !(可以访问任何权限的方法)

Get Field

Get the attributes of a class through the class object

1. getDeclaredField(String filedName)
根据属性的名称, 获取一个属性对象 (所有属性)
2. getDeclaredFields()
获取所有属性
3. getField(String filedName)
根据属性的名称, 获取一个属性对象 (public属性)
4. getFields()
获取所有属性 (public)

Object type of Field property

常用方法:
1. get(Object o );
参数: 要获取属性的对象
获取指定对象的此属性值
2. set(Object o , Object value);
3.8、获取注解信息
3.8.1、获取类/属性/方法的全部注解对象
3.8.2、根据类型获取类/属性/方法的注解对象
4、内省
4.1、简介
4.2、Introspector
参数1. 要设置属性值的 对象
参数2. 要设置的值
设置指定对象的属性的值
3. getName()
获取属性的名称
4. setAccessible(boolean flag)
如果flag为true 则表示忽略访问权限检查 !(可以访问任何权限的属性)

Get annotation information

Get all annotation objects of the class/attribute/method

Annotation[] annotations01 = Class/Field/Method.getAnnotations();
for (Annotation annotation : annotations01) {
    
    
System.out.println(annotation);
}

Get the annotation object of the class/attribute/method according to the type

注解类型 对象名 = (注解类型) c.getAnnotation(注解类型.class);

introspection

Introduction

基于反射 , java所提供的一套应用到JavaBean的API
一个定义在包中的类 ,
拥有无参构造器
所有属性私有,
所有属性提供get/set方法
实现了序列化接口
这种类, 我们称其为 bean类 .
Java提供了一套java.beans包的api , 对于反射的操作, 进行了封装 !

Introspector

获取Bean类信息
方法:
BeanInfo getBeanInfo(Class cls)
通过传入的类信息, 得到这个Bean类的封装对象 .

BeanInfo

常用的方法:
MethodDescriptor[] getPropertyDescriptors():
获取bean类的 get/set方法 数组

MethodDescriptor

常用方法:
1. Method getReadMethod();
获取一个get方法
2. Method getWriteMethod();
获取一个set方法
有可能返回null 注意 ,加判断 !

Insert picture description here

Guess you like

Origin blog.csdn.net/AzirBoDa/article/details/113788826