org.apache.logging.log4j.util
Class LoaderUtil

java.lang.Object
  extended by org.apache.logging.log4j.util.LoaderUtil

public final class LoaderUtil
extends Object

Consider this class private. Utility class for ClassLoaders.

See Also:
ClassLoader, RuntimePermission, Thread.getContextClassLoader(), ClassLoader.getSystemClassLoader()

Field Summary
static String IGNORE_TCCL_PROPERTY
          System property to set to ignore the thread context ClassLoader.
 
Method Summary
static Collection<URL> findResources(String resource)
          Finds classpath resources.
static ClassLoader getThreadContextClassLoader()
          Gets the current Thread ClassLoader.
static Class<?> loadClass(String className)
          Loads a class by name.
static
<T> T
newCheckedInstanceOf(String className, Class<T> clazz)
          Loads and instantiates a derived class using its default constructor.
static Object newInstanceOf(String className)
          Loads and instantiates a Class using the default constructor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IGNORE_TCCL_PROPERTY

public static final 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 ClassLoader getThreadContextClassLoader()
Gets the current Thread ClassLoader. Returns the system ClassLoader if the TCCL is null. If the system ClassLoader is null as well, then the ClassLoader for this class is returned. If running with a SecurityManager that does not allow access to the Thread ClassLoader or system ClassLoader, then the ClassLoader for this class is returned.

Returns:
the current ThreadContextClassLoader.

loadClass

public static Class<?> loadClass(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 - The class name.
Returns:
the Class for the given name.
Throws:
ClassNotFoundException - if the specified class name could not be found
Since:
2.1

newInstanceOf

public static Object 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 ClassLoaders
IllegalAccessException - if the class can't be instantiated through a public constructor
InstantiationException - if there was an exception whilst instantiating the class
NoSuchMethodException - if there isn't a no-args constructor on the class
InvocationTargetException - if there was an exception whilst constructing the class
Since:
2.1

newCheckedInstanceOf

public static <T> T newCheckedInstanceOf(String className,
                                         Class<T> clazz)
                              throws ClassNotFoundException,
                                     NoSuchMethodException,
                                     InvocationTargetException,
                                     InstantiationException,
                                     IllegalAccessException
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
IllegalAccessException - if the class can't be instantiated through a public constructor
InstantiationException - if there was an exception whilst instantiating the class
NoSuchMethodException - if there isn't a no-args constructor on the class
InvocationTargetException - if there was an exception whilst constructing the class
ClassCastException - if the constructed object isn't type compatible with T
Since:
2.1

findResources

public static Collection<URL> findResources(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


Copyright © 1999-2014 Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.