Class LoaderUtil


  • public final class LoaderUtil
    extends java.lang.Object
    Consider this class private. Utility class for ClassLoaders.
    See Also:
    ClassLoader, RuntimePermission, Thread.getContextClassLoader(), ClassLoader.getSystemClassLoader()
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private LoaderUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static void accumulateClassLoaders​(java.lang.ClassLoader loader, java.util.Collection<java.lang.ClassLoader> loaders)
      Adds the provided loader to the loaders collection, and traverses up the tree until either a null value or a classloader which has already been added is encountered.
      static java.util.Collection<java.net.URL> findResources​(java.lang.String resource)
      Finds classpath resources.
      (package private) static java.util.Collection<LoaderUtil.UrlResource> findUrlResources​(java.lang.String resource)  
      static java.lang.ClassLoader[] getClassLoaders()  
      static java.lang.ClassLoader getThreadContextClassLoader()
      Gets the current Thread ClassLoader.
      static boolean isClassAvailable​(java.lang.String className)
      Determines if a named Class can be loaded or not.
      private static boolean isIgnoreTccl()  
      static java.lang.Class<?> loadClass​(java.lang.String className)
      Loads a class by name.
      static <T> T newCheckedInstanceOf​(java.lang.String className, java.lang.Class<T> clazz)
      Loads and instantiates a derived class using its default constructor.
      static <T> T newCheckedInstanceOfProperty​(java.lang.String propertyName, java.lang.Class<T> clazz)
      Loads and instantiates a class given by a property name.
      static <T> T newInstanceOf​(java.lang.Class<T> clazz)
      Loads and instantiates a Class using the default constructor.
      static <T> T newInstanceOf​(java.lang.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 java.lang.String IGNORE_TCCL_PROPERTY
        System property to set to ignore the thread context ClassLoader.
        Since:
        2.1
        See Also:
        Constant Field Values
      • SECURITY_MANAGER

        private static final java.lang.SecurityManager SECURITY_MANAGER
      • ignoreTCCL

        private static java.lang.Boolean ignoreTCCL
      • GET_CLASS_LOADER_DISABLED

        private static final boolean GET_CLASS_LOADER_DISABLED
      • TCCL_GETTER

        private static final java.security.PrivilegedAction<java.lang.ClassLoader> TCCL_GETTER
    • Constructor Detail

      • LoaderUtil

        private LoaderUtil()
    • Method Detail

      • getThreadContextClassLoader

        public static java.lang.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.
      • getClassLoaders

        public static java.lang.ClassLoader[] getClassLoaders()
      • accumulateClassLoaders

        private static void accumulateClassLoaders​(java.lang.ClassLoader loader,
                                                   java.util.Collection<java.lang.ClassLoader> loaders)
        Adds the provided loader to the loaders collection, and traverses up the tree until either a null value or a classloader which has already been added is encountered.
      • isClassAvailable

        public static boolean isClassAvailable​(java.lang.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 java.lang.Class<?> loadClass​(java.lang.String className)
                                            throws java.lang.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:
        java.lang.ClassNotFoundException - if the specified class name could not be found
        Since:
        2.1
      • newInstanceOf

        public static <T> T newInstanceOf​(java.lang.Class<T> clazz)
                                   throws java.lang.InstantiationException,
                                          java.lang.IllegalAccessException,
                                          java.lang.reflect.InvocationTargetException
        Loads and instantiates a Class using the default constructor.
        Parameters:
        clazz - The class.
        Returns:
        new instance of the class.
        Throws:
        java.lang.IllegalAccessException - if the class can't be instantiated through a public constructor
        java.lang.InstantiationException - if there was an exception whilst instantiating the class
        java.lang.reflect.InvocationTargetException - if there was an exception whilst constructing the class
        Since:
        2.7
      • newInstanceOf

        public static <T> T newInstanceOf​(java.lang.String className)
                                   throws java.lang.ClassNotFoundException,
                                          java.lang.IllegalAccessException,
                                          java.lang.InstantiationException,
                                          java.lang.NoSuchMethodException,
                                          java.lang.reflect.InvocationTargetException
        Loads and instantiates a Class using the default constructor.
        Parameters:
        className - The class name.
        Returns:
        new instance of the class.
        Throws:
        java.lang.ClassNotFoundException - if the class isn't available to the usual ClassLoaders
        java.lang.IllegalAccessException - if the class can't be instantiated through a public constructor
        java.lang.InstantiationException - if there was an exception whilst instantiating the class
        java.lang.NoSuchMethodException - if there isn't a no-args constructor on the class
        java.lang.reflect.InvocationTargetException - if there was an exception whilst constructing the class
        Since:
        2.1
      • newCheckedInstanceOf

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

        public static <T> T newCheckedInstanceOfProperty​(java.lang.String propertyName,
                                                         java.lang.Class<T> clazz)
                                                  throws java.lang.ClassNotFoundException,
                                                         java.lang.NoSuchMethodException,
                                                         java.lang.reflect.InvocationTargetException,
                                                         java.lang.InstantiationException,
                                                         java.lang.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:
        java.lang.ClassNotFoundException - if the class isn't available to the usual ClassLoaders
        java.lang.IllegalAccessException - if the class can't be instantiated through a public constructor
        java.lang.InstantiationException - if there was an exception whilst instantiating the class
        java.lang.NoSuchMethodException - if there isn't a no-args constructor on the class
        java.lang.reflect.InvocationTargetException - if there was an exception whilst constructing the class
        java.lang.ClassCastException - if the constructed object isn't type compatible with T
        Since:
        2.5
      • isIgnoreTccl

        private static boolean isIgnoreTccl()
      • findResources

        public static java.util.Collection<java.net.URL> findResources​(java.lang.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
      • findUrlResources

        static java.util.Collection<LoaderUtil.UrlResource> findUrlResources​(java.lang.String resource)