Skip to content

Commit 90966bc

Browse files
committed
UserClassLoader__类加载
1 parent 89b1aa8 commit 90966bc

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.cpucode.java.loader;
2+
3+
import java.io.*;
4+
5+
/**
6+
* @author : cpucode
7+
* @date : 2021/2/3
8+
* @time : 16:47
9+
* @github : https://github.com/CPU-Code
10+
* @csdn : https://blog.csdn.net/qq_44226094
11+
*/
12+
public class UserClassLoader extends ClassLoader{
13+
private String rootDir;
14+
15+
public static void main(String[] args) {
16+
String rootDir = "D:\\Date\\github\\java\\JVM\\src\\";
17+
18+
try{
19+
//创建自定义的类的加载器1
20+
UserClassLoader loader1 = new UserClassLoader(rootDir);
21+
Class clazz1 = loader1.findClass("com.cpucode.java.loader.User");
22+
23+
//创建自定义的类的加载器2
24+
UserClassLoader loader2 = new UserClassLoader(rootDir);
25+
Class clazz2 = loader2.findClass("com.cpucode.java.loader.User");
26+
27+
System.out.println(clazz1.getClassLoader());
28+
System.out.println(clazz2.getClassLoader());
29+
30+
//clazz1与clazz2对应了不同的类模板结构
31+
System.out.println(clazz1 == clazz2);
32+
33+
/*****************************************************/
34+
35+
Class clazz3 = ClassLoader.getSystemClassLoader().loadClass("com.cpucode.java.loader.User");
36+
System.out.println(clazz3.getClassLoader());
37+
38+
System.out.println(clazz1.getClassLoader().getParent());
39+
}catch (ClassNotFoundException e){
40+
e.printStackTrace();
41+
}
42+
43+
/**
44+
* com.cpucode.java.loader.UserClassLoader@330bedb4
45+
* com.cpucode.java.loader.UserClassLoader@4b67cf4d
46+
* false
47+
* sun.misc.Launcher$AppClassLoader@18b4aac2
48+
* sun.misc.Launcher$AppClassLoader@18b4aac2
49+
* */
50+
}
51+
52+
public UserClassLoader(String rootDir){
53+
this.rootDir = rootDir;
54+
}
55+
56+
/**
57+
* 编写findClass方法的逻辑
58+
* */
59+
@Override
60+
protected Class<?> findClass(String name) throws ClassNotFoundException{
61+
// 获取类的class文件字节数组
62+
byte[] classData = getClassData(name);
63+
64+
if (classData == null){
65+
throw new ClassNotFoundException();
66+
}else {
67+
//直接生成class对象
68+
return defineClass(name, classData, 0, classData.length);
69+
}
70+
}
71+
72+
/**
73+
* 编写获取class文件并转换为字节码流的逻辑
74+
* */
75+
private byte[] getClassData(String className){
76+
// 读取类文件的字节
77+
String path = classNameToPath(className);
78+
79+
try{
80+
InputStream ins = new FileInputStream(path);
81+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
82+
byte[] buffer = new byte[1024];
83+
84+
int len = 0;
85+
86+
// 读取类文件的字节码
87+
while ((len = ins.read(buffer)) != -1){
88+
baos.write(buffer, 0, len);
89+
}
90+
91+
return baos.toByteArray();
92+
93+
} catch (IOException e){
94+
e.printStackTrace();
95+
}
96+
97+
return null;
98+
}
99+
100+
/**
101+
* 类文件的完全路径
102+
* */
103+
private String classNameToPath(String className){
104+
return rootDir + "\\" + className.replace('.', '\\') + ".class";
105+
}
106+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ java编程基础 面向对象 javaAPI 集合 IO GUI JDBC 多线程 网络编程
778778
-----------------
779779

780780
- [x] [UserTest__显隐加载](JVM/src/com/cpucode/java/loader/UserTest.java)
781+
- [x] [UserClassLoader__类加载](JVM/src/com/cpucode/java/loader/UserClassLoader.java)
781782

782783
- [返回目录](#文件目录)
783784

0 commit comments

Comments
 (0)