Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/main/java/club/shengsheng/MyClassLoader.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
package club.shengsheng;


import java.io.File;
import java.nio.file.Files;

/**
* @author gongxuanzhangmelt@gmail.com
**/
public class MyClassLoader extends ClassLoader {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
try {
// 获取项目根目录路径
String projectRoot = System.getProperty("user.dir");
File encryptedFile = new File(projectRoot + File.separator + "加密.class");
byte[] bytes = Files.readAllBytes(encryptedFile.toPath());

for (int i = 0; i < bytes.length; i++) {
bytes[i] = (byte) (bytes[i] - 1);
}
return defineClass(name, bytes, 0, bytes.length);
} catch (Exception e) {
throw new ClassNotFoundException(name, e);
}
}


@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
// copy原ClassLoader中的实现,将双亲委派逻辑去掉,并判断tech包下的类由自定义的类加载器加载
synchronized (getClassLoadingLock(name)) {
Class<?> c = findLoadedClass(name);
if (c == null) {
if (name.startsWith("tech")) {
c = findClass(name);
} else {
c = getParent().loadClass(name);
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
}
Loading