当前位置:网站首页>反射之类加载过程
反射之类加载过程
2022-07-25 23:32:00 【孙少平】
.java文件 编译(javac.exe) 变成 .class文件 .class文件 类加载(java.exe)进入内存的方法区 并且用.class文件内容创建一个Class对象(类的加载) 接着是,类的链接 打个比方: static int a=2; 在类的链接 是 static int a=0; 而在类的初始化变成 static int a=2; 而三个过程有时统称类加载 


public static void main(String[] args) {
//系统类加载器
ClassLoader classLoader = AnnoTest.class.getClassLoader();
System.out.println(classLoader);//sun.misc.Launcher$AppClassLoader@18b4aac2
//扩展类加载器
ClassLoader parent = classLoader.getParent();
System.out.println(parent);//sun.misc.Launcher$ExtClassLoader@677327b6
//引导类加载器
//此处为null不代表没有引导类加载器,而是无法获取到,只能jvm自己用
ClassLoader parent1 = parent.getParent();
System.out.println(parent1);//null
}

路径:

@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.权限修饰符
int modifier = f.getModifiers();
System.out.print(Modifier.toString(modifier) + "\t");
System.out.println("+++++++++++++++++++++++++++");
//2.数据类型
Class type = f.getType();
System.out.print(type.getName() + "\t");
System.out.println("***************************");
//3.变量名
String fName = f.getName();
System.out.print(fName);
}
}
public class MythodTest {
/**
* @Xxxx
* 权限修饰符 返回值类型 方法名(参数类型1 形参名1,...) throws XxxException{}
*/
@Test
public void test2() {
Class clazz = Person.class;
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method m : declaredMethods) {
//1.获取方法声明的注解
Annotation[] annos = m.getAnnotations();
for (Annotation a : annos) {
System.out.println(a + "KKKK");
}
//2.权限修饰符
System.out.print(Modifier.toString(m.getModifiers()) + "\t");
//3.返回值类型
System.out.print(m.getReturnType().getName() + "\t");
//4.方法名
System.out.print(m.getName());
System.out.print("(");
//5.形参列表
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.抛出的异常
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");
}
}
构造器
public class OtherTest {
/**
* 获取构造器的结构
*/
@Test
public void test(){
Class clazz = Person.class;
//getConstructors():获取当前运行时类中声明为public的构造器
Constructor[] constructors = clazz.getConstructors();
for(Constructor c : constructors){
System.out.println(c);
}
System.out.println("************************");
//getDeclaredConstructors():获取当前运行时类中声明的所有的构造器
Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
for(Constructor c : declaredConstructors){
System.out.println(c);
}
}
}
边栏推荐
- This point inside the function / change this point inside the function
- [QNX Hypervisor 2.2用户手册]9.7 generate
- Computed and watch listening properties
- WordPress function encyclopedia, you can do the theme after learning it
- 【MUDUO】EventLoopThreadPool
- JS get the current date and time
- TS class
- 多模态——Deep Multi-Modal Sets
- 762. Prime number calculation setting in binary representation
- Practical skills of easyexcel
猜你喜欢

Graph traversal DFS, BFS (code explanation)

Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions

多模态——Deep Multi-Modal Sets

电商RPA,大促轻松上阵的法宝

Learning exploration-3d rotation card

Idea sets get and set templates to solve the naming problem of boolean type fields

学习探索-波浪

Duplicate numbers in array

ABAP 代码中读取会计科目的字段状态(隐藏、可选、必输)

Classes and objects (2) (6 default member functions)
随机推荐
【MUDUO】Thread封装
762. Prime number calculation setting in binary representation
serialization and deserialization
Which securities firm is the best and safest for beginners to open an account
TS function
动态内存管理
Classes and objects (3)
Rendering, filtering (filtering) and sorting of lists
The difference between MySQL clustered index and non clustered index
PHP wechat scan code, follow official account and authorize login source code
Learning exploration - waves
Three board axe! Help you become an excellent software engineer
XxE & XML external entity injection utilization and bypass
1913. Maximum product difference between two number pairs - no sorting required
学习探索-波浪
日期类的实现
推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
About the foundation of fetch
Serialize operator
Computed and watch listening properties