当前位置:网站首页>Introduction to reflection
Introduction to reflection
2022-07-24 05:21:00 【x0757】
1. What is reflection ?
Reflection is the soul of frame design , frame : It's a semi-finished product , It can be used , Add your own business code . Improve development efficiency .
Reflection is the process of extracting members from a class into other classes . This is reflection .

2. How to get reflection class objects --Class
There are three kinds of :
(1) adopt Class.forName Get the reflection object .
Class.forName(" The full path ") --spring It is the mode used <bean class=" The full path ">(2) By class name .class obtain
Class name .class; --- proxy class --->SqlSession.getMapper(StudentDao.class)(3) Through object .getClass() Method to get
object .getClass(); --- When you know the object, you can get the reflection object in this way
public class Test01 {
public static void main(String[] args) throws ClassNotFoundException {
//1. adopt Class.forName To get reflection class objects .
Class aClass = Class.forName("demo.People");
//2. Called by the class name .class Get the reflection class object
Class aClass1 = People.class;
//3. Get reflection class objects through objects
People p=new People();
Class aClass2 = p.getClass();
// reflection : Whether the reference addresses of the above three reflection objects are consistent ! It's consistent . A class will only be added to memory once .
System.out.println(aClass==aClass1);
System.out.println(aClass2==aClass1);
}
}3. Get the corresponding class object through the reflection class .
public class Test02 {
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
//1. Get the reflection class object
Class<People> aClass = People.class;
//2. Create class objects from reflection classes --- Call a parameterless constructor
People people = aClass.newInstance();
People people2 = aClass.newInstance();
//3. Are their addresses the same
System.out.println(people==people2);
}
}
4. Get the corresponding... Through reflection Field Property object .
public class Test03 {
public static void main(String[] args) throws Exception {
Class<?> aClass = Class.forName("demo01.People");
// Get the attributes specified in this class and its parent class --- It has to be for public Embellished .
Field sex = aClass.getField("sex");
System.out.println(sex);
// Get the property object specified in this class
Field name = aClass.getDeclaredField("name");
System.out.println(name);
// Get all in this class and its parent class public Decorated property object
Field[] fields = aClass.getFields();
for (Field field:
fields) {
System.out.println(field);
}
// Get all property objects in this class
Field[] declaredFields = aClass.getDeclaredFields();
for (Field field:
declaredFields) {
System.out.println(field);
}
}
}4.2Field Common methods in attribute objects .
public class Test04 {
public static void main(String[] args) throws Exception{
Class<People> peopleClass = People.class;
// Gets the name of the property
Field nameFiled = peopleClass.getDeclaredField("name");
String name = nameFiled.getName();
System.out.println(name);
// Assign values to properties
People people = peopleClass.newInstance();
System.out.println(people);
nameFiled.setAccessible(true);
nameFiled.set(people," Wang Wu ");
System.out.println(people);
// Get attribute value
Object o = nameFiled.get(people);
System.out.println(o);
}
}
5. Get the corresponding... Through reflection Method Method object
public class Test05 {
public static void main(String[] args) throws Exception{
Class<People> peopleClass = People.class;
// Get the method object with the specified name in this class and its parent class
Method setName = peopleClass.getMethod("setName", String.class);
System.out.println(setName);
System.out.println("*******");
// Get all in this class and parent class public The method of decoration
Method[] methods = peopleClass.getMethods();
for (Method m :
methods) {
System.out.println(m);
}
System.out.println("+++++++");
// The method object specified in this class
Method fun = peopleClass.getDeclaredMethod("fun");
System.out.println(fun);
System.out.println("=========");
// Get all the methods in this class .
Method[] declaredMethods = peopleClass.getDeclaredMethods();
for (Method m:declaredMethods
) {
System.out.println(m);
}
System.out.println("-------");
People people = peopleClass.newInstance();
setName.invoke(people,"zhangsan");
System.out.println(people);
}
}
5.2 Method Class
//method.invoke( object , Method parameter value );
System.out.println("-------");
People people = peopleClass.newInstance();
setName.invoke(people,"zhangsan");
System.out.println(people);
6. Get the corresponding annotation object
TableFiled tableField = declaredField.getAnnotation(TableFiled.class);
public int insert(T t){
try{
StringBuffer sb = new StringBuffer("insert into ");
Class<?> aClass = t.getClass();
TableName annotation = aClass.getAnnotation(TableName.class);
String tableName ="";
if(annotation!=null){
tableName = annotation.value();
}else {
tableName = aClass.getSimpleName();
}
sb.append(tableName);
System.out.println(sb);
Field[] declaredFields = aClass.getDeclaredFields();
List<String> columns = new ArrayList<>();
List<String> values = new ArrayList<>();
for (Field declaredField:declaredFields){
declaredField.setAccessible(true);
TableFiled tableField = declaredField.getAnnotation(TableFiled.class);
if(tableField!=null){
columns.add(tableField.value());
}else {
columns.add(declaredField.getName());
}
values.add("'"+declaredField.get(t)+"'");
}
sb.append(columns.toString().replace("[","(").replace("]",")"));
sb.append(" values ");
sb.append(values.toString().replace("[","(").replace("]",")"));
System.out.println(sb);
Connection conn = DBUtils.getConn();
PreparedStatement ps = conn.prepareStatement(sb.toString());
int i = ps.executeUpdate();
return i;
}catch (Exception e){
e.printStackTrace();
}finally {
DBUtils.closeAll(rs,ps,connection);
}
return 0;
}
}
边栏推荐
- How to avoid the most common mistakes when building a knowledge base?
- web开发
- 1、基于增量式生成遮挡与对抗抑制的行人再识别
- BeanShell built-in variable CTX
- 跟李沐学ai 线性回归 从零开始的代码实现超详解
- Markov random field: definition, properties, maximum a posteriori probability problem, energy minimization problem
- Pointer learning diary (III)
- Generics and annotations
- Embedded system transplantation [2] - Construction of cross development environment
- scikit-learn笔记
猜你喜欢

Wang Qing, director of cloud infrastructure software research and development of Intel (China): Intel's technology development and prospects in cloud native

T 1-5

Hotel IPTV digital TV system solution
![[deep learning] (III) image classification](/img/8a/86b8ec6951f112f47be816378ab7d8.png)
[deep learning] (III) image classification

T 11-20

AiN 0722 签到

What are the core strengths of a knowledge base that supports customers quickly?

Reading excerpts from Liu run's "bottom logic"

【sklearn】PCA

FTP file transfer protocol
随机推荐
Beginners' preparation for the Blue Bridge Cup (University Programming learning history records, topic ideas for students who need to prepare for the Blue Bridge Cup)
Embedded system transplantation [3] - uboot burning and use
Heavy! The 2022 China open source development blue book was officially released
Create and delete databases using databases
Knowledge record of College Physics C in advance in summer [update]
Context encoders: feature learning by painting paper notes
Echo speaker pairing and operation method
View progress!!! RPM installation!!!
The difference between compiled language and interpreted language
Learning some contents of vector and iterator
AiN 0722 签到
Embedded system transplantation [2] - Construction of cross development environment
Hcip day 3 - mGRE experiment
Drools 开发决策表
Read the summary of "machine learning - Zhou Zhihua"
Tips for using the built-in variable vars in BeanShell
浅谈不可转让的声誉积分NFT SBTs面临的困境
Installation and login login
纯小白教程 在idea里使用Druid数据库连接池
)的低字节来反馈给应用层或者成多种格式文档: