Java ClassLoader加载机制理解 实际例子

时间:2021-08-09 19:40:09

针对 Java ClassLoader加载机制理解, 做了个如何自定制简单的ClassLoader,并成功加载指定的类。

不废话,直接上代码。

package com.chq.study.cl;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

/**
 * @desc 自定义ClassLoader,只能加载.class结尾的,用来测试java的classLoader机制
 */
public class ChqClassLoader extends ClassLoader {
    private String fileName;

    public ChqClassLoader(String fileName) {
        this.fileName = fileName;
    }

    protected Class<?> findClass(String className) throws ClassNotFoundException {
        Class<?> clazz = this.findLoadedClass(className);
        if (null == clazz) {
            try {
                String classFile = getClassFile(className);
                System.out.println("findClass " + classFile);
                FileInputStream fis = new FileInputStream(classFile);
                FileChannel fileC = fis.getChannel();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                WritableByteChannel outC = Channels.newChannel(baos);
                ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
                while (true) {
                    int i = fileC.read(buffer);
                    if (i == 0 || i == -1) {
                        break;
                    }
                    buffer.flip();
                    outC.write(buffer);
                    buffer.clear();
                }
                fis.close();
                byte[] bytes = baos.toByteArray();

                clazz = defineClass(className, bytes, 0, bytes.length);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return clazz;
    }

    private String getClassFile(String name) {
        StringBuffer sb = new StringBuffer(fileName);
        name = name.replace('.', File.separatorChar) + ".class";
        sb.append(File.separator + name);
        return sb.toString();
    }
}
package com.chq.study.cl;

/**
 * @desc 自我介绍测试接口类,塑型用的
 */
public interface ITest {
    public void self();
}
package com.chq.study.cl;

/**
 * @desc 未从接口类继承
 */
public class Test {
    public void self() {
        System.out.println("this is from Test instance " + this);
    }
}
package com.chq.study.cl;

/**
 * @desc 自我介绍测试实现类
 */
public class TestImpl implements ITest {

    /* (non-Javadoc)
     * @see com.chq.study.cl.ITest#self()
     */
    @Override
    public void self() {
        System.out.println("this is from TestImpl instance " + this);
    }

} 
package com.chq.study.cl;

/**
 * @author chenqing
 * @datetime 2015年2月4日 下午4:54:12
 * @desc 入口类, 调用自定义的ClassLoader,来加载类进行验证
 */
public class MainClassLoader {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ChqClassLoader cl = new ChqClassLoader("C:\\workspaces\\MyEclipse Professional 2014\\classloader\\bin");
        try {
            Class<?> clazz = cl.findClass("com.chq.study.cl.Test");
            try {
                // 此处执行会抛出异常,验证了classLoader的全盘负责机制
                Test cc = (Test) clazz.newInstance();
                cc.self();
                System.out.println("belong class loader: " + cc.getClass().getClassLoader().toString());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassCastException e) {
                e.printStackTrace();
            }
            
            // 确保输出顺序
            try {
                Thread.sleep(100); 
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            
            clazz = cl.findClass("com.chq.study.cl.TestImpl");
            try {
                // 此处正常,通过塑性为基类来绕开全盘负责机制
                ITest ic = (ITest)clazz.newInstance();
                ic.self();
                System.out.println("belong class loader: " + ic.getClass().getClassLoader().toString());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassCastException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}

 

最终输出的结果表明, Test & TestImpl 都找到了,但在实例化时,因为不同的classLoader加载的,导致前者失败,后者则通过塑型为基类而成功加载并实例化。

findClass C:\workspaces\MyEclipse Professional 2014\classloader\bin\com\chq\study\cl\Test.class
java.lang.ClassCastException: com.chq.study.cl.Test cannot be cast to com.chq.study.cl.Test
    at com.chq.study.cl.MainClassLoader.main(MainClassLoader.java:22)
findClass C:\workspaces\MyEclipse Professional 2014\classloader\bin\com\chq\study\cl\TestImpl.class
this is from TestImpl instance com.chq.study.cl.TestImpl@173a10f
belong class loader: com.chq.study.cl.ChqClassLoader@14318bb