Class LoaderUtil

    • 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

      • getClassLoader

        public static ClassLoader getClassLoader()
        Returns the ClassLoader to use.
        Returns:
        the ClassLoader.
        Since:
        2.22.0
      • getClassLoader

        public static ClassLoader getClassLoader​(Class<?> class1,
                                                 Class<?> class2)
        Since:
        2.22.0
      • getThreadContextClassLoader

        public static ClassLoader getThreadContextClassLoader()
        Looks up the ClassLoader for this current thread. If this class does not have the runtime permission getClassLoader, 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 of AccessController. If none of these strategies can obtain a ClassLoader, then this returns null.
        Returns:
        the current thread's ClassLoader, a fallback loader, or null if no fallback can be determined
      • 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 - 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
      • loadClassUnchecked

        public static Class<?> loadClassUnchecked​(String className)
        Loads and initializes a class given its fully qualified class name. All checked reflective operation exceptions are translated into equivalent LinkageError classes.
        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:
        loadClass(String)
      • newInstanceOfUnchecked

        public static <T> T newInstanceOfUnchecked​(Class<T> clazz)
        Creates an instance of the provided class using the default constructor. All checked reflective operation exceptions are translated into LinkageError or InternalException.
        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
      • newInstanceOfUnchecked

        public static <T> T newInstanceOfUnchecked​(String className)
        Loads and instantiates a class by name using its default constructor. All checked reflective operation exceptions are translated into corresponding LinkageError classes.
        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
      • newInstanceOfUnchecked

        public static <T> T newInstanceOfUnchecked​(String className,
                                                   Class<T> supertype)
        Loads the provided class by name as a checked subtype of the given class. All checked reflective operation exceptions are translated into corresponding LinkageError classes.
        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
      • 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