当前位置:网站首页>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);
}
}
}
边栏推荐
- E-commerce RPA, a magic weapon to promote easy entry
- Es5 new method
- Multimodal deep multi modal sets
- 【MUDUO】EventLoopThreadPool
- This point inside the function / change this point inside the function
- 【JUC】并发需要了解的关键字volatile
- BI 系统中为什么会有很多快照表?
- ABAP 代码中读取会计科目的字段状态(隐藏、可选、必输)
- TS interface
- [Database Foundation] summary of MySQL Foundation
猜你喜欢
随机推荐
XxE & XML external entity injection utilization and bypass
ES6 syntax (difference between let, const, VaR, deconstruction assignment, arrow function, residual parameters, extension method of array)
Family relationship calculator wechat applet source code
[QNX hypervisor 2.2 user manual]9.7 generate
@Import
2022 Niuke multi School Game 2
Mongodb query and projection operators
Overview of MES system equipment management (Part 2)
ETL tool (data synchronization) II
LeetCode 0135. 分发糖果
三板斧!助你成为优秀软件工程师
Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
[wechat applet] page navigation
1913. 两个数对之间的最大乘积差-无需排序法
新手哪个券商开户最好 开户最安全
Serialize addition, deletion, modification and query
谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
About the foundation of fetch
Recommended system - an embedded learning framework for numerical features in CTR prediction








![[nodejs] nodejs create a simple server](/img/00/7ab5629bf67777da21b41f44665880.png)
