Class Loader
java.lang.Object
org.apache.logging.log4j.core.util.Loader
Load resources (or images) from various sources.
-
Method Summary
Modifier and TypeMethodDescriptionstatic ClassLoaderReturns the ClassLoader to use.static ClassLoadergetClassLoader(Class<?> class1, Class<?> class2) static @Nullable URLgetResource(String resource, ClassLoader defaultLoader) This method will search forresourcein different places.static InputStreamgetResourceAsStream(String resource, ClassLoader defaultLoader) This method will search forresourcein different places.static ClassLoaderReturns the ClassLoader of current thread if possible, or falls back to the system ClassLoader if none is available.static Class<?>initializeClass(String className, ClassLoader loader) Loads and initializes a named Class using a given ClassLoader.static booleanisClassAvailable(String className) Determines if a named Class can be loaded or not.static booleanDeprecated.Since 2.25.0 without a replacement.static Class<?>Loads a class by name.static Class<?>loadClass(String className, ClassLoader loader) Loads a named Class using a given ClassLoader.static Class<?>loadSystemClass(String className) Load a Class in thejava.*namespace by name.static <T> TnewCheckedInstanceOf(String className, Class<T> clazz) Loads, instantiates, and casts a Class using the default constructor.static <T> TnewCheckedInstanceOfProperty(String propertyName, Class<T> clazz) Loads and instantiates a class given by a property name.static <T> TnewInstanceOf(String className) Loads and instantiates a Class using the default constructor.
-
Method Details
-
getClassLoader
Returns the ClassLoader to use.- Returns:
- the ClassLoader.
-
getThreadContextClassLoader
Returns the ClassLoader of current thread if possible, or falls back to the system ClassLoader if none is available.- Returns:
- the TCCL.
- See Also:
-
getClassLoader
-
getResource
This method will search forresourcein different places. The search order is as follows:- Search for
resourceusing the thread context class loader under Java2. If that fails, search forresourceusing the class loader that loaded this class (Loader). Under JDK 1.1, only the class loader that loaded this class (Loader) is used. - Try one last time with
ClassLoader.getSystemResource(resource), that is using the system class loader in JDK 1.2 and virtual machine's built-in class loader in JDK 1.1.
- Parameters:
resource- The resource to load.defaultLoader- The default ClassLoader.- Returns:
- A URL to the resource or
null.
- Search for
-
getResourceAsStream
This method will search forresourcein different places. The search order is as follows:- Search for
resourceusing the thread context class loader under Java2. If that fails, search forresourceusing the class loader that loaded this class (Loader). Under JDK 1.1, only the class loader that loaded this class (Loader) is used. - Try one last time with
ClassLoader.getSystemResource(resource), that is using the system class loader in JDK 1.2 and virtual machine's built-in class loader in JDK 1.1.
- Parameters:
resource- The resource to load.defaultLoader- The default ClassLoader.- Returns:
- An InputStream to read the resouce.
- Search for
-
initializeClass
public static Class<?> initializeClass(String className, ClassLoader loader) throws ClassNotFoundException Loads and initializes a named Class using a given ClassLoader.- Parameters:
className- The class name.loader- The class loader.- Returns:
- The class.
- Throws:
ClassNotFoundException- if the class could not be found.
-
loadClass
public static Class<?> loadClass(String className, ClassLoader loader) throws ClassNotFoundException Loads a named Class using a given ClassLoader.- Parameters:
className- The class name.loader- The class loader.- Returns:
- The class, or null if loader is null.
- Throws:
ClassNotFoundException- if the class could not be found.
-
loadSystemClass
Load a Class in thejava.*namespace by name. Useful for peculiar scenarios typically involving Google App Engine.- Parameters:
className- The class name.- Returns:
- The Class.
- Throws:
ClassNotFoundException- if the Class could not be found.
-
newInstanceOf
public static <T> T newInstanceOf(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException Loads and instantiates a Class using the default constructor.- Parameters:
className- The class name.- Returns:
- new instance of the class.
- Throws:
ClassNotFoundException- if the class isn't available to the usual ClassLoadersIllegalAccessException- if the class can't be instantiated through a public constructorInstantiationException- if there was an exception whilst instantiating the classNoSuchMethodException- if there isn't a no-args constructor on the classInvocationTargetException- if there was an exception whilst constructing the class
-
newCheckedInstanceOf
public static <T> T newCheckedInstanceOf(String className, Class<T> clazz) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException Loads, instantiates, and casts a Class using the default constructor.- Type Parameters:
T- The type to cast it to.- 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 ClassLoadersIllegalAccessException- if the class can't be instantiated through a public constructorInstantiationException- if there was an exception whilst instantiating the classNoSuchMethodException- if there isn't a no-args constructor on the classInvocationTargetException- if there was an exception whilst constructing the classClassCastException- if the constructed object isn't type compatible withT
-
newCheckedInstanceOfProperty
public static <T> T newCheckedInstanceOfProperty(String propertyName, Class<T> clazz) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException 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 ClassLoadersIllegalAccessException- if the class can't be instantiated through a public constructorInstantiationException- if there was an exception whilst instantiating the classNoSuchMethodException- if there isn't a no-args constructor on the classInvocationTargetException- if there was an exception whilst constructing the classClassCastException- if the constructed object isn't type compatible withT
-
isClassAvailable
Determines if a named Class can be loaded or not.- Parameters:
className- The class name.- Returns:
trueif the class could be found orfalseotherwise.
-
isJansiAvailable
Deprecated.Since 2.25.0 without a replacement. -
loadClass
Loads 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:
ClassNotFoundException- if the specified class name could not be found
-