Class ClassUtils


  • public abstract class ClassUtils
    extends java.lang.Object
    Miscellaneous class utility methods.
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T createInstance​(java.lang.Class<T> cls)
      Method that can be called to try to create an instantiate of specified type.
      static <T> T createInstance​(java.lang.Class<T> cls, java.lang.Object... args)
      Method that can be called to try to create an instantiate of specified type.
      static <T> T createInstance​(java.lang.Class<T> cls, java.lang.Object[] args, java.lang.Class<?>[] argTypes)
      Method that can be called to try to create an instantiate of specified type.
      static <T> java.lang.reflect.Constructor<T> findConstructor​(java.lang.Class<T> cls, java.lang.Class<?>... argTypes)
      Obtain an accessible constructor for the given class and parameters.
      static java.lang.ClassLoader getDefaultClassLoader()
      Returns the default class loader within the current context.
      static boolean isVisible​(java.lang.Class<?> clazz, java.lang.ClassLoader classLoader)
      Check whether the given class is visible in the given ClassLoader.
      static java.lang.ClassLoader overrideThreadContextClassLoader​(java.lang.ClassLoader classLoaderToUse)
      Override the thread context ClassLoader with the environment's bean ClassLoader if necessary, i.e.
      static void restoreThreadContextClassLoader​(java.lang.ClassLoader classLoader)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PACKAGE_SEPARATOR_CHAR

        public static final char PACKAGE_SEPARATOR_CHAR
        The package separator character '.'
        See Also:
        Constant Field Values
      • CLASS_FILE_SUFFIX

        public static final java.lang.String CLASS_FILE_SUFFIX
        The ".class" file suffix
        See Also:
        Constant Field Values
    • Constructor Detail

      • ClassUtils

        public ClassUtils()
    • Method Detail

      • createInstance

        public static <T> T createInstance​(java.lang.Class<T> cls)
        Method that can be called to try to create an instantiate of specified type. Instantiation is done using default no-argument constructor.
        Type Parameters:
        T - the generic type
        Parameters:
        cls - the class to check
        Returns:
        an instantiated object
        Throws:
        java.lang.IllegalArgumentException - if instantiation fails for any reason; except for cases where constructor throws an unchecked exception (which will be passed as is)
      • createInstance

        public static <T> T createInstance​(java.lang.Class<T> cls,
                                           java.lang.Object... args)
        Method that can be called to try to create an instantiate of specified type.
        Type Parameters:
        T - the generic type
        Parameters:
        cls - the class to check
        args - the arguments
        Returns:
        an instantiated object
        Throws:
        java.lang.IllegalArgumentException - if instantiation fails for any reason; except for cases where constructor throws an unchecked exception (which will be passed as is)
      • createInstance

        public static <T> T createInstance​(java.lang.Class<T> cls,
                                           java.lang.Object[] args,
                                           java.lang.Class<?>[] argTypes)
        Method that can be called to try to create an instantiate of specified type.
        Type Parameters:
        T - the generic type
        Parameters:
        cls - the class to check
        args - the arguments
        argTypes - the argument types of the desired constructor
        Returns:
        an instantiated object
        Throws:
        java.lang.IllegalArgumentException - if instantiation fails for any reason; except for cases where constructor throws an unchecked exception (which will be passed as is)
      • findConstructor

        public static <T> java.lang.reflect.Constructor<T> findConstructor​(java.lang.Class<T> cls,
                                                                           java.lang.Class<?>... argTypes)
                                                                    throws java.lang.NoSuchMethodException
        Obtain an accessible constructor for the given class and parameters.
        Type Parameters:
        T - the generic type
        Parameters:
        cls - the class to check
        argTypes - the argument types of the desired constructor
        Returns:
        the constructor reference
        Throws:
        java.lang.NoSuchMethodException - if no such constructor exists
      • isVisible

        public static boolean isVisible​(java.lang.Class<?> clazz,
                                        java.lang.ClassLoader classLoader)
        Check whether the given class is visible in the given ClassLoader.
        Parameters:
        clazz - the class to check (typically an interface)
        classLoader - the ClassLoader to check against (may be null in which case this method will always return true)
        Returns:
        true if the given class is visible; otherwise false
        Since:
        6.0.0
      • getDefaultClassLoader

        public static java.lang.ClassLoader getDefaultClassLoader()
        Returns the default class loader within the current context. If there is a context classloader it is returned, otherwise the classloader which loaded the ClassUtils Class is returned.
        Returns:
        the appropriate default classloader which is guaranteed to be non-null
      • overrideThreadContextClassLoader

        @Nullable
        public static java.lang.ClassLoader overrideThreadContextClassLoader​(@Nullable
                                                                             java.lang.ClassLoader classLoaderToUse)
        Override the thread context ClassLoader with the environment's bean ClassLoader if necessary, i.e. if the bean ClassLoader is not equivalent to the thread context ClassLoader already.
        Parameters:
        classLoaderToUse - the actual ClassLoader to use for the thread context
        Returns:
        the original thread context ClassLoader, or null if not overridden
      • restoreThreadContextClassLoader

        public static void restoreThreadContextClassLoader​(@Nullable
                                                           java.lang.ClassLoader classLoader)