当前位置:网站首页>Get class files and attributes by reflection
Get class files and attributes by reflection
2022-07-05 06:36:00 【Said it was all】
The following code is to be obtained by using the reflection mechanism :
package com.zzuli;
/** * @author luoqingchen * @reate 2022-02-03-9:46 */
public class reflect01 {
private boolean flag;
public int a;
protected String s;
}
Reflection acquisition :
public class Test01 {
public static void main(String[] args) throws Exception {
Class reflect=Class.forName("com.zzuli.reflect01");
StringBuilder s=new StringBuilder();
s.append(Modifier.toString(reflect.getModifiers())+" class "+reflect.getSimpleName());
s.append("\n");
s.append("{");
s.append("\n");
Field[] fields= reflect.getDeclaredFields();
for (Field fs:fields){
s.append("\t\t");
s.append( Modifier.toString(fs.getModifiers()));
s.append(" ");
s.append(fs.getType().getSimpleName());
s.append(" ");
s.append(fs.getName());
s.append("\n");
}
s.append("\n");
s.append("}");
System.out.println(s);
}
}
Running results :
Use reflection to get the attribute value in the class :
public class Test02 {
public static void main(String[] args) throws Exception{
Class reflect=Class.forName("com.zzuli.reflect01");
Object obj=reflect.newInstance();
// get attribute
Field field=reflect.getDeclaredField("a");
// Setting property values
field.set(obj,8);
Object obj2=field.get(obj);
// Get the attribute value
System.out.println(obj2);
}
}
Running results :( This example is getting public The value of the property )
Want to get the value of the private attribute , Need to break the package :
public class Test02 {
public static void main(String[] args) throws Exception{
Class reflect=Class.forName("com.zzuli.reflect01");
Object obj=reflect.newInstance();
// get attribute
Field field=reflect.getDeclaredField("flag");
// Break the package
field.setAccessible(true);
// Setting property values
field.set(obj,true);
Object obj2=field.get(obj);
// Get the attribute value
System.out.println(obj2);
}
}
Running results :
边栏推荐
- Presentation of attribute value of an item
- 11-gorm-v2-02-create data
- 高斯消元 AcWing 884. 高斯消元解异或線性方程組
- H5 embedded app adapts to dark mode
- how to understand the “model independent.“
- 求组合数 AcWing 887. 求组合数 III
- Vant weapp swippecell set multiple buttons
- 高斯消元 AcWing 884. 高斯消元解异或线性方程组
- SQL三种连接:内连接、外连接、交叉连接
- Sum of three terms (construction)
猜你喜欢
Game theory acwing 892 Steps Nim game
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
NVM Downloading npm version 6.7.0... Error
将webApp或者H5页面打包成App
代码中的英语全部
Some classic recursion problems
LeetCode-61
P2575 master fight
区间问题 AcWing 906. 区间分组
2. Addition and management of Oracle data files
随机推荐
Find the combination number acwing 888 Find the combination number IV
The “mode“ argument must be integer. Received an instance of Object
MySQL (UDF authorization)
Find the combination number acwing 889 01 sequence meeting conditions
Winter messenger 2
2022/6/29-日报
ADG5412FBRUZ-RL7应用 双电源模拟开关和多路复用器IC
[QT] QT multithreading development qthread
Vscode configures the typera editor for MD
安装OpenCV--conda建立虚拟环境并在jupyter中添加此环境的kernel
There are three kinds of SQL connections: internal connection, external connection and cross connection
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
Inclusion exclusion principle acwing 890 Divisible number
11-gorm-v2-03-basic query
ollvm编译出现的问题纪录
SQL三种连接:内连接、外连接、交叉连接
20220213-CTF MISC-a_ good_ Idea (use of stegsolve tool) -2017_ Dating_ in_ Singapore
Vant weave swipecell sets multiple buttons
博弈论 AcWing 892. 台阶-Nim游戏
5. Oracle tablespace