package com.sleep;
import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
/**
* @author xuguilin
* @version 1.0
* @date 2021/12/1 17:03
*/
public class MyClassLoader extends ClassLoader {
public MyClassLoader(ClassLoader parent) {
super(parent);
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
ClassLoader systemClassLoader = getSystemClassLoader().getParent();
//System.out.println(systemClassLoader);
Class<?> aClass = null;
try {
aClass = systemClassLoader.loadClass(name);
} catch (Exception e) {
}
if (aClass == null) {
aClass = findClass(name);
}
return aClass;
}
private byte[] getClassBytes(File file) throws Exception {
// 这里要读入.class的字节,因此要使用字节流
FileInputStream fis = new FileInputStream(file);
FileChannel fc = fis.getChannel();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
WritableByteChannel wbc = Channels.newChannel(baos);
ByteBuffer by = ByteBuffer.allocate(1024);
while (true) {
int i = fc.read(by);
if (i == 0 || i == -1)
break;
by.flip();
wbc.write(by);
by.clear();
}
fis.close();
return baos.toByteArray();
}
private File getClassFile(String name) {
File file = new File("D:/Per.class");
return file;
}
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
File file = getClassFile(name);
try {
byte[] bytes = getClassBytes(file);
Class<?> c = this.defineClass(name, bytes, 0, bytes.length);
return c;
} catch (Exception e) {
e.printStackTrace();
}
throw new ClassNotFoundException(name);
}
}