当前位置:网站首页>Reflection + annotation + generics
Reflection + annotation + generics
2022-07-22 18:29:00 【Qwe7】
One 、 Reflection
1.1 What is reflex
Java Reflection of (reflection) The mechanism is to get the information of the class at run time ( Constructors 、 attribute 、 Method 、 annotation ).
1.2 The effect of reflection
1. Create object of class
2. Get properties in class , And set the value of the attribute or get the value of the attribute
3. Get the methods in the class , Methods can be executed ( Include private )
4. Get annotation information
1.3 Application of reflection
1. Create object of class , such as JSON analysis , Some classes in the framework
2. Get or set the content of the property Field such as JSON analysis ,JDBC Package, etc
3. Execution method , You can call Method invoke The proxy pattern
1.4 Demonstration of reflection
Fully qualified class name : The full name of a class , contain : Package name . Class name
public static void main(String[] args) {
// Reflection Class Bytecode establish Class object
//Class Three ways to create objects 1. Class name .class 2. Object name .getClass() 3.Class.forName(" Fully qualified class name ")
//1. establish Class object
Class clz=Teacher.class;
//2. Get the public properties in the class
Field[] fields1=clz.getFields();// Get the properties of the public access modifier in the class , Include inheritance
for(Field f:fields1){
System.out.println(f.getName());
}
System.out.println("---------");
//3. Get all the properties in the class , Does not contain inherited
Field[] fields2=clz.getDeclaredFields();// Get all the properties in the class , Does not contain in the parent class
for(Field f:fields2){
System.out.println(f.getName());
}
System.out.println("---------");
//4. Get public methods
Method[] methods1=clz.getMethods();// Methods to get public access modifiers in classes , Include inheritance
for(Method m:methods1){
System.out.println(m.getName());
}
System.out.println("**********");
//5. Get all methods , Without the parent class
Method[] methods2=clz.getDeclaredMethods();// Get all the methods in the class , Does not contain in the parent class
for(Method m:methods2){
System.out.println(m.getName());
}
}
1.5 Reflection summary
1.Class Object creation method
● Class name .class
● Object name .getClass()
●Class.forName(" Full name class name ")
2.Class The method commonly used in
Return value | Method name | effect |
---|---|---|
Field[] | getFields | Get the properties of the public access modifier in the class , Include inheritance |
Field[] | getDeclaredFields | Get all the properties in the class , Does not contain in the parent class |
Field | getDeclaredField( Property name ) | Get the specified attribute in the class |
Method[] | getMethods | Methods to get public access modifiers in classes , Include inheritance |
Method[] | getDeclaredMethods | Get all the methods in the class , Does not contain in the parent class |
Method | getDeclaredField | Get the specified method in the class |
Object | newInstance | Create object of class |
3.Field The common method of
getName Get the property name
setAccessible Set whether to ignore the access modifier
set() Set the value of the property
4.Method The common method of
setAccessible Set whether to ignore the access modifier
invoke Execution method
1.6 Reflection practical application
Parse string
Java
Copy code
1
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException {
2
String str="name= Bei Bei &sex= Woman &info= narcissism !";
3
// Please convert the string to Teacher object
4
//1. Parse string
5
//2. objects creating Attribute assignment
6
// establish Class object - Reflection
7
Class clz=Teacher.class;
8
// Create object of class
9
Teacher teacher= (Teacher) clz.newInstance();
10
// String common methods Parse string cutting & Array of strings
11
String[] strs1=str.split("&");
12
13
System.out.println(Arrays.toString(strs1));
14
// Loop traversal Key value pair
15
for(String s1:strs1){
16
// cutting =
17
String[] strs2=s1.split("=");
18
System.out.println(Arrays.toString(strs2));
19
// Reflection Get the specified attribute object
20
Field field=clz.getDeclaredField(strs2[0]);
21
if(field!=null){
22
// Set ignore access modifier
23
field.setAccessible(true);
24
// assignment
25
field.set(teacher,strs2[1]);
26
}
27
}
28
System.out.println(teacher);
29
}
边栏推荐
- 面向项目版本差异性的漏洞识别技术研究
- Qt|编辑框的使用总结
- A trick to teach you how to visualize recruitment data ~ is there anyone who can't analyze these data cases?
- Gbase8s database identity connection
- QT notes - unpolish() and polish() of QT dynamic attributes
- Rocky基础练习题-shell脚本-1
- fcntl函数
- 在ubuntu中使用pypyodbc无法连接sql server
- Geowebcache publishes ArcGIS slice data
- 力扣解法汇总814-二叉树剪枝
猜你喜欢
Female guest registration
"Review of software engineering in Wuhan University of technology" Chapter 2 | software process model
Simplify the complexity and talk about the abstraction of replication state machine system architecture
Fcntl function
SQL多条件查询无法实现
Fabric.js 居中元素
「武汉理工大学 软件工程复习」第七章 | 软件测试
Dr. water
文件描述符的复制
Reentrant function
随机推荐
Fabric. JS control element level
力扣解法汇总1260-二维网格迁移
力扣解法汇总1108-IP 地址无效化
力扣解法汇总532-数组中的 k-diff 数对
Reversible information hiding method of image in ciphertext domain based on fine-grained embedding space reservation
[10:00 public class]: cloud video conference system privatization practice
力扣解法汇总513-找树左下角的值
面向项目版本差异性的漏洞识别技术研究
力扣解法汇总535-TinyURL 的加密与解密
【How To 系列】好友裂变平台搭建
力扣解法汇总814-二叉树剪枝
Geowebcache publishes ArcGIS slice data
力扣解法汇总515-在每个树行中找最大值
力扣解法汇总1089-复写零
在ubuntu中使用pypyodbc无法连接sql server
43. String multiplication
力扣解法汇总522-最长特殊序列 II
qt5.12 + vs2019 无法定位程序输入点 于动态链接库
Internet Download Manager2022试用版(简称 IDM)
QT notes - qjason