Custom ClassLoader

  • 2018-02-11
  • 105
  • 0
package com.loader;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/*
/Bootstrap Classloader -> rt.jar 
Extension Classloader extends Classloader -> %JAVA_HOME%/lib/ext/*.jar
App Classloader extends Classloader -> ClassPath
*/


public class MyLoader extends ClassLoader {
    private String path;
    private String name;

    public MyLoader(String name, String path) {
        // make the sys loader as parent loader
        super();
        this.path = path;
        this.name = name;
    }

    public MyLoader(ClassLoader parent, String name, String path) {
        // point parent loader
        super(parent);
        this.path = path;
        this.name = name;
    }

    // custom define class
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] data = readClassFileToByteArray(name);
        return this.defineClass(name, data, 0, data.length);
    }

    //get .class file byte array
    private byte[] readClassFileToByteArray(String name) {
        InputStream is = null;
        byte[] returnData = null;
        name = name.replaceAll("\\.","/");
        String filePath = this.path + name + ".class";
        File file = new File(filePath);

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            is = new FileInputStream(file);
            int tmp = 0;
            while((tmp = is.read()) != -1){
                os.write(tmp);
            }
            returnData = os.toByteArray();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                is.close();
                os.close();
            } catch (Exception e2){
                e2.printStackTrace();
            }
        }
        return returnData;
    }



    @Override
    public String toString() {
        return this.name;
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        MyLoader loader = new MyLoader("There", "D:/tmp/");
        //null: bootstrap classloader
        //not found, loadClass will try to go method findClass which was overwritten by child
        MyLoader childLoader = new MyLoader(null, "child", "D:/tmp/");
        Class<?> c = childLoader.loadClass("Demo");
        c.newInstance();
    }
    /*
    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
                    PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                    PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                    PerfCounter.getFindClasses().increment();
                }
            }
            if (resolve) {
                resolveClass(c);
            }
            return c;
        }
    }
    */
}

评论

还没有任何评论,你来说两句吧