当前位置:网站首页>Notes and reflection notes
Notes and reflection notes
2022-07-23 05:30:00 【CHY resounding】
Catalog
3. Bottom layer and usage analysis of reflection
(4) Get the structure of the class runtime
(5) Dynamically create object execution methods
(7) Get annotation information
1. annotation





2. Reflection preliminary
(1) summary :



(2)class class
ps: The entity class is slightly different from the previous one , Entity class It's a person who has Set and Get Class of method . Entity classes are usually related to databases ( The so-called persistent layer data ) Connect . This connection is through the framework ( Such as Hibernate) To build .






//class Class creation method
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
Person person = new Student();
System.out.println(" This is a :" + person.name);
// Mode one : Get... By object
Class c1 = person.getClass();
System.out.println(c1.hashCode());
// Method 2 :forname get
Class<?> c2 = Class.forName("com.example.Student");
System.out.println(c2.hashCode());
// Method 3 : By class name
Class c3=Student.class;
System.out.println(c3.hashCode());
// Method four : The wrapper class of the basic built-in type has a type attribute , But only limited to basic built-in types
Class<Integer> type = Integer.TYPE;
}
}
class Person {
public String name;
public double height;
public Person() {
}
public Person(String name, double height) {
this.name = name;
this.height = height;
}
/**
* obtain
*
* @return name
*/
public String getName() {
return name;
}
/**
* Set up
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* obtain
*
* @return height
*/
public double getHeight() {
return height;
}
/**
* Set up
*
* @param height
*/
public void setHeight(double height) {
this.height = height;
}
public String toString() {
return "Person{name = " + name + ", height = " + height + "}";
}
}
class Student extends Person {
public Student() {
name = " Student ";
}
}
class Teacher extends Person {
public Teacher() {
name = " teacher ";
}
}
// There's another. getsuperclass Get the parent type result :

(3)class Type of object

PS: Select and press alt The key can be copied vertically as a whole
test :

For example, below , Two different lengths int Array , Only one-dimensional arrays , Then there is only one class


3. Bottom layer and usage analysis of reflection
(1) Class loading

The parsing step in the link :

Example :

That's why you pinch ? First load the bytecode of the class in the method area , Then create in the heap Class Class object , Then enter the stack main Method , Then continue to call ......

Link phase m Default initialization is 0, then new A , Right A Initialize this object of , So combine assignment and static code blocks , So actually , This is the order m=300;m=100; Last m Is equal to 100

(2) Class initialization

Active loading is a common method , Here is a passive reference :

Here we call static int b=2, So only load Main And the father

Nothing is done here , Just main To load the , No load son class
M Is a constant in the parent class

Then constants will not cause the loading of parent or child classes
Just one main
(3) Class loader

Caching for efficiency



null It means loaded by the root loader .

expand :
Parent delegate mechanism , Guarantee safety , For example, you wrote a java.lang.String, He will always find the root loader , If you find the system here, there is also one Java.lang.String, Then he won't write it yourself , You can't write it yourself , To ensure safety and efficiency
(4) Get the structure of the class runtime

test :



(5) Dynamically create object execution methods


Law two :





We can already get the method through reflection 、 Field 、 Constructors , You can also create objects through them 、 Calling method 、 Operation properties .
Be careful , Reflection is relatively slow :




(6) Reflection generics

here test01(Map<>,list<>){
}, And here .class get class Object and then getMethod Access method , Reuse Type【】 Generics accept parameter types
received map and list, Then we also know map,list What exactly is inside , It needs to be used. instance of Determine whether these generics are the parameter types of this method , If it is another strong turn , Use geractualtype, use Type【】 Accept the return value of parameter type , So as to know all 

Next, we will examine the return value type , It's the same logic

(7) Get annotation information

Finally, I tried it myself :
// Practice reflection operation notes
public class Test2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class<?> c1 = Class.forName("com.example.Test2");
// Get annotations by reflection
Annotation[] annotations = c1.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
// The English of annotation is annotation
// Get the specified annotation value value
Taketake take = c1.getAnnotation(Taketake.class);
String value=take.value();
System.out.println(value);
// Get the specified annotation of the class
Field f =c1.getDeclaredField("name");
Fielddo annotation = f.getAnnotation(Fielddo.class);
System.out.println(annotation.columnName());
System.out.println(annotation.length());
System.out.println(annotation.type());
}
}
@Taketake("student_db")
class Student {
@Fielddo(columnName = "name_db", type = "varchar", length = 3)
private String name;
@Fielddo(columnName = "age_db", type = "int", length = 10)
private String age;
public Student() {
}
public Student(String name, String age) {
this.name = name;
this.age = age;
}
/**
* obtain
*
* @return name
*/
public String getName() {
return name;
}
/**
* Set up
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* obtain
*
* @return age
*/
public String getAge() {
return age;
}
/**
* Set up
*
* @param age
*/
public void setAge(String age) {
this.age = age;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
}
// Annotation of class name :
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Taketake {
String value();
}
// Comment for attribute :
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Fielddo {
String columnName();
String type();
int length();
}In the future, some annotations may be defined in the class , Then read and generate the corresponding information through the reflection framework . Suppose there is a table in the database , We can define the type corresponding to this table , Then generate some database languages through annotations for addition, deletion, modification and query
The course of choice :【 Madness theory Java】 Annotation and reflection _ Bili, Bili _bilibili
边栏推荐
- C语言中带指针和不带指针交换变量值
- leetcode-528.按权重随机选择
- Verilog pit avoidance Guide (continuously updated)
- Leetcode-241. design priorities for operational expressions
- APB protocol details vs. 3.0-4.0-5.0
- 正则表达式的应用及分组
- libcurl重定向
- Event delegation & form validation & regular expression
- Hefei University of technology information theory and coding course design, including code, visual interface, course design report
- 浏览器插件损坏怎么办,删除|安装插件的方法
猜你喜欢

二叉树—堆

大一暑假实习day7总结

Druid source code read the init process of 2-druiddatasource

注解和反射笔记

Code random notes_ Linked list_ 160 intersecting linked list

Leetcode-213. house raiding II

Construction training camp module II operation

Ctfshow VIP limited and free topics CSDN creative punch in

代码随想录笔记_链表_24两两交换链表中的节点

PR初始入门
随机推荐
Druid source code reading 5-druiddatasource's shrink process
Watermelon book machine learning notes -- naive Bayes
Code random notes_ Array_ 704 binary search
Leetcode-241. design priorities for operational expressions
代码随想录笔记_链表_160相交链表
Solve the master-slave replication problem of MySQL under CentOS
Ctfshow VIP limited and free topics CSDN creative punch in
leetcode-494.目标和
12月1日作业
leetcode-528.按权重随机选择
正则表达式的应用及分组
树和二叉树(C语言)
《ctfshow vip限免题目|CSDN创作打卡》
Source insight - create new projects and solve Chinese garbled code
AHB agreement related
leetcode-326. 3的幂
MySQL
Druid source code read the getconnection process of 4-druiddatasource
Apple 注销账户 Revoke Token
如何用C语言实现无头单向非循环链表Single List ?