Class ClassLoaderUtils
- java.lang.Object
-
- org.thymeleaf.util.ClassLoaderUtils
-
public final class ClassLoaderUtils extends java.lang.ObjectUtility class for obtaining a correct classloader on which to operate from a specific class.
- Since:
- 2.0.6
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.ClassLoaderclassClassLoaderprivate static java.lang.ClassLoadersystemClassLoaderprivate static booleansystemClassLoaderAccessibleFromClassClassLoader
-
Constructor Summary
Constructors Modifier Constructor Description privateClassLoaderUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.Class<?>findClass(java.lang.String className)Try to obtain a class by name, returningnullif not found.static java.net.URLfindResource(java.lang.String resourceName)Try to obtain a resource by name, returningnullif it could not be located.static java.io.InputStreamfindResourceAsStream(java.lang.String resourceName)Try to obtain a resource by name, returningnullif it could not be located.private static java.lang.ClassLoadergetClassClassLoader(java.lang.Class<?> clazz)static java.lang.ClassLoadergetClassLoader(java.lang.Class<?> clazz)Try to obtain a classloader, following these priorities:private static java.lang.ClassLoadergetSystemClassLoader()private static java.lang.ClassLoadergetThreadContextClassLoader()static booleanisClassPresent(java.lang.String className)Checks whether a class is present at the application's class path.private static booleanisKnownClassLoaderAccessibleFrom(java.lang.ClassLoader accessibleCL, java.lang.ClassLoader fromCL)private static booleanisKnownLeafClassLoader(java.lang.ClassLoader classLoader)static booleanisResourcePresent(java.lang.String resourceName)Checks whether a resource is present at the application's class path.static java.lang.Class<?>loadClass(java.lang.String className)Obtain a class by name, throwing an exception if it is not present.static java.io.InputStreamloadResourceAsStream(java.lang.String resourceName)Obtain a resource by name, throwing an exception if it is not present.
-
-
-
Method Detail
-
getClassLoader
public static java.lang.ClassLoader getClassLoader(java.lang.Class<?> clazz)
Try to obtain a classloader, following these priorities:
- If there is a thread context class loader, return it.
- Else if there is a class loader related to the class passed as argument, return it.
- Else return the system class loader.
- Parameters:
clazz- the class which loader will be obtained in the second step. Can be null (that will skip that second step).- Returns:
- a non-null, safe classloader to use.
-
loadClass
public static java.lang.Class<?> loadClass(java.lang.String className) throws java.lang.ClassNotFoundExceptionObtain a class by name, throwing an exception if it is not present.
First the context class loader will be used. If this class loader is not able to load the class, then the class class loader (
ClassLoaderUtils.class.getClassLoader()) will be used if it is different from the thread context one. Last, the System class loader will be tried.This method does never return
null.- Parameters:
className- the name of the class to be obtained.- Returns:
- the loaded class (null never returned).
- Throws:
java.lang.ClassNotFoundException- if the class could not be loaded.- Since:
- 3.0.3
-
findClass
public static java.lang.Class<?> findClass(java.lang.String className)
Try to obtain a class by name, returning
nullif not found.This method works very similarly to
loadClass(String)but will just returnnullif the class is not found by the sequence of class loaders being tried.- Parameters:
className- the name of the class to be obtained.- Returns:
- the found class, or
nullif it could not be found. - Since:
- 3.0.3
-
isClassPresent
public static boolean isClassPresent(java.lang.String className)
Checks whether a class is present at the application's class path.
This method works very similarly to
findClass(String)but will just returntrueorfalsedepending on whether the class could be found or not.- Parameters:
className- the name of the class to be checked.- Returns:
trueif the class was found (by any class loader),falseif not.- Since:
- 3.0.3
-
findResource
public static java.net.URL findResource(java.lang.String resourceName)
Try to obtain a resource by name, returning
nullif it could not be located.First the context class loader will be used. If this class loader is not able to locate the resource, then the class class loader (
ClassLoaderUtils.class.getClassLoader()) will be used if it is different from the thread context one. Last, the System class loader will be tried.- Parameters:
resourceName- the name of the resource to be obtained.- Returns:
- the found resource, or
nullif it could not be located. - Since:
- 3.0.3
-
isResourcePresent
public static boolean isResourcePresent(java.lang.String resourceName)
Checks whether a resource is present at the application's class path.
This method works very similarly to
findResource(String)but will just returntrueorfalsedepending on whether the resource could be located or not.- Parameters:
resourceName- the name of the resource to be checked.- Returns:
trueif the class was located (by any class loader),falseif not.- Since:
- 3.0.3
-
loadResourceAsStream
public static java.io.InputStream loadResourceAsStream(java.lang.String resourceName) throws java.io.IOExceptionObtain a resource by name, throwing an exception if it is not present.
First the context class loader will be used. If this class loader is not able to locate the resource, then the class class loader (
ClassLoaderUtils.class.getClassLoader()) will be used if it is different from the thread context one. Last, the System class loader will be tried.This method does never return
null.- Parameters:
resourceName- the name of the resource to be obtained.- Returns:
- an input stream on the resource (null never returned).
- Throws:
java.io.IOException- if the resource could not be located.- Since:
- 3.0.3
-
findResourceAsStream
public static java.io.InputStream findResourceAsStream(java.lang.String resourceName)
Try to obtain a resource by name, returning
nullif it could not be located.This method works very similarly to
loadResourceAsStream(String)but will just returnnullif the resource cannot be located by the sequence of class loaders being tried.- Parameters:
resourceName- the name of the resource to be obtained.- Returns:
- an input stream on the resource, or
nullif it could not be located. - Since:
- 3.0.3
-
getThreadContextClassLoader
private static java.lang.ClassLoader getThreadContextClassLoader()
-
getClassClassLoader
private static java.lang.ClassLoader getClassClassLoader(java.lang.Class<?> clazz)
-
getSystemClassLoader
private static java.lang.ClassLoader getSystemClassLoader()
-
isKnownClassLoaderAccessibleFrom
private static boolean isKnownClassLoaderAccessibleFrom(java.lang.ClassLoader accessibleCL, java.lang.ClassLoader fromCL)
-
isKnownLeafClassLoader
private static boolean isKnownLeafClassLoader(java.lang.ClassLoader classLoader)
-
-