package Week_01;
import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;
/** * 自定义一个 Classloader,加载一个 Hello.xlass 文件,执行 hello 方法, * 此文件内容是一个 Hello.class 文件所有字节(x=255-x)处理后的文件。文件群里提供。 */public class HelloClassLoader extends ClassLoader { public static void main(String[] args) { try { Class<?> helloClass = new HelloClassLoader().findClass("Hello"); Method helloMethod = helloClass.getMethod("hello"); helloMethod.invoke(helloClass.newInstance()); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
@Override protected Class<?> findClass(String name) throws ClassNotFoundException {
Path path = Paths.get("/Users/zhanghongang/Study/GeekBang/JavaCourse/JAVA-Homework/Week_01/Week_01/Hello.xlass"); byte[] helloBase64 = new byte[0]; try { helloBase64 = Files.readAllBytes(path); } catch (IOException e) { e.printStackTrace(); }
for (int i = 0; i < helloBase64.length; i++) { helloBase64[i] = (byte) (255 - helloBase64[i]); }
return defineClass(name, helloBase64, 0, helloBase64.length); }}
评论