Package org.apache.logging.log4j.util
Class LoaderUtil
java.lang.Object
org.apache.logging.log4j.util.LoaderUtil
Consider this class private. Utility class for ClassLoaders.
- Since:
- 2.0.1
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringSystem property to set to ignore the thread context ClassLoader.
- 
Method SummaryModifier and TypeMethodDescriptionstatic Collection<URL>findResources(String resource) Finds classpath resources.static ClassLoaderReturns the ClassLoader to use.static ClassLoadergetClassLoader(Class<?> class1, Class<?> class2) static ClassLoaderLooks up the ClassLoader for this current thread.static booleanisClassAvailable(String className) Determines if a named Class can be loaded or not.static Class<?>Loads a class by name.static Class<?>loadClassUnchecked(String className) Loads and initializes a class given its fully qualified class name.static <T> TnewCheckedInstanceOf(String className, Class<T> clazz) Loads and instantiates a derived class using its default constructor.static <T> TnewCheckedInstanceOfProperty(String propertyName, Class<T> clazz) Loads and instantiates a class given by a property name.static <T> TnewCheckedInstanceOfProperty(String propertyName, Class<T> clazz, Supplier<T> defaultSupplier) Loads and instantiates a class given by a property name.static <T> TnewInstanceOf(Class<T> clazz) Loads and instantiates a Class using the default constructor.static <T> TnewInstanceOf(String className) Loads and instantiates a Class using the default constructor.static <T> TnewInstanceOfUnchecked(Class<T> clazz) Creates an instance of the provided class using the default constructor.static <T> TnewInstanceOfUnchecked(String className) Loads and instantiates a class by name using its default constructor.static <T> TnewInstanceOfUnchecked(String className, Class<T> supertype) Loads the provided class by name as a checked subtype of the given class.
- 
Field Details- 
IGNORE_TCCL_PROPERTYSystem property to set to ignore the thread context ClassLoader.- Since:
- 2.1
- See Also:
 
 
- 
- 
Method Details- 
getClassLoaderReturns the ClassLoader to use.- Returns:
- the ClassLoader.
- Since:
- 2.22.0
 
- 
getClassLoader- Since:
- 2.22.0
 
- 
getThreadContextClassLoaderLooks 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
 
- 
isClassAvailableDetermines if a named Class can be loaded or not.- Parameters:
- className- The class name.
- Returns:
- trueif the class could be found or- falseotherwise.
- Since:
- 2.7
 
- 
loadClassLoads 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:
- 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
 
- 
loadClassUncheckedLoads 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:
- 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:
 
- 
newInstanceOfpublic static <T> T newInstanceOf(Class<T> clazz) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException Loads and instantiates a Class using the default constructor.- Type Parameters:
- T- the type of the class modeled by the- Classobject.
- Parameters:
- clazz- The class.
- Returns:
- new instance of the class.
- Throws:
- NoSuchMethodException- if no zero-arg constructor exists
- SecurityException- if this class is not allowed to access declared members of the provided class
- 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
- ExceptionInInitializerError- if an exception was thrown while initializing the class
- Since:
- 2.7
 
- 
newInstanceOfUncheckedCreates 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:
- 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
 
- 
newInstanceOfpublic static <T> T newInstanceOf(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException Loads 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:
- ClassNotFoundException- if the class isn't available to the usual ClassLoaders
- 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
- NoSuchMethodException- if no zero-arg constructor exists
- SecurityException- if this class is not allowed to access declared members of the provided class
- 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
- Since:
- 2.1
 
- 
newCheckedInstanceOfPropertypublic static <T> T newCheckedInstanceOfProperty(String propertyName, Class<T> clazz) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException 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 nullif the property was unset.
- Throws:
- ClassNotFoundException- if the class isn't available to the usual ClassLoaders
- 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
- NoSuchMethodException- if no zero-arg constructor exists
- SecurityException- if this class is not allowed to access declared members of the provided class
- 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
- Since:
- 2.5
 
- 
newCheckedInstanceOfPropertypublic static <T> T newCheckedInstanceOfProperty(String propertyName, Class<T> clazz, Supplier<T> defaultSupplier) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException 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.
- 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:
- ClassNotFoundException- if the class isn't available to the usual ClassLoaders
- 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
- NoSuchMethodException- if no zero-arg constructor exists
- SecurityException- if this class is not allowed to access declared members of the provided class
- 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
- Since:
- 2.22.0
 
- 
newInstanceOfUncheckedLoads 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:
- 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
 
- 
newCheckedInstanceOfpublic static <T> T newCheckedInstanceOf(String className, Class<T> clazz) throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException, NoSuchMethodException 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 ClassLoaders
- 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
- NoSuchMethodException- if no zero-arg constructor exists
- SecurityException- if this class is not allowed to access declared members of the provided class
- IllegalAccessException- if the class can't be instantiated through a public constructor
- InstantiationException- if the provided class is abstract or an interface
- InvocationTargetException- if there was an exception whilst constructing the class
- Since:
- 2.1
 
- 
newInstanceOfUncheckedLoads 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 load
- supertype- supertype of the class being loaded
- Returns:
- new instance of the requested class
- Throws:
- 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
 
- 
findResourcesFinds 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
 
 
-