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()
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringIGNORE_TCCL_PROPERTYSystem property to set to ignore the thread context ClassLoader.
-
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.static java.lang.ClassLoader[]getClassLoaders()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.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
-
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
-
-
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.
-
getClassLoaders
public static java.lang.ClassLoader[] getClassLoaders()
-
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.- 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.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.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.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.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.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.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
-
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
-
-