当前位置:网站首页>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 :
边栏推荐
- The route of wechat applet jumps again without triggering onload
- H5 module suspension drag effect
- 11-gorm-v2-03-basic query
- Inclusion exclusion principle acwing 890 Divisible number
- [Chongqing Guangdong education] National Open University 2018 autumn 0702-22t contemporary Chinese political system reference questions
- PR automatically moves forward after deleting clips
- Applicable to Net free barcode API [off] - free barcode API for NET [closed]
- VLAN experiment
- Chart. JS - Format Y axis - chart js - Formatting Y axis
- 5.Oracle-錶空間
猜你喜欢

博弈论 AcWing 893. 集合-Nim游戏

博弈论 AcWing 891. Nim游戏

容斥原理 AcWing 890. 能被整除的数

Idea debug failed

Inclusion exclusion principle acwing 890 Divisible number

4. Object mapping Mapster

Day 2 document

Configuration method and configuration file of SolidWorks GB profile library

Alibaba's new member "Lingyang" officially appeared, led by Peng Xinyu, Alibaba's vice president, and assembled a number of core department technical teams

Interval problem acwing 906 Interval grouping
随机推荐
Game theory acwing 894 Split Nim game
3.Oracle-控制文件的管理
[learning] database: several cases of index failure
Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
4. Object mapping Mapster
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
Presentation of attribute value of an item
5.Oracle-表空间
3. Oracle control file management
LSA Type Explanation - lsa-1 [type 1 LSA - router LSA] detailed explanation
Single chip computer engineering experience - layered idea
Vscode creates its own code template
Client use of Argo CD installation
Alibaba established the enterprise digital intelligence service company "Lingyang" to focus on enterprise digital growth
Getting started with typescript
2048项目实现
C job interview - casting and comparing - C job interview - casting and comparing
2022-5-the fourth week daily
Vscode configures the typera editor for MD
our solution