Methods Summary |
---|
public static java.net.URL | getResource(java.lang.String resourceName, java.lang.Class callingClass)Load a given resource.
This method will try to load the resource using the following methods (in order):
- From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()}
- From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()}
- From the {@link Class#getClassLoader() callingClass.getClassLoader() }
URL url = null;
url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
if (url == null) {
url = ClassLoaderUtils.class.getClassLoader().getResource(resourceName);
}
if (url == null) {
url = callingClass.getClassLoader().getResource(resourceName);
}
return url;
|
public static java.io.InputStream | getResourceAsStream(java.lang.String resourceName, java.lang.Class callingClass)This is a convenience method to load a resource as a stream.
The algorithm used to find the resource is given in getResource()
URL url = getResource(resourceName, callingClass);
try {
return (url != null) ? url.openStream() : null;
} catch (IOException e) {
return null;
}
|
public static java.lang.Class | loadClass(java.lang.String className, java.lang.Class callingClass)Load a class with a given name.
It will try to load the class in the following order:
- From {@link Thread#getContextClassLoader() Thread.currentThread().getContextClassLoader()}
- Using the basic {@link Class#forName(java.lang.String) }
- From {@link Class#getClassLoader() ClassLoaderUtil.class.getClassLoader()}
- From the {@link Class#getClassLoader() callingClass.getClassLoader() }
try {
return Thread.currentThread().getContextClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
try {
return Class.forName(className);
} catch (ClassNotFoundException ex) {
try {
return ClassLoaderUtils.class.getClassLoader().loadClass(className);
} catch (ClassNotFoundException exc) {
return callingClass.getClassLoader().loadClass(className);
}
}
}
|
public static void | printClassLoader()Prints the current classloader hierarchy - useful for debugging.
System.out.println("ClassLoaderUtils.printClassLoader");
printClassLoader(Thread.currentThread().getContextClassLoader());
|
public static void | printClassLoader(java.lang.ClassLoader cl)Prints the classloader hierarchy from a given classloader - useful for debugging.
System.out.println("ClassLoaderUtils.printClassLoader(cl = " + cl + ")");
if (cl != null) {
printClassLoader(cl.getParent());
}
|