Skip to content
Closed
Show file tree
Hide file tree
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 com.sun.source.tree.SynchronizedTree;

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

/**
* @author gongxuanzhangmelt@gmail.com
**/
public class MyClassLoader extends ClassLoader {

@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
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;
}
}

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
String replace = "加密.class";
File classFile = new File(replace);
try {
byte[] bytes = Files.readAllBytes(classFile.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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MyClassLoaderTest {

@Test
void testLoadShengSheng() throws Exception {
Class<?> shengshengClass = new MyClassLoader().loadClass("tech.insight.ShengSheng");
Class<?> shengshengClass = new MyClassLoader().loadClass("tech.insight.ShengSheng", false);
Constructor<?> constructor = shengshengClass.getConstructor();
Object shengsheng = constructor.newInstance();
Method declaredMethod = shengshengClass.getDeclaredMethod("say");
Expand Down
Loading