- java.lang.Object
-
- org.apache.logging.log4j.util.LoaderUtil
-
@InternalApi public final class LoaderUtil extends Object
Consider this class private. Utility class for ClassLoaders.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
LoaderUtil.UrlResource
URL
andClassLoader
pair.
-
Field Summary
Fields Modifier and Type Field Description protected static Boolean
forceTcclOnly
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Collection<URL>
findResources(String resource)
Finds classpath resources.static Collection<URL>
findResources(String resource, boolean useTccl)
static Collection<LoaderUtil.UrlResource>
findUrlResources(String resource, boolean useTccl)
This method will only find resources that follow the JPMS rules for encapsulation.static ClassLoader
getClassLoader()
Returns the ClassLoader to use.static ClassLoader
getClassLoader(Class<?> class1, Class<?> class2)
static ClassLoader[]
getClassLoaders()
static ClassLoader
getThreadContextClassLoader()
Gets the current Thread ClassLoader.static boolean
isClassAvailable(String className)
Determines if a named Class can be loaded or not.static Class<?>
loadClass(String className)
Loads a class by name.static <T> T
newCheckedInstanceOf(String className, Class<T> clazz)
Loads and instantiates a derived class using its default constructor.static <T> T
newCheckedInstanceOfProperty(String propertyName, Class<T> clazz)
Loads and instantiates a class given by a property name.static <T> T
newInstanceOf(Class<T> clazz)
Loads and instantiates a Class using the default constructor.static <T> T
newInstanceOf(String className)
Loads and instantiates a Class using the default constructor.
-
-
-
Field Detail
-
forceTcclOnly
protected static Boolean forceTcclOnly
-
-
Method Detail
-
getClassLoader
public static ClassLoader getClassLoader()
Returns the ClassLoader to use.- Returns:
- the ClassLoader.
-
getClassLoader
public static ClassLoader getClassLoader(Class<?> class1, Class<?> class2)
-
getThreadContextClassLoader
public static 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 ClassLoader[] getClassLoaders()
-
isClassAvailable
public static boolean isClassAvailable(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 Class<?> loadClass(String className) throws ClassNotFoundException
Loads a class by name. This method respects the Log4j property. If this property is specified and set to anything besidesfalse
, then this class's ClassLoader will be used as specified byClass.forName(String)
.- Parameters:
className
- The class name.- Returns:
- the Class for the given name.
- Throws:
ClassNotFoundException
- if the specified class name could not be found- Since:
- 2.1
-
newInstanceOf
public static <T> T newInstanceOf(Class<T> clazz) throws InstantiationException, IllegalAccessException, InvocationTargetException
Loads and instantiates a Class using the default constructor.- Type Parameters:
T
- the type of the class modeled by theClass
object.- Parameters:
clazz
- The class.- Returns:
- new instance of the class.
- Throws:
IllegalAccessException
- if the class can't be instantiated through a public constructorInstantiationException
- if there was an exception whilst instantiating the classInvocationTargetException
- if there was an exception whilst constructing the class- Since:
- 2.7
-
newInstanceOf
public static <T> T newInstanceOf(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException
Loads and instantiates a Class using the default constructor.- Type Parameters:
T
- The class's type.- Parameters:
className
- The class name.- Returns:
- new instance of the class.
- Throws:
ClassNotFoundException
- if the class isn't available to the usual ClassLoadersIllegalAccessException
- if the class can't be instantiated through a public constructorInstantiationException
- if there was an exception whilst instantiating the classInvocationTargetException
- if there was an exception whilst constructing the class- Since:
- 2.1
-
newCheckedInstanceOf
public static <T> T newCheckedInstanceOf(String className, Class<T> clazz) throws ClassNotFoundException, InvocationTargetException, InstantiationException, 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:
ClassNotFoundException
- if the class isn't available to the usual ClassLoadersIllegalAccessException
- if the class can't be instantiated through a public constructorInstantiationException
- if there was an exception whilst instantiating the classInvocationTargetException
- if there was an exception whilst constructing the classClassCastException
- if the constructed object isn't type compatible withT
- Since:
- 2.1
-
newCheckedInstanceOfProperty
public static <T> T newCheckedInstanceOfProperty(String propertyName, Class<T> clazz) throws ClassNotFoundException, InvocationTargetException, InstantiationException, 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:
ClassNotFoundException
- if the class isn't available to the usual ClassLoadersIllegalAccessException
- if the class can't be instantiated through a public constructorInstantiationException
- if there was an exception whilst instantiating the classInvocationTargetException
- if there was an exception whilst constructing the classClassCastException
- if the constructed object isn't type compatible withT
- Since:
- 2.5
-
findResources
public static Collection<URL> findResources(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
-
findResources
public static Collection<URL> findResources(String resource, boolean useTccl)
-
findUrlResources
public static Collection<LoaderUtil.UrlResource> findUrlResources(String resource, boolean useTccl)
This method will only find resources that follow the JPMS rules for encapsulation. Resources on the class path should be found as normal along with resources with no package name in all modules. Resources within packages in modules must declare those resources open to org.apache.logging.log4j.- Parameters:
resource
- The resource to locate.- Returns:
- The located resources.
-
-