public final class ClassLoaderUtils extends Object
Utility class for obtaining a correct classloader on which to operate from a specific class.
Modifier and Type | Method and Description |
---|---|
static Class<?> |
findClass(String className)
Try to obtain a class by name, returning null if not found.
|
static URL |
findResource(String resourceName)
Try to obtain a resource by name, returning null if it could not be located.
|
static InputStream |
findResourceAsStream(String resourceName)
Try to obtain a resource by name, returning null if it could not be located.
|
static ClassLoader |
getClassLoader(Class<?> clazz)
Try to obtain a classloader, following these priorities:
|
static boolean |
isClassPresent(String className)
Checks whether a class is present at the application's class path.
|
static boolean |
isResourcePresent(String resourceName)
Checks whether a resource is present at the application's class path.
|
static Class<?> |
loadClass(String className)
Obtain a class by name, throwing an exception if it is not present.
|
static InputStream |
loadResourceAsStream(String resourceName)
Obtain a resource by name, throwing an exception if it is not present.
|
public static ClassLoader getClassLoader(Class<?> clazz)
Try to obtain a classloader, following these priorities:
clazz
- the class which loader will be obtained in the second step. Can be null (that will
skip that second step).public static Class<?> loadClass(String className) throws ClassNotFoundException
Obtain 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.
className
- the name of the class to be obtained.ClassNotFoundException
- if the class could not be loaded.public static Class<?> findClass(String className)
Try to obtain a class by name, returning null if not found.
This method works very similarly to loadClass(String)
but will just return null
if the class is not found by the sequence of class loaders being tried.
className
- the name of the class to be obtained.public static boolean isClassPresent(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 return true
or false depending on whether the class could be found or not.
className
- the name of the class to be checked.public static URL findResource(String resourceName)
Try to obtain a resource by name, returning null if 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.
resourceName
- the name of the resource to be obtained.public static boolean isResourcePresent(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 return true
or false depending on whether the resource could be located or not.
resourceName
- the name of the resource to be checked.public static InputStream loadResourceAsStream(String resourceName) throws IOException
Obtain 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.
resourceName
- the name of the resource to be obtained.IOException
- if the resource could not be located.public static InputStream findResourceAsStream(String resourceName)
Try to obtain a resource by name, returning null if it could not be located.
This method works very similarly to loadResourceAsStream(String)
but will just return null
if the resource cannot be located by the sequence of class loaders being tried.
resourceName
- the name of the resource to be obtained.Copyright © 2017 The THYMELEAF team. All rights reserved.