Class LoaderUtil

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

public final class LoaderUtil extends Object
Consider this class private. Utility class for ClassLoaders.
See Also:
  • Field Details

    • EMPTY_CLASS_LOADER_ARRAY

      private static final ClassLoader[] EMPTY_CLASS_LOADER_ARRAY
    • 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:
    • SECURITY_MANAGER

      private static final SecurityManager SECURITY_MANAGER
    • ignoreTCCL

      private static Boolean ignoreTCCL
    • GET_CLASS_LOADER_DISABLED

      private static final boolean GET_CLASS_LOADER_DISABLED
    • TCCL_GETTER

      private static final PrivilegedAction<ClassLoader> TCCL_GETTER
  • Constructor Details

    • LoaderUtil

      private LoaderUtil()
  • Method Details

    • 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.
    • isClassAvailable

      public static boolean isClassAvailable(String className)
      Determines if a named Class can be loaded or not.
      Parameters:
      className - The class name.
      Returns:
      true if the class could be found or false otherwise.
      Since:
      2.7
    • 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 <T> T newInstanceOf(Class<T> clazz) throws InstantiationException, IllegalAccessException, InvocationTargetException
      Loads and instantiates a Class using the default constructor.
      Type Parameters:
      T - the type of the class modeled by the Class object.
      Parameters:
      clazz - The class.
      Returns:
      new instance of the class.
      Throws:
      IllegalAccessException - if the class can't be instantiated through a public constructor
      InstantiationException - if there was an exception whilst instantiating the class
      InvocationTargetException - if there was an exception whilst constructing the class
      Since:
      2.7
    • newInstanceOf

      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
      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, 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
      InvocationTargetException - if there was an exception whilst constructing the class
      ClassCastException - if the constructed object isn't type compatible with T
      Since:
      2.1
    • newCheckedInstanceOfProperty

      public static <T> T newCheckedInstanceOfProperty(String propertyName, Class<T> clazz) throws ClassNotFoundException, 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 null if the property was unset.
      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
      InvocationTargetException - if there was an exception whilst constructing the class
      ClassCastException - if the constructed object isn't type compatible with T
      Since:
      2.5
    • isIgnoreTccl

      private static boolean isIgnoreTccl()
    • 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
    • findResources

      static Collection<URL> findResources(String resource, boolean useTccl)
    • findUrlResources

      static Collection<LoaderUtil.UrlResource> findUrlResources(String resource, boolean useTccl)