Skip to content

Commit 120ef3e

Browse files
Revert "feat: 实现自定义类加载器 (#27)"
This reverts commit 254c512.
1 parent 254c512 commit 120ef3e

1 file changed

Lines changed: 0 additions & 48 deletions

File tree

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

33

4-
import java.io.File;
5-
import java.io.IOException;
6-
import java.nio.file.Files;
7-
84
/**
95
* @author gongxuanzhangmelt@gmail.com
106
**/
117
public class MyClassLoader extends ClassLoader {
128

13-
@Override
14-
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
15-
synchronized (getClassLoadingLock(name)) {
16-
Class<?> c = findLoadedClass(name);
17-
if (c == null) {
18-
if (name.startsWith("tech.insight")) {
19-
c = findClass(name);
20-
} else {
21-
c = this.getParent().loadClass(name);
22-
}
23-
}
24-
if (resolve) {
25-
resolveClass(c);
26-
}
27-
return c;
28-
}
29-
}
30-
31-
@Override
32-
protected Class<?> findClass(String name) throws ClassNotFoundException {
33-
boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("win");
34-
String relativePath;
35-
if (name.equals("tech.insight.ShengSheng")) {
36-
relativePath = "加密.class";
37-
} else {
38-
relativePath = name.replaceAll("\\.", isWindows ? "\\".concat(File.separator) : File.separator).concat(".class");
39-
}
40-
File classFile = new File(relativePath);
41-
byte[] bytes;
42-
try {
43-
bytes = decodeBytes(Files.readAllBytes(classFile.toPath()));
44-
} catch (IOException e) {
45-
throw new RuntimeException(e);
46-
}
47-
return defineClass(name, bytes, 0, bytes.length);
48-
}
49-
50-
private byte[] decodeBytes(byte[] bytes) {
51-
byte[] decodedBytes = new byte[bytes.length];
52-
for (int i = 0; i < bytes.length; i++) {
53-
decodedBytes[i] = (byte) (bytes[i] - 1);
54-
}
55-
return decodedBytes;
56-
}
579
}

0 commit comments

Comments
 (0)