Java類加載工具--ClassUtil

jopen 10年前發布 | 27K 次閱讀 ClassUtil Java開發

介紹一個類加載工具,ClassUtil 需要加載類或資源的話可以使用該工具類。

    import java.io.InputStream;

/** 
 * A utility class to assist with loading classes or resources by name. Many application servers use 
 * custom classloaders, which will break uses of: 
 * <pre> 
 *    Class.forName(className); 
 * </pre> 
 * 
 * This utility attempts to load the class or resource using a number of different mechanisms to 
 * work around this problem. 
 * 
 * @author Matt Tucker 
 */  
public class ClassUtils {  


    private static ClassUtils instance = new ClassUtils();  


    /** 
     * Loads the class with the specified name. 
     * 
     * @param className the name of the class 
     * @return the resulting <code>Class</code> object 
     * @throws ClassNotFoundException if the class was not found 
     */  
    public static Class forName(String className) throws ClassNotFoundException {  
        return instance.loadClass(className);  
    }  


    /** 
     * Loads the given resource as a stream. 
     * 
     * @param name the name of the resource that exists in the classpath. 
     * @return the resource as an input stream or <tt>null</tt> if the resource was not found. 
     */  
    public static InputStream getResourceAsStream(String name) {  
        return instance.loadResource(name);  
    }  


    /** 
     * Not instantiatable. 
     */  
    private ClassUtils() {}  


    public Class loadClass(String className) throws ClassNotFoundException {  
        Class theClass = null;  
        try {  
            theClass = Class.forName(className);  
        }  
        catch (ClassNotFoundException e1) {  
            try {  
                theClass = Thread.currentThread().getContextClassLoader().loadClass(className);  
            }  
            catch (ClassNotFoundException e2) {  
                theClass = getClass().getClassLoader().loadClass(className);  
            }  
        }  
        return theClass;  
    }  


    private InputStream loadResource(String name) {  
        InputStream in = getClass().getResourceAsStream(name);  
        if (in == null) {  
            in = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);  
            if (in == null) {  
                in = getClass().getClassLoader().getResourceAsStream(name);  
            }  
        }  
        return in;  
    }  
}  </pre>
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!