2018-11-17 ClassLoaderWrapper

ClassLoaderWrapper,Resources使用的,具体的用来加载资源的类

package org.apache.ibatis.io;

import java.io.InputStream;
import java.net.URL;

/封装了5个类加载器/
public class ClassLoaderWrapper {

ClassLoader defaultClassLoader;/*在Resources类中设置*/
ClassLoader systemClassLoader;/*在构造方法中初始化*/

ClassLoaderWrapper() {/*构造方法*/
    try {
        systemClassLoader = ClassLoader.getSystemClassLoader();/*初始化类加载器*/
    } catch (SecurityException ignored) {
        // AccessControlException on Google App Engine
    }
}
/*获取类加载器数组*/
ClassLoader[] getClassLoaders(ClassLoader classLoader) {
    return new ClassLoader[]{
            classLoader,/*参数传入*/
            defaultClassLoader,/*在Resources中设置*/
            Thread.currentThread().getContextClassLoader(),/*当前线程绑定的类加载器*/
            getClass().getClassLoader(),/*当前类使用的类加载器*/
            systemClassLoader};/*在Resources中设置*/
}

/*使用当前类路径获取资源作为URL*/
public URL getResourceAsURL(String resource) {
    return getResourceAsURL(resource, getClassLoaders(null));
}
/*使用当前类路径获取资源作为URL,指定的类加载器*/
public URL getResourceAsURL(String resource, ClassLoader classLoader) {
    return getResourceAsURL(resource, getClassLoaders(classLoader));
}

URL getResourceAsURL(String resource, ClassLoader[] classLoader) {
    URL url;
    for (ClassLoader cl : classLoader) {
        if (null != cl) {
            url = cl.getResource(resource);
            if (null == url) {
                url = cl.getResource("/" + resource);
            }
            if (null != url) {
                return url;
            }
        }
    }
    return null;
}

/*从指定路径获取资源作为stream*/
public InputStream getResourceAsStream(String resource) {
    return getResourceAsStream(resource, getClassLoaders(null));
}

/*从指定路径获取资源作为stream指定classLoader*/
public InputStream getResourceAsStream(String resource, ClassLoader classLoader) {
    return getResourceAsStream(resource, getClassLoaders(classLoader));
}
/*从指定路径获取资源作为stream,具体实现*/
InputStream getResourceAsStream(String resource, ClassLoader[] classLoader) {
    for (ClassLoader cl : classLoader) {
        if (null != cl) {
            // 根据入参resource,读取该路径下文件
            InputStream returnValue = cl.getResourceAsStream(resource);
            // 如果没有,加上“/”再尝试读取
            if (null == returnValue) {
                returnValue = cl.getResourceAsStream("/" + resource);
            }
            //如果读取到,终止循环,返回结果
            if (null != returnValue) {
                return returnValue;
            }
        }
    }
    return null;
}


/*获取name类的类对象,对外暴漏的方法*/
public Class classForName(String name) throws ClassNotFoundException {
    return classForName(name, getClassLoaders(null));
}
/*使用classLoader获取name类的类对象,对外暴漏的方法*/
public Class classForName(String name, ClassLoader classLoader) throws ClassNotFoundException {
    return classForName(name, getClassLoaders(classLoader));
}
/*使用classLoader获取name类的类对象*/
Class classForName(String name, ClassLoader[] classLoader) throws ClassNotFoundException {
    for (ClassLoader cl : classLoader) {
        if (null != cl) {
            try {
                Class c = Class.forName(name, true, cl);
                if (null != c) {
                    return c;
                }
            } catch (ClassNotFoundException e) {
                // we'll ignore this until all classloaders fail to locate the class
            }
        }
    }
    throw new ClassNotFoundException("Cannot find class: " + name);
}

}

你可能感兴趣的:(2018-11-17 ClassLoaderWrapper)