Package org.apache.logging.log4j.util
Class LoaderUtil
- java.lang.Object
-
- org.apache.logging.log4j.util.LoaderUtil
-
public final class LoaderUtil extends java.lang.ObjectConsider 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 classLoaderUtil.ThreadContextClassLoaderGetter(package private) static classLoaderUtil.UrlResourceURLandClassLoaderpair.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.ClassLoader[]EMPTY_CLASS_LOADER_ARRAYprivate static booleanGET_CLASS_LOADER_DISABLEDstatic java.lang.StringIGNORE_TCCL_PROPERTYSystem property to set to ignore the thread context ClassLoader.private static java.lang.BooleanignoreTCCLprivate static java.lang.SecurityManagerSECURITY_MANAGERprivate static java.security.PrivilegedAction<java.lang.ClassLoader>TCCL_GETTER
-
Constructor Summary
Constructors Modifier Constructor Description privateLoaderUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.Collection<java.net.URL>findResources(java.lang.String resource)Finds classpath resources.(package private) static java.util.Collection<java.net.URL>findResources(java.lang.String resource, boolean useTccl)(package private) static java.util.Collection<LoaderUtil.UrlResource>findUrlResources(java.lang.String resource, boolean useTccl)static java.lang.ClassLoadergetThreadContextClassLoader()Gets the current Thread ClassLoader.static booleanisClassAvailable(java.lang.String className)Determines if a named Class can be loaded or not.private static booleanisIgnoreTccl()static java.lang.Class<?>loadClass(java.lang.String className)Loads a class by name.static <T> TnewCheckedInstanceOf(java.lang.String className, java.lang.Class<T> clazz)Loads and instantiates a derived class using its default constructor.static <T> TnewCheckedInstanceOfProperty(java.lang.String propertyName, java.lang.Class<T> clazz)Loads and instantiates a class given by a property name.static <T> TnewInstanceOf(java.lang.Class<T> clazz)Loads and instantiates a Class using the default constructor.static <T> TnewInstanceOf(java.lang.String className)Loads and instantiates a Class using the default constructor.
-
-
-
Field Detail
-
EMPTY_CLASS_LOADER_ARRAY
private static final java.lang.ClassLoader[] EMPTY_CLASS_LOADER_ARRAY
-
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 isnullas well, then the ClassLoader for this class is returned. If running with aSecurityManagerthat does not allow access to the Thread ClassLoader or system ClassLoader, then the ClassLoader for this class is returned.- Returns:
- the current ThreadContextClassLoader.
-
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:
trueif the class could be found orfalseotherwise.- Since:
- 2.7
-
loadClass
public static java.lang.Class<?> loadClass(java.lang.String className) throws java.lang.ClassNotFoundExceptionLoads a class by name. This method respects theIGNORE_TCCL_PROPERTYLog4j 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.InvocationTargetExceptionLoads and instantiates a Class using the default constructor.- Type Parameters:
T- the type of the class modeled by theClassobject.- 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.reflect.InvocationTargetExceptionLoads 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.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.reflect.InvocationTargetException, java.lang.InstantiationException, java.lang.IllegalAccessExceptionLoads 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.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.reflect.InvocationTargetException, java.lang.InstantiationException, java.lang.IllegalAccessExceptionLoads 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
nullif 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.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
-
findResources
static java.util.Collection<java.net.URL> findResources(java.lang.String resource, boolean useTccl)
-
findUrlResources
static java.util.Collection<LoaderUtil.UrlResource> findUrlResources(java.lang.String resource, boolean useTccl)
-
-