Skip to content

Commit 5f4eaed

Browse files
authored
提交作业 (#34)
merge
1 parent 7bdf657 commit 5f4eaed

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,54 @@
11
package club.shengsheng;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
37

48
/**
59
610
**/
711
public class MyClassLoader extends ClassLoader {
812

13+
public MyClassLoader() {
14+
super();
15+
}
16+
17+
@Override
18+
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
19+
synchronized (getClassLoadingLock(name)) {
20+
// First, check if the class has already been loaded
21+
Class<?> c = findLoadedClass(name);
22+
if (c == null) {
23+
if (name.startsWith("tech.insight")) {
24+
c = findClass(name);
25+
} else {
26+
c = getParent().loadClass(name);
27+
}
28+
}
29+
if (resolve) {
30+
resolveClass(c);
31+
}
32+
return c;
33+
}
34+
}
35+
36+
@Override
37+
protected Class<?> findClass(String name) throws ClassNotFoundException {
38+
String path = name.replace(".", "/").concat(".class");
39+
String fileName = "加密" + ".class";
40+
File file = new File("./", fileName);
41+
Path filePath = file.toPath();
42+
System.out.println(filePath);
43+
try {
44+
byte[] bytes = Files.readAllBytes(filePath);
45+
for (int i = 0; i < bytes.length; i++) {
46+
bytes[i] = (byte) (bytes[i] - 1);
47+
}
48+
return defineClass(name, bytes, 0, bytes.length);
49+
} catch (IOException e) {
50+
throw new RuntimeException(e);
51+
}
52+
}
53+
954
}

0 commit comments

Comments
 (0)