前言
今天我们来自定义一个类加载器
第一步
继承ClassLoader类
public class MyClassLoader extends ClassLoader
第二步
重写findClass方法
public Class<?> findClass(String name) throws ClassNotFoundException{ try { byte[] data = loadByte(name); return defineClass(name, data ,0,data.length); }catch (Exception e){ e.printStackTrace(); throw new ClassNotFoundException(); } }
其中defineClass类是核心,需要传入类名以及类的路径data,所以需要对类进行一个类路径的获取
private byte[] loadByte(String name) throws Exception{ name = name.replaceAll("\\.","/"); FileInputStream fis = new FileInputStream("/" + name + ".class"); int len = fis.available(); byte[] data = new byte[len]; fis.read(data); fis.close(); return data; }
最后写一个main方法
public static void main(String[] args) throws Exception{ MyClassLoaderTest.MyClassLoader classLoader = new MyClassLoaderTest.MyClassLoader("D:\test"); Class clazz = classLoader.loadClass("User"); Object obj = clazz.newInstance(); Method method = clazz.getDeclaredMethod("sout",null); method.invoke(obj,null); System.out.println(clazz.getClassLoader().getClass().getName()); }
其中我们将User类的class文件放到D:\test文件夹下,其中User类中有一个sout方法,方法中输出一句话证明我们调用成功了,总代码如下
import java.io.FileInputStream; import java.lang.reflect.Method; public class MyClassLoader extends ClassLoader{ private byte[] loadByte(String name) throws Exception{ name = name.replaceAll("\\.","/"); FileInputStream fis = new FileInputStream("/" + name + ".class"); int len = fis.available(); byte[] data = new byte[len]; fis.read(data); fis.close(); return data; } public Class<?> findClass(String name) throws ClassNotFoundException{ try { byte[] data = loadByte(name); return defineClass(name, data ,0,data.length); }catch (Exception e){ e.printStackTrace(); throw new ClassNotFoundException(); } } public static void main(String[] args) throws Exception{ MyClassLoaderTest.MyClassLoader classLoader = new MyClassLoaderTest.MyClassLoader("D:\test"); Class clazz = classLoader.loadClass("User"); Object obj = clazz.newInstance(); Method method = clazz.getDeclaredMethod("sout",null); method.invoke(obj,null); System.out.println(clazz.getClassLoader().getClass().getName()); } }
运行结果如下
但是每一次都会使用自定义的类加载器的父类去加载文件,所以我们要打破双亲委派机制,我们找到jdk自带的类ClassLoader下的loadClass方法,将他复制到我们的项目中
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { synchronized (getClassLoadingLock(name)) { // First, check if the class has already been loaded Class<?> c = findLoadedClass(name); if (c == null) { long t0 = System.nanoTime(); try { if (parent != null) { c = parent.loadClass(name, false); } else { c = findBootstrapClassOrNull(name); } } catch (ClassNotFoundException e) { // ClassNotFoundException thrown if class not found // from the non-null parent class loader } if (c == null) { // If still not found, then invoke findClass in order // to find the class. long t1 = System.nanoTime(); c = findClass(name); // this is the defining class loader; record the stats sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0); sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1); sun.misc.PerfCounter.getFindClasses().increment(); } } if (resolve) { resolveClass(c); } return c; } }
其中这几句代码的作用就是实现了双亲委派机制,很明显他的意思是如果父加载器不为空就调用父类加载器,如果父类加载器为空就使用引导类BootstrapClass加载器加载,我们将这端代码删除就可以打破双亲委派机制了。