jvm介绍 day4

2020-01-16  本文已影响0人  小明同学呀呀呀

获取ClassLoader的途径

  1. 获取当前类的ClassLoader
    clazz.getCLassLoader();
  2. 获取当前线程上下文的ClassLoader
    Thread.currentThread().getContextClassLoader()
  3. 获得系统的ClassLoader
    ClassLoader.getSystemClassLoader()
  4. 获得调用者的ClassLoader
    DirverManager.getCallerClassLoader()
/**
 * @author NingXioaoming
 * @createTime 2019/12/16 13:56
 * @description
 */
public class MyTest14 {
    public static void main(String[] args) {
         ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();

        System.out.println(contextClassLoader);


        final Class<MyTest14> clazz = MyTest14.class;
        System.out.println(clazz.getClassLoader());//由系统类加载sun.misc.Launcher$AppClassLoader@18b4aac2
        final Class clazz1 = String.class;
        System.out.println(clazz1.getClassLoader()); //由根类或者启动类加载的,null    因为String位于rt.jar包中
    }
}

数组对是在程序运行期间由jvm创建的而不是由类加载器加载的,调用数组的getClassLoader得到的结果是数组中对象所返回的结果,如果数组中的元素是原生类型(int,double)的话调用getClassLoader是没有类加载器的

自定义类加载器([loader1])和系统类加载器(sun.misc.Launcher$AppClassLoader@18b4aac2)的区别

package com.mobius.vision.jvm.classloader;

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

/**
 * @author NingXioaoming
 * @createTime 2019/12/21 10:43
 * @description
 */
public class MyTest16 extends ClassLoader {

    private String classLoaderName;
    private final String fileExtension = ".class";

    private String path;

    public void setPath(String path) {
        this.path = path;
    }

    public MyTest16(String classLoaderName){
        super(); //将系统加载器当做该类加载器的父加载器
        this.classLoaderName = classLoaderName;
    }

    public MyTest16(ClassLoader parent,String classLoaderName){
        super(parent);//显示指定该类加载器的父加载器
        this.classLoaderName = classLoaderName;
//        System.out.println("nihap111");
    }

    @Override
    public String toString() {
        return "["+this.classLoaderName+"]";
    }

    @Override
    protected Class<?> findClass(String className) throws ClassNotFoundException {
        byte[] data = this.loadClassData(className);
        System.out.println("findClass invoked:" + className);
        System.out.println("class loader name:"+ this.classLoaderName);
        return this.defineClass(className,data,0,data.length);
    }

    private byte[] loadClassData(String className){
        InputStream is = null;
        byte[] data = null;
        ByteArrayOutputStream baos = null;

        className = className.replace(".","\\");

        try {
            is = new FileInputStream(new File(this.path+className+this.fileExtension));
            baos = new ByteArrayOutputStream();
            int ch;
            while (-1 !=(ch = is.read())){
                baos.write(ch);
            }
            data = baos.toByteArray();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                is.close();
                baos.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }



        return data;
    }

/*    public static void test(ClassLoader classLoader) throws Exception{

        final Class<?> clazz = classLoader.loadClass("com.mobius.vision.jvm.classloader.MyTest1");
        final Object object = clazz.newInstance();
        System.out.println(object);
//        System.out.println(object.getClass().getClassLoader());
//        System.out.println(clazz.getClassLoader());
    }*/

    public static void main(String[] args) throws Exception {
        final MyTest16 loader1 = new MyTest16("loader1");
        loader1.setPath("C:\\Users\\user\\Desktop\\");
        final Class<?> clazz = loader1.loadClass("com.mobius.vision.jvm.classloader.MyTest1");
        System.out.println("class:"+clazz.hashCode());
        final Object object = clazz.newInstance();
        System.out.println(object.getClass().getClassLoader());

//        test(loader1);
        System.out.println("************************");
        final MyTest16 loader2 = new MyTest16("loader2");
        loader2.setPath("C:\\Users\\user\\Desktop\\");
        final Class<?> clazz2 = loader2.loadClass("com.mobius.vision.jvm.classloader.MyTest1");
        System.out.println("class:"+clazz2.hashCode());
        final Object object2 = clazz2.newInstance();
        System.out.println(object2.getClass().getClassLoader());

    }
}

自定义类加载器输出的结果

findClass invoked:com.mobius.vision.jvm.classloader.MyTest1
class loader name:loader1
class:325040804
[loader1]
************************
findClass invoked:com.mobius.vision.jvm.classloader.MyTest1
class loader name:loader2
class:621009875
[loader2]

Process finished with exit code 0

系统类加载器的输出结果

class:2133927002
sun.misc.Launcher$AppClassLoader@18b4aac2
************************
class:2133927002
sun.misc.Launcher$AppClassLoader@18b4aac2

Process finished with exit code 0

将代码中第二次new的时候改成MyTest16 loader2 = new MyTest16(loader1,"loader2");变一个构造方法 将loader1的类加载器作为loader2的父加载器进行加载的输出结果

findClass invoked:com.mobius.vision.jvm.classloader.MyTest1
class loader name:loader1
class:325040804
[loader1]
************************
class:325040804
[loader1]
上一篇下一篇

猜你喜欢

热点阅读