Class ClassUtils

java.lang.Object
com.aspectran.utils.ClassUtils

public abstract class ClassUtils extends Object
Miscellaneous class utility methods.
  • Field Details

    • PACKAGE_SEPARATOR_CHAR

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

      public static final String CLASS_FILE_SUFFIX
      The ".class" file suffix
      See Also:
  • Constructor Details

    • ClassUtils

      public ClassUtils()
  • Method Details

    • createInstance

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

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

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

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

      public static boolean isVisible(Class<?> clazz, 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
    • isLambdaClass

      public static boolean isLambdaClass(@NonNull Class<?> clazz)
      Determine if the supplied Class is a JVM-generated implementation class for a lambda expression or method reference.

      This method makes a best-effort attempt at determining this, based on checks that work on modern, mainstream JVMs.

      Parameters:
      clazz - the class to check
      Returns:
      true if the class is a lambda implementation class
    • classForName

      @NonNull public static Class<?> classForName(String name) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • classForName

      @NonNull public static Class<?> classForName(String name, ClassLoader classLoader) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • loadClass

      public static Class<?> loadClass(String name) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • getDefaultClassLoader

      public static 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 ClassLoader overrideThreadContextClassLoader(@Nullable 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 ClassLoader classLoader)