Top Description Inners Fields Constructors Methods
org.apache.logging.log4j.util

public final Class LoaderUtil

extends Object
Class Inheritance
Annotations
@InternalApi
Imports
java.io.IOException, java.lang.reflect.Constructor, .InvocationTargetException, java.net.URL, java.security.AccessController, .PrivilegedAction, java.util.Collection, .Enumeration, .LinkedHashSet, .Objects, java.util.function.Supplier

Consider this class private. Utility class for ClassLoaders.
See Also
ClassLoader, RuntimePermission, Thread#getContextClassLoader(), ClassLoader#getSystemClassLoader()

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
private static final RuntimePermission
private static final boolean
public static final String
IGNORE_TCCL_PROPERTY

System property to set to ignore the thread context ClassLoader.

private static Boolean
private static final PrivilegedAction<ClassLoader>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Collection<URL>

Returns:

a Collection of URLs matching the resource name. If no resources could be found, then this will be empty.
findResources
(final String
the name of the resource to find.
resource
)

Finds classpath resources.

pack-priv static Collection<URL>
findResources(final String resource, final boolean useTccl)

pack-priv static Collection<LoaderUtil.UrlResource>
findUrlResources(final String resource, final boolean useTccl)

public static ClassLoader

Returns:

the ClassLoader.
getClassLoader
()

Returns the ClassLoader to use.

public static ClassLoader
getClassLoader(final Class<?> class1, final Class<?> class2)

private static ClassLoader
public static ClassLoader

Returns:

the current thread's ClassLoader, a fallback loader, or null if no fallback can be determined
getThreadContextClassLoader
()

Looks up the ClassLoader for this current thread.

private static boolean

Returns:

true if the first ClassLoader is a strict descendant of the second ClassLoader.
isChild
(final ClassLoader
the ClassLoader to check for childhood.
loader1
,
final ClassLoader
the ClassLoader to check for parenthood.
loader2
)

Determines if one ClassLoader is a child of another ClassLoader.

public static boolean

Returns:

true if the class could be found or false otherwise.
isClassAvailable
(final String
The class name.
className
)

Determines if a named Class can be loaded or not.

private static boolean
public static Class<?>

Returns:

the loaded class
loadClass
(final String
fully qualified class name to load
className
)

Loads a class by name.

public static Class<?>

Returns:

the loaded class
loadClassUnchecked
(final String
fully qualified class name to load
className
)

Loads and initializes a class given its fully qualified class name.

public static <
The type of the class to check.
T
>
T

Returns:

new instance of the class cast to T
newCheckedInstanceOf
(final String
The class name.
className
,
final Class<T>
The class to cast it to.
clazz
)

Loads and instantiates a derived class using its default constructor.

public static <
The type to cast it to.
T
>
T

Returns:

new instance of the class given in the property or null if the property was unset.
newCheckedInstanceOfProperty
(final String
The property name to look up a class name for.
propertyName
,
final Class<T>
The class to cast it to.
clazz
)

Loads and instantiates a class given by a property name.

public static <
The type to cast it to.
T
>
T

Returns:

new instance of the class given in the property or null if the property was unset.
newCheckedInstanceOfProperty
(final String
The property name to look up a class name for.
propertyName
,
final Class<T>
The class to cast it to.
clazz
,
final Supplier<T>
Supplier of a default value if the property is not present.
defaultSupplier
)

Loads and instantiates a class given by a property name.

public static <
the type of the class modeled by the Class object.
T
>
T

Returns:

new instance of the class.
newInstanceOf
(final Class<T>
The class.
clazz
)

Loads and instantiates a Class using the default constructor.

public static <
type the class must be compatible with
T
>
T

Returns:

new instance of the class
newInstanceOf
(final String
fully qualified class name to load, initialize, and construct
className
)

Loads and instantiates a Class using the default constructor.

public static <
the type of the object being instantiated
T
>
T

Returns:

instance of the class
newInstanceOfUnchecked
(final Class<T>
class to instantiate
clazz
)

Creates an instance of the provided class using the default constructor.

public static <
type the class must be compatible with
T
>
T

Returns:

new instance of the class
newInstanceOfUnchecked
(final String
fully qualified class name to load, initialize, and construct
className
)

Loads and instantiates a class by name using its default constructor.

public static <
type of instance to return
T
>
T

Returns:

new instance of the requested class
newInstanceOfUnchecked
(final String
fully qualified class name to load
className
,
final Class<T>
supertype of the class being loaded
supertype
)

Loads the provided class by name as a checked subtype of the given class.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

GET_CLASS_LOADERback to summary
private static final RuntimePermission GET_CLASS_LOADER
GET_CLASS_LOADER_DISABLEDback to summary
private static final boolean GET_CLASS_LOADER_DISABLED
IGNORE_TCCL_PROPERTYback to summary
public static final String IGNORE_TCCL_PROPERTY

System property to set to ignore the thread context ClassLoader.

Since
2.1
ignoreTCCLback to summary
private static Boolean ignoreTCCL
TCCL_GETTERback to summary
private static final PrivilegedAction<ClassLoader> TCCL_GETTER

Constructor Detail

LoaderUtilback to summary
private LoaderUtil()

Method Detail

findResourcesback to summary
public static Collection<URL> findResources(final String resource)

Finds classpath resources.

Parameters
resource:String

the name of the resource to find.

Returns:Collection<URL>

a Collection of URLs matching the resource name. If no resources could be found, then this will be empty.

Since
2.1
findResourcesback to summary
pack-priv static Collection<URL> findResources(final String resource, final boolean useTccl)
findUrlResourcesback to summary
pack-priv static Collection<LoaderUtil.UrlResource> findUrlResources(final String resource, final boolean useTccl)
getClassLoaderback to summary
public static ClassLoader getClassLoader()

Returns the ClassLoader to use.

Returns:ClassLoader

the ClassLoader.

getClassLoaderback to summary
public static ClassLoader getClassLoader(final Class<?> class1, final Class<?> class2)
getThisClassLoaderback to summary
private static ClassLoader getThisClassLoader()
getThreadContextClassLoaderback to summary
public static ClassLoader getThreadContextClassLoader()

Looks up the ClassLoader for this current thread. If this class does not have the runtime permission getClassLoader, 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 of AccessController. If none of these strategies can obtain a ClassLoader, then this returns null.

Returns:ClassLoader

the current thread's ClassLoader, a fallback loader, or null if no fallback can be determined

isChildback to summary
private static boolean isChild(final ClassLoader loader1, final ClassLoader loader2)

Determines if one ClassLoader is a child of another ClassLoader. Note that a null ClassLoader is interpreted as the system ClassLoader as per convention.

Parameters
loader1:ClassLoader

the ClassLoader to check for childhood.

loader2:ClassLoader

the ClassLoader to check for parenthood.

Returns:boolean

true if the first ClassLoader is a strict descendant of the second ClassLoader.

isClassAvailableback to summary
public static boolean isClassAvailable(final String className)

Determines if a named Class can be loaded or not.

Parameters
className:String

The class name.

Returns:boolean

true if the class could be found or false otherwise.

Since
2.7
isIgnoreTcclback to summary
private static boolean isIgnoreTccl()
loadClassback to summary
public static Class<?> loadClass(final String className) throws ClassNotFoundException

Loads a class by name. This method respects the IGNORE_TCCL_PROPERTY Log4j property. If this property is specified and set to anything besides false, then the default ClassLoader will be used.

Parameters
className:String

fully qualified class name to load

Returns:Class<?>

the loaded class

Exceptions
ClassNotFoundException:
if the specified class name could not be found
ExceptionInInitializerError:
if an exception is thrown during class initialization
LinkageError:
if the linkage of the class fails for any other reason
Since
2.1
loadClassUncheckedback to summary
public static Class<?> loadClassUnchecked(final String className)

Loads and initializes a class given its fully qualified class name. All checked reflective operation exceptions are translated into equivalent LinkageError classes.

Parameters
className:String

fully qualified class name to load

Returns:Class<?>

the loaded class

Exceptions
NoClassDefFoundError:
if the specified class name could not be found
ExceptionInInitializerError:
if an exception is thrown during class initialization
LinkageError:
if the linkage of the class fails for any other reason
Since
2.22.0
See Also
loadClass(String)
newCheckedInstanceOfback to summary
public static <T> T newCheckedInstanceOf(final String className, final Class<T> clazz) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException

Loads and instantiates a derived class using its default constructor.

Parameters
<T>
The type of the class to check.
className:String

The class name.

clazz:Class<T>

The class to cast it to.

Returns:T

new instance of the class cast to T

Exceptions
ClassNotFoundException:
if the class isn't available to the usual ClassLoaders
InvocationTargetException:
if there was an exception whilst constructing the class
InstantiationException:
if the provided class is abstract or an interface
IllegalAccessException:
if the class can't be instantiated through a public constructor
NoSuchMethodException:
if no zero-arg constructor exists
ExceptionInInitializerError:
if an exception is thrown during class initialization
LinkageError:
if the linkage of the class fails for any other reason
ClassCastException:
if the constructed object isn't type compatible with T
SecurityException:
if this class is not allowed to access declared members of the provided class
Since
2.1
newCheckedInstanceOfPropertyback to summary
public static <T> T newCheckedInstanceOfProperty(final String propertyName, final Class<T> clazz) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException

Loads and instantiates a class given by a property name.

Parameters
<T>
The type to cast it to.
propertyName:String

The property name to look up a class name for.

clazz:Class<T>

The class to cast it to.

Returns:T

new instance of the class given in the property or null if the property was unset.

Exceptions
ClassNotFoundException:
if the class isn't available to the usual ClassLoaders
InvocationTargetException:
if an exception is thrown by the constructor
InstantiationException:
if the provided class is abstract or an interface
IllegalAccessException:
if the class can't be instantiated through a public constructor
NoSuchMethodException:
if no zero-arg constructor exists
ExceptionInInitializerError:
if an exception was thrown while initializing the class
LinkageError:
if the linkage of the class fails for any other reason
ClassCastException:
if the class is not compatible with the generic type parameter provided
SecurityException:
if this class is not allowed to access declared members of the provided class
Since
2.5
newCheckedInstanceOfPropertyback to summary
public static <T> T newCheckedInstanceOfProperty(final String propertyName, final Class<T> clazz, final Supplier<T> defaultSupplier) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException

Loads and instantiates a class given by a property name.

Parameters
<T>
The type to cast it to.
propertyName:String

The property name to look up a class name for.

clazz:Class<T>

The class to cast it to.

defaultSupplier:Supplier<T>

Supplier of a default value if the property is not present.

Returns:T

new instance of the class given in the property or null if the property was unset.

Exceptions
ClassNotFoundException:
if the class isn't available to the usual ClassLoaders
InvocationTargetException:
if an exception is thrown by the constructor
InstantiationException:
if the provided class is abstract or an interface
IllegalAccessException:
if the class can't be instantiated through a public constructor
NoSuchMethodException:
if no zero-arg constructor exists
ExceptionInInitializerError:
if an exception was thrown while initializing the class
LinkageError:
if the linkage of the class fails for any other reason
ClassCastException:
if the class is not compatible with the generic type parameter provided
SecurityException:
if this class is not allowed to access declared members of the provided class
Since
2.22
newInstanceOfback to summary
public static <T> T newInstanceOf(final Class<T> clazz) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException

Loads and instantiates a Class using the default constructor.

Parameters
<T>
the type of the class modeled by the Class object.
clazz:Class<T>

The class.

Returns:T

new instance of the class.

Exceptions
InstantiationException:
if the provided class is abstract or an interface
IllegalAccessException:
if the class can't be instantiated through a public constructor
InvocationTargetException:
if an exception is thrown by the constructor
NoSuchMethodException:
if no zero-arg constructor exists
SecurityException:
if this class is not allowed to access declared members of the provided class
ExceptionInInitializerError:
if an exception was thrown while initializing the class
Since
2.7
newInstanceOfback to summary
public static <T> T newInstanceOf(final String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException

Loads and instantiates a Class using the default constructor.

Parameters
<T>
type the class must be compatible with
className:String

fully qualified class name to load, initialize, and construct

Returns:T

new instance of the class

Exceptions
ClassNotFoundException:
if the class isn't available to the usual ClassLoaders
IllegalAccessException:
if the class can't be instantiated through a public constructor
InstantiationException:
if the provided class is abstract or an interface
InvocationTargetException:
if an exception is thrown by the constructor
NoSuchMethodException:
if no zero-arg constructor exists
ExceptionInInitializerError:
if an exception was thrown while initializing the class
LinkageError:
if the linkage of the class fails for any other reason
ClassCastException:
if the class is not compatible with the generic type parameter provided
SecurityException:
if this class is not allowed to access declared members of the provided class
Since
2.1
newInstanceOfUncheckedback to summary
public static <T> T newInstanceOfUnchecked(final Class<T> clazz)

Creates an instance of the provided class using the default constructor. All checked reflective operation exceptions are translated into LinkageError or InternalException.

Parameters
<T>
the type of the object being instantiated
clazz:Class<T>

class to instantiate

Returns:T

instance of the class

Exceptions
NoSuchMethodError:
if no zero-arg constructor exists
SecurityException:
if this class is not allowed to access declared members of the provided class
InternalException:
if an exception is thrown by the constructor
InstantiationError:
if the provided class is abstract or an interface
IllegalAccessError:
if the class cannot be accessed
Since
2.22.0
newInstanceOfUncheckedback to summary
public static <T> T newInstanceOfUnchecked(final String className)

Loads and instantiates a class by name using its default constructor. All checked reflective operation exceptions are translated into corresponding LinkageError classes.

Parameters
<T>
type the class must be compatible with
className:String

fully qualified class name to load, initialize, and construct

Returns:T

new instance of the class

Exceptions
NoClassDefFoundError:
if the specified class name could not be found
ExceptionInInitializerError:
if an exception is thrown during class initialization
ClassCastException:
if the class is not compatible with the generic type parameter provided
NoSuchMethodError:
if no zero-arg constructor exists
SecurityException:
if this class is not allowed to access declared members of the provided class
InternalException:
if an exception is thrown by the constructor
InstantiationError:
if the provided class is abstract or an interface
IllegalAccessError:
if the class cannot be accessed
LinkageError:
if the linkage of the class fails for any other reason
Since
2.22.0
newInstanceOfUncheckedback to summary
public static <T> T newInstanceOfUnchecked(final String className, final 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 corresponding LinkageError classes.

Parameters
<T>
type of instance to return
className:String

fully qualified class name to load

supertype:Class<T>

supertype of the class being loaded

Returns:T

new instance of the requested class

Exceptions
NoClassDefFoundError:
if the provided class name could not be found
ExceptionInInitializerError:
if an exception is thrown during class initialization
ClassCastException:
if the loaded class is not a subtype of the provided class
NoSuchMethodError:
if no zero-arg constructor exists
SecurityException:
if this class is not allowed to access declared members of the provided class
InternalException:
if an exception is thrown by the constructor
InstantiationError:
if the provided class is abstract or an interface
IllegalAccessError:
if the class cannot be accessed
LinkageError:
if the linkage of the class fails for any other reason
Since
2.22.0
org.apache.logging.log4j.util back to summary

private Class LoaderUtil.ThreadContextClassLoaderGetter

extends Object
implements PrivilegedAction<ClassLoader>
Class Inheritance
  • java.lang.Object
  • org.apache.logging.log4j.util.LoaderUtil.ThreadContextClassLoaderGetter
All Implemented Interfaces
java.security.PrivilegedAction

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public ClassLoader
run()

Implements java.security.PrivilegedAction.run.

Performs the computation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ThreadContextClassLoaderGetterback to summary
private ThreadContextClassLoaderGetter()

Method Detail

runback to summary
public ClassLoader run()

Implements java.security.PrivilegedAction.run.

Doc from java.security.PrivilegedAction.run.

Performs the computation. This method will be called by AccessController.doPrivileged after enabling privileges.

Returns:ClassLoader

a class-dependent value that may represent the results of the computation. Each class that implements PrivilegedAction should document what (if anything) this value represents.

Annotations
@Override
org.apache.logging.log4j.util back to summary

pack-priv Class LoaderUtil.UrlResource

extends Object
Class Inheritance

URL and ClassLoader pair.

Field Summary

Modifier and TypeField and Description
private final ClassLoader
private final URL

Constructor Summary

AccessConstructor and Description
pack-priv
UrlResource(final ClassLoader classLoader, final URL url)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(final Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public ClassLoader
public URL
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

classLoaderback to summary
private final ClassLoader classLoader
urlback to summary
private final URL url

Constructor Detail

UrlResourceback to summary
pack-priv UrlResource(final ClassLoader classLoader, final URL url)

Method Detail

equalsback to summary
public boolean equals(final Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getClassLoaderback to summary
public ClassLoader getClassLoader()
getUrlback to summary
public URL getUrl()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override