Package org.apache.logging.log4j.util
Class LoaderUtil
- java.lang.Object
-
- org.apache.logging.log4j.util.LoaderUtil
-
@InternalApi public final class LoaderUtil extends java.lang.Object
Consider this class private. Utility class for ClassLoaders.- Since:
- 2.0.1
- 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 (package private) static java.lang.RuntimePermissionGET_CLASS_LOADER(package private) static LazyBooleanGET_CLASS_LOADER_DISABLEDstatic java.lang.StringIGNORE_TCCL_PROPERTYSystem property to set to ignore the thread context ClassLoader.private static java.lang.BooleanignoreTCCLprivate static LoggerLOGGERprivate 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.ClassLoadergetClassLoader()Returns the ClassLoader to use.static java.lang.ClassLoadergetClassLoader(java.lang.Class<?> class1, java.lang.Class<?> class2)private static java.lang.ClassLoadergetThisClassLoader()static java.lang.ClassLoadergetThreadContextClassLoader()Looks up the ClassLoader for this current thread.private static booleanisChild(java.lang.ClassLoader loader1, java.lang.ClassLoader loader2)Determines if one ClassLoader is a child of another 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 java.lang.Class<?>loadClassUnchecked(java.lang.String className)Loads and initializes a class given its fully qualified class 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> TnewCheckedInstanceOfProperty(java.lang.String propertyName, java.lang.Class<T> clazz, java.util.function.Supplier<T> defaultSupplier)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.static <T> TnewInstanceOfUnchecked(java.lang.Class<T> clazz)Creates an instance of the provided class using the default constructor.static <T> TnewInstanceOfUnchecked(java.lang.String className)Loads and instantiates a class by name using its default constructor.static <T> TnewInstanceOfUnchecked(java.lang.String className, java.lang.Class<T> supertype)Loads the provided class by name as a checked subtype of the given class.private static <T> TrunPrivileged(java.security.PrivilegedAction<T> action)
-
-
-
Field Detail
-
LOGGER
private static final Logger LOGGER
-
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
-
ignoreTCCL
private static java.lang.Boolean ignoreTCCL
-
GET_CLASS_LOADER
static final java.lang.RuntimePermission GET_CLASS_LOADER
-
GET_CLASS_LOADER_DISABLED
static final LazyBoolean GET_CLASS_LOADER_DISABLED
-
TCCL_GETTER
private static final java.security.PrivilegedAction<java.lang.ClassLoader> TCCL_GETTER
-
-
Method Detail
-
getClassLoader
public static java.lang.ClassLoader getClassLoader()
Returns the ClassLoader to use.- Returns:
- the ClassLoader.
- Since:
- 2.22.0
-
getClassLoader
public static java.lang.ClassLoader getClassLoader(java.lang.Class<?> class1, java.lang.Class<?> class2)- Since:
- 2.22.0
-
isChild
private static boolean isChild(java.lang.ClassLoader loader1, java.lang.ClassLoader loader2)Determines if one ClassLoader is a child of another ClassLoader. Note that anullClassLoader is interpreted as the system ClassLoader as per convention.- Parameters:
loader1- the ClassLoader to check for childhood.loader2- the ClassLoader to check for parenthood.- Returns:
trueif the first ClassLoader is a strict descendant of the second ClassLoader.
-
getThreadContextClassLoader
public static java.lang.ClassLoader getThreadContextClassLoader()
Looks up the ClassLoader for this current thread. If this class does not have the runtime permissiongetClassLoader, then the only ClassLoader this attempts to look up is the loader behind this class. When a SecurityManager is installed, this attempts to make a privileged call to get the current thread context ClassLoader, falling back to either the ClassLoader of this class or the system ClassLoader. When no SecurityManager is present, the same lookups are performed without use ofAccessController. If none of these strategies can obtain a ClassLoader, then this returnsnull.- Returns:
- the current thread's ClassLoader, a fallback loader, or null if no fallback can be determined
-
getThisClassLoader
private static java.lang.ClassLoader getThisClassLoader()
-
runPrivileged
private static <T> T runPrivileged(java.security.PrivilegedAction<T> action)
-
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- fully qualified class name to load- Returns:
- the loaded class
- Throws:
java.lang.ClassNotFoundException- if the specified class name could not be foundjava.lang.ExceptionInInitializerError- if an exception is thrown during class initializationjava.lang.LinkageError- if the linkage of the class fails for any other reason- Since:
- 2.1
-
loadClassUnchecked
public static java.lang.Class<?> loadClassUnchecked(java.lang.String className)
Loads and initializes a class given its fully qualified class name. All checked reflective operation exceptions are translated into equivalentLinkageErrorclasses.- Parameters:
className- fully qualified class name to load- Returns:
- the loaded class
- Throws:
java.lang.NoClassDefFoundError- if the specified class name could not be foundjava.lang.ExceptionInInitializerError- if an exception is thrown during class initializationjava.lang.LinkageError- if the linkage of the class fails for any other reason- Since:
- 2.22.0
- See Also:
loadClass(String)
-
newInstanceOf
public static <T> T newInstanceOf(java.lang.Class<T> clazz) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodExceptionLoads 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.NoSuchMethodException- if no zero-arg constructor existsjava.lang.SecurityException- if this class is not allowed to access declared members of the provided classjava.lang.IllegalAccessException- if the class can't be instantiated through a public constructorjava.lang.InstantiationException- if the provided class is abstract or an interfacejava.lang.reflect.InvocationTargetException- if an exception is thrown by the constructorjava.lang.ExceptionInInitializerError- if an exception was thrown while initializing the class- Since:
- 2.7
-
newInstanceOfUnchecked
public static <T> T newInstanceOfUnchecked(java.lang.Class<T> clazz)
Creates an instance of the provided class using the default constructor. All checked reflective operation exceptions are translated intoLinkageErrororInternalException.- Type Parameters:
T- the type of the object being instantiated- Parameters:
clazz- class to instantiate- Returns:
- instance of the class
- Throws:
java.lang.NoSuchMethodError- if no zero-arg constructor existsjava.lang.SecurityException- if this class is not allowed to access declared members of the provided classInternalException- if an exception is thrown by the constructorjava.lang.InstantiationError- if the provided class is abstract or an interfacejava.lang.IllegalAccessError- if the class cannot be accessed- Since:
- 2.22.0
-
newInstanceOf
public static <T> T newInstanceOf(java.lang.String className) throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException, java.lang.NoSuchMethodExceptionLoads and instantiates a Class using the default constructor.- Type Parameters:
T- type the class must be compatible with- Parameters:
className- fully qualified class name to load, initialize, and construct- Returns:
- new instance of the class
- Throws:
java.lang.ClassNotFoundException- if the class isn't available to the usual ClassLoadersjava.lang.ExceptionInInitializerError- if an exception was thrown while initializing the classjava.lang.LinkageError- if the linkage of the class fails for any other reasonjava.lang.ClassCastException- if the class is not compatible with the generic type parameter providedjava.lang.NoSuchMethodException- if no zero-arg constructor existsjava.lang.SecurityException- if this class is not allowed to access declared members of the provided classjava.lang.IllegalAccessException- if the class can't be instantiated through a public constructorjava.lang.InstantiationException- if the provided class is abstract or an interfacejava.lang.reflect.InvocationTargetException- if an exception is thrown by the constructor- 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.IllegalAccessException, java.lang.NoSuchMethodExceptionLoads 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.ExceptionInInitializerError- if an exception was thrown while initializing the classjava.lang.LinkageError- if the linkage of the class fails for any other reasonjava.lang.ClassCastException- if the class is not compatible with the generic type parameter providedjava.lang.NoSuchMethodException- if no zero-arg constructor existsjava.lang.SecurityException- if this class is not allowed to access declared members of the provided classjava.lang.IllegalAccessException- if the class can't be instantiated through a public constructorjava.lang.InstantiationException- if the provided class is abstract or an interfacejava.lang.reflect.InvocationTargetException- if an exception is thrown by the constructor- Since:
- 2.5
-
newCheckedInstanceOfProperty
public static <T> T newCheckedInstanceOfProperty(java.lang.String propertyName, java.lang.Class<T> clazz, java.util.function.Supplier<T> defaultSupplier) throws java.lang.ClassNotFoundException, java.lang.reflect.InvocationTargetException, java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.NoSuchMethodExceptionLoads 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.defaultSupplier- Supplier of a default value if the property is not present.- 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.ExceptionInInitializerError- if an exception was thrown while initializing the classjava.lang.LinkageError- if the linkage of the class fails for any other reasonjava.lang.ClassCastException- if the class is not compatible with the generic type parameter providedjava.lang.NoSuchMethodException- if no zero-arg constructor existsjava.lang.SecurityException- if this class is not allowed to access declared members of the provided classjava.lang.IllegalAccessException- if the class can't be instantiated through a public constructorjava.lang.InstantiationException- if the provided class is abstract or an interfacejava.lang.reflect.InvocationTargetException- if an exception is thrown by the constructor- Since:
- 2.22.0
-
newInstanceOfUnchecked
public static <T> T newInstanceOfUnchecked(java.lang.String className)
Loads and instantiates a class by name using its default constructor. All checked reflective operation exceptions are translated into correspondingLinkageErrorclasses.- Type Parameters:
T- type the class must be compatible with- Parameters:
className- fully qualified class name to load, initialize, and construct- Returns:
- new instance of the class
- Throws:
java.lang.NoClassDefFoundError- if the specified class name could not be foundjava.lang.ExceptionInInitializerError- if an exception is thrown during class initializationjava.lang.ClassCastException- if the class is not compatible with the generic type parameter providedjava.lang.NoSuchMethodError- if no zero-arg constructor existsjava.lang.SecurityException- if this class is not allowed to access declared members of the provided classInternalException- if an exception is thrown by the constructorjava.lang.InstantiationError- if the provided class is abstract or an interfacejava.lang.IllegalAccessError- if the class cannot be accessedjava.lang.LinkageError- if the linkage of the class fails for any other reason- Since:
- 2.22.0
-
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.IllegalAccessException, java.lang.NoSuchMethodExceptionLoads 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.ExceptionInInitializerError- if an exception is thrown during class initializationjava.lang.LinkageError- if the linkage of the class fails for any other reasonjava.lang.ClassCastException- if the constructed object isn't type compatible withTjava.lang.NoSuchMethodException- if no zero-arg constructor existsjava.lang.SecurityException- if this class is not allowed to access declared members of the provided classjava.lang.IllegalAccessException- if the class can't be instantiated through a public constructorjava.lang.InstantiationException- if the provided class is abstract or an interfacejava.lang.reflect.InvocationTargetException- if there was an exception whilst constructing the class- Since:
- 2.1
-
newInstanceOfUnchecked
public static <T> T newInstanceOfUnchecked(java.lang.String className, java.lang.Class<T> supertype)Loads the provided class by name as a checked subtype of the given class. All checked reflective operation exceptions are translated into correspondingLinkageErrorclasses.- Type Parameters:
T- type of instance to return- Parameters:
className- fully qualified class name to loadsupertype- supertype of the class being loaded- Returns:
- new instance of the requested class
- Throws:
java.lang.NoClassDefFoundError- if the provided class name could not be foundjava.lang.ExceptionInInitializerError- if an exception is thrown during class initializationjava.lang.ClassCastException- if the loaded class is not a subtype of the provided classjava.lang.NoSuchMethodError- if no zero-arg constructor existsjava.lang.SecurityException- if this class is not allowed to access declared members of the provided classInternalException- if an exception is thrown by the constructorjava.lang.InstantiationError- if the provided class is abstract or an interfacejava.lang.IllegalAccessError- if the class cannot be accessedjava.lang.LinkageError- if the linkage of the class fails for any other reason- Since:
- 2.22.0
-
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)
-
-