Package org.apache.logging.log4j.util
Class LoaderUtil
- java.lang.Object
-
- org.apache.logging.log4j.util.LoaderUtil
-
public final class LoaderUtil extends java.lang.Object
Consider this class private. Utility class for ClassLoaders.- See Also:
ClassLoader
,RuntimePermission
,Thread.getContextClassLoader()
,ClassLoader.getSystemClassLoader()
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
LoaderUtil.ThreadContextClassLoaderGetter
(package private) static class
LoaderUtil.UrlResource
URL
andClassLoader
pair.
-
Field Summary
Fields Modifier and Type Field 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
-
Constructor Summary
Constructors Modifier Constructor Description private
LoaderUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static void
accumulateClassLoaders(java.lang.ClassLoader loader, java.util.Collection<java.lang.ClassLoader> loaders)
Adds the provided loader to the loaders collection, and traverses up the tree until either a null value or a classloader which has already been added is encountered.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[]
getClassLoaders()
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.
-
-
-
Field Detail
-
IGNORE_TCCL_PROPERTY
public static final java.lang.String IGNORE_TCCL_PROPERTY
System property to set to ignore the thread context ClassLoader.- Since:
- 2.1
- See Also:
- Constant Field Values
-
SECURITY_MANAGER
private static final java.lang.SecurityManager SECURITY_MANAGER
-
ignoreTCCL
private static java.lang.Boolean ignoreTCCL
-
GET_CLASS_LOADER_DISABLED
private static final boolean GET_CLASS_LOADER_DISABLED
-
TCCL_GETTER
private static final java.security.PrivilegedAction<java.lang.ClassLoader> TCCL_GETTER
-
-
Method Detail
-
getThreadContextClassLoader
public static java.lang.ClassLoader getThreadContextClassLoader()
Gets the current Thread ClassLoader. Returns the system ClassLoader if the TCCL isnull
. If the system ClassLoader isnull
as well, then the ClassLoader for this class is returned. If running with aSecurityManager
that does not allow access to the Thread ClassLoader or system ClassLoader, then the ClassLoader for this class is returned.- Returns:
- the current ThreadContextClassLoader.
-
getClassLoaders
public static java.lang.ClassLoader[] getClassLoaders()
-
accumulateClassLoaders
private static void accumulateClassLoaders(java.lang.ClassLoader loader, java.util.Collection<java.lang.ClassLoader> loaders)
Adds the provided loader to the loaders collection, and traverses up the tree until either a null value or a classloader which has already been added is encountered.
-
isClassAvailable
public static boolean isClassAvailable(java.lang.String className)
Determines if a named Class can be loaded or not.- Parameters:
className
- The class name.- Returns:
true
if the class could be found orfalse
otherwise.- Since:
- 2.7
-
loadClass
public static java.lang.Class<?> loadClass(java.lang.String className) throws java.lang.ClassNotFoundException
Loads a class by name. This method respects theIGNORE_TCCL_PROPERTY
Log4j property. If this property is specified and set to anything besidesfalse
, then the default ClassLoader will be used.- Parameters:
className
- The class name.- Returns:
- the Class for the given name.
- Throws:
java.lang.ClassNotFoundException
- if the specified class name could not be found- Since:
- 2.1
-
newInstanceOf
public static <T> T newInstanceOf(java.lang.Class<T> clazz) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Loads and instantiates a Class using the default constructor.- Parameters:
clazz
- The class.- Returns:
- new instance of the class.
- Throws:
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 class- Since:
- 2.7
-
newInstanceOf
public 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
Loads and instantiates a Class using the default constructor.- Parameters:
className
- The class name.- Returns:
- new instance of the class.
- Throws:
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 class- Since:
- 2.1
-
newCheckedInstanceOf
public 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
Loads and instantiates a derived class using its default constructor.- Type Parameters:
T
- The type of the class to check.- Parameters:
className
- The class name.clazz
- The class to cast it to.- Returns:
- new instance of the class cast to
T
- Throws:
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 withT
- Since:
- 2.1
-
newCheckedInstanceOfProperty
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
Loads and instantiates a class given by a property name.- Type Parameters:
T
- The type to cast it to.- Parameters:
propertyName
- The property name to look up a class name for.clazz
- The class to cast it to.- Returns:
- new instance of the class given in the property or
null
if the property was unset. - Throws:
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 withT
- Since:
- 2.5
-
isIgnoreTccl
private static boolean isIgnoreTccl()
-
findResources
public static java.util.Collection<java.net.URL> findResources(java.lang.String resource)
Finds classpath resources.- Parameters:
resource
- the name of the resource to find.- Returns:
- a Collection of URLs matching the resource name. If no resources could be found, then this will be empty.
- Since:
- 2.1
-
findUrlResources
static java.util.Collection<LoaderUtil.UrlResource> findUrlResources(java.lang.String resource)
-
-