|
1 | 1 | package club.shengsheng; |
2 | 2 |
|
3 | 3 |
|
4 | | -import java.io.File; |
5 | | -import java.io.IOException; |
6 | | -import java.nio.file.Files; |
7 | | - |
8 | 4 | /** |
9 | 5 | * @author gongxuanzhangmelt@gmail.com |
10 | 6 | **/ |
11 | 7 | public class MyClassLoader extends ClassLoader { |
12 | 8 |
|
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 | | - } |
57 | 9 | } |
0 commit comments