当前位置:网站首页>Loading process such as reflection
Loading process such as reflection
2022-07-25 23:34:00 【Sun Shaoping】
.java file compile (javac.exe) become .class file .class file Class loading (java.exe) Enter the method area of memory And use .class File contents create a Class object ( Class loading ) Next is , Class Links For example : static int a=2; Link in class yes static int a=0; In class initialization, it becomes static int a=2; The three processes are sometimes collectively referred to as class loading 


public static void main(String[] args) {
// system class loader
ClassLoader classLoader = AnnoTest.class.getClassLoader();
System.out.println(classLoader);//sun.misc.Launcher$AppClassLoader@18b4aac2
// Extend the classloader
ClassLoader parent = classLoader.getParent();
System.out.println(parent);//sun.misc.Launcher$ExtClassLoader@677327b6
// Boot class loader
// Here is null It does not mean that there is no boot class loader , But can't get it , Can only jvm Self use
ClassLoader parent1 = parent.getParent();
System.out.println(parent1);//null
}

route :

@Test
public void t1() throws IOException {
InputStream is = AnnoTest.class.getClassLoader().getResourceAsStream("b.properties");
Properties properties = new Properties();
properties.load(is);
String user = properties.getProperty("user");
System.out.println(user);
FileInputStream fis = new FileInputStream("a.properties");
Properties pro = new Properties();
pro.load(fis);
String user1 = pro.getProperty("user");
System.out.println(user1);
}
@Test
public void test2(){
Class clazz = Person.class;
Field[] declaredFields = clazz.getDeclaredFields();
for(Field f : declaredFields){
//1. Permission modifier
int modifier = f.getModifiers();
System.out.print(Modifier.toString(modifier) + "\t");
System.out.println("+++++++++++++++++++++++++++");
//2. data type
Class type = f.getType();
System.out.print(type.getName() + "\t");
System.out.println("***************************");
//3. Variable name
String fName = f.getName();
System.out.print(fName);
}
}
public class MythodTest {
/**
* @Xxxx
* Permission modifier return type Method name ( Parameter type 1 The name of the parameter 1,...) throws XxxException{}
*/
@Test
public void test2() {
Class clazz = Person.class;
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method m : declaredMethods) {
//1. Get annotation for method declaration
Annotation[] annos = m.getAnnotations();
for (Annotation a : annos) {
System.out.println(a + "KKKK");
}
//2. Permission modifier
System.out.print(Modifier.toString(m.getModifiers()) + "\t");
//3. return type
System.out.print(m.getReturnType().getName() + "\t");
//4. Method name
System.out.print(m.getName());
System.out.print("(");
//5. Parameter list
Class[] pTs = m.getParameterTypes();
if(!(pTs == null && pTs.length == 0)){
for(int i = 0;i < pTs.length;i++){
if(i == pTs.length - 1){
System.out.print(pTs[i].getName() + " args_" + i);
break;
}
System.out.print(pTs[i].getName() + " args_" + i + ",");
}
}
System.out.print(")");
//6. Exception thrown
Class[] eTs = m.getExceptionTypes();
if(eTs.length > 0){
System.out.print("throws ");
for(int i = 0;i < eTs.length;i++){
if(i == eTs.length - 1){
System.out.print(eTs[i].getName());
break;
}
System.out.print(eTs[i].getName() + ",");
}
}
System.out.println("TQA");
}
}
Constructors
public class OtherTest {
/**
* Get the structure of the constructor
*/
@Test
public void test(){
Class clazz = Person.class;
//getConstructors(): Get the current class declaration in the runtime public Constructor
Constructor[] constructors = clazz.getConstructors();
for(Constructor c : constructors){
System.out.println(c);
}
System.out.println("************************");
//getDeclaredConstructors(): Get all constructors declared in the current runtime class
Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
for(Constructor c : declaredConstructors){
System.out.println(c);
}
}
}
边栏推荐
- [Muduo] EventLoop event cycle
- 网格参数化Least Squares Conformal Maps实现(3D网格映射到2D平面)
- 疫情之下的好消息
- Node Foundation
- TS class
- Summary of kotlin common knowledge points
- TS union type
- S4/HANA ME21N创建PO 输出控制消息按钮丢失解决方法(切换EDI 输出模式BRF+至NAST模式)
- Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
- idea设置get、set模板解决boolean类型字段的命名问题
猜你喜欢

Numerical learning iota, accumulate

Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download

Duplicate numbers in array

XxE & XML external entity injection utilization and bypass

npm+模块加载机制

SAP Message No. VG202 IDoc E1EDK18 中付款条款已经转移:检查数据

Solution of phpstudy service environment 80 port occupied by process system under Windows

XXE&XML-外部实体注入-利用和绕过

Query commodity cases (operate data with array addition method) / key points
![[JUC] concurrent keyword volatile](/img/80/2f1b33f1e8c87fd4f8806eafb83139.png)
[JUC] concurrent keyword volatile
随机推荐
[JUC] concurrent keyword volatile
物理防火墙是什么?有什么作用?
Serialize addition, deletion, modification and query
从哪些维度评判代码质量的好坏?如何具备写出高质量代码的能力?
numeric学习之iota,accumulate
[QNX hypervisor 2.2 user manual]9.6 GDB
[Muduo] EventLoop event cycle
Rendering, filtering (filtering) and sorting of lists
SAP Message No. VG202 IDoc E1EDK18 中付款条款已经转移:检查数据
[QNX Hypervisor 2.2用户手册]9.8 load
Duplicate numbers in array
反射之类加载过程
LeetCode 0919. 完全二叉树插入器:完全二叉树的数组表示
数组中重复的数字
Graph traversal DFS, BFS (code explanation)
762. Prime number calculation setting in binary representation
@Autowired annotation required attribute
Vscode shortcut key: collapse and expand code
idea设置get、set模板解决boolean类型字段的命名问题
Kotlin 常用知识点汇总