public final class LoaderUtil
extends java.lang.Object
ClassLoader
,
RuntimePermission
,
Thread.getContextClassLoader()
,
ClassLoader.getSystemClassLoader()
Modifier and Type | Class and Description |
---|---|
private static class |
LoaderUtil.ThreadContextClassLoaderGetter |
(package private) static class |
LoaderUtil.UrlResource
URL and ClassLoader pair. |
Modifier and Type | Field and Description |
---|---|
private static boolean |
GET_CLASS_LOADER_DISABLED |
static java.lang.String |
IGNORE_TCCL_PROPERTY
System property to set to ignore the thread context ClassLoader.
|
private static java.lang.Boolean |
ignoreTCCL |
private static java.lang.SecurityManager |
SECURITY_MANAGER |
private static java.security.PrivilegedAction<java.lang.ClassLoader> |
TCCL_GETTER |
Modifier | Constructor and Description |
---|---|
private |
LoaderUtil() |
Modifier and Type | Method and Description |
---|---|
static java.util.Collection<java.net.URL> |
findResources(java.lang.String resource)
Finds classpath resources.
|
(package private) static java.util.Collection<LoaderUtil.UrlResource> |
findUrlResources(java.lang.String resource) |
static java.lang.ClassLoader |
getThreadContextClassLoader()
Gets the current Thread ClassLoader.
|
static boolean |
isClassAvailable(java.lang.String className)
Determines if a named Class can be loaded or not.
|
private static boolean |
isIgnoreTccl() |
static java.lang.Class<?> |
loadClass(java.lang.String className)
Loads a class by name.
|
static <T> T |
newCheckedInstanceOf(java.lang.String className,
java.lang.Class<T> clazz)
Loads and instantiates a derived class using its default constructor.
|
static <T> T |
newCheckedInstanceOfProperty(java.lang.String propertyName,
java.lang.Class<T> clazz)
Loads and instantiates a class given by a property name.
|
static <T> T |
newInstanceOf(java.lang.Class<T> clazz)
Loads and instantiates a Class using the default constructor.
|
static <T> T |
newInstanceOf(java.lang.String className)
Loads and instantiates a Class using the default constructor.
|
public static final java.lang.String IGNORE_TCCL_PROPERTY
private static final java.lang.SecurityManager SECURITY_MANAGER
private static java.lang.Boolean ignoreTCCL
private static final boolean GET_CLASS_LOADER_DISABLED
private static final java.security.PrivilegedAction<java.lang.ClassLoader> TCCL_GETTER
public static java.lang.ClassLoader getThreadContextClassLoader()
null
. If the system
ClassLoader is null
as well, then the ClassLoader for this class is returned. If running with a
SecurityManager
that does not allow access to the Thread ClassLoader or system ClassLoader, then the
ClassLoader for this class is returned.public static boolean isClassAvailable(java.lang.String className)
className
- The class name.true
if the class could be found or false
otherwise.public static java.lang.Class<?> loadClass(java.lang.String className) throws java.lang.ClassNotFoundException
IGNORE_TCCL_PROPERTY
Log4j property. If this property is
specified and set to anything besides false
, then the default ClassLoader will be used.className
- The class name.java.lang.ClassNotFoundException
- if the specified class name could not be foundpublic static <T> T newInstanceOf(java.lang.Class<T> clazz) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
clazz
- The class.java.lang.IllegalAccessException
- if the class can't be instantiated through a public constructorjava.lang.InstantiationException
- if there was an exception whilst instantiating the classjava.lang.reflect.InvocationTargetException
- if there was an exception whilst constructing the classpublic static <T> T newInstanceOf(java.lang.String className) throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException
className
- The class name.java.lang.ClassNotFoundException
- if the class isn't available to the usual ClassLoadersjava.lang.IllegalAccessException
- if the class can't be instantiated through a public constructorjava.lang.InstantiationException
- if there was an exception whilst instantiating the classjava.lang.NoSuchMethodException
- if there isn't a no-args constructor on the classjava.lang.reflect.InvocationTargetException
- if there was an exception whilst constructing the classpublic static <T> T newCheckedInstanceOf(java.lang.String className, java.lang.Class<T> clazz) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.InstantiationException, java.lang.IllegalAccessException
T
- The type of the class to check.className
- The class name.clazz
- The class to cast it to.T
java.lang.ClassNotFoundException
- if the class isn't available to the usual ClassLoadersjava.lang.IllegalAccessException
- if the class can't be instantiated through a public constructorjava.lang.InstantiationException
- if there was an exception whilst instantiating the classjava.lang.NoSuchMethodException
- if there isn't a no-args constructor on the classjava.lang.reflect.InvocationTargetException
- if there was an exception whilst constructing the classjava.lang.ClassCastException
- if the constructed object isn't type compatible with T
public static <T> T newCheckedInstanceOfProperty(java.lang.String propertyName, java.lang.Class<T> clazz) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.InstantiationException, java.lang.IllegalAccessException
T
- The type to cast it to.propertyName
- The property name to look up a class name for.clazz
- The class to cast it to.null
if the property was unset.java.lang.ClassNotFoundException
- if the class isn't available to the usual ClassLoadersjava.lang.IllegalAccessException
- if the class can't be instantiated through a public constructorjava.lang.InstantiationException
- if there was an exception whilst instantiating the classjava.lang.NoSuchMethodException
- if there isn't a no-args constructor on the classjava.lang.reflect.InvocationTargetException
- if there was an exception whilst constructing the classjava.lang.ClassCastException
- if the constructed object isn't type compatible with T
private static boolean isIgnoreTccl()
public static java.util.Collection<java.net.URL> findResources(java.lang.String resource)
resource
- the name of the resource to find.static java.util.Collection<LoaderUtil.UrlResource> findUrlResources(java.lang.String resource)