Class ClassUtils


  • public final class ClassUtils
    extends java.lang.Object
    Utility class for safe class loading operations.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <T> java.lang.Class<? extends T> forName​(java.lang.String className, java.lang.Class<T> expectedClass, java.lang.ClassLoader classLoader)
      Deprecated.
      use forName(String, Class, ClassLoaderStrategy, ClassLoader), which also lets the caller fall back to the thread context classloader
      static <T> java.lang.Class<? extends T> forName​(java.lang.String className, java.lang.Class<T> expectedClass, ClassLoaderStrategy strategy, java.lang.ClassLoader driverClassLoader)
      Loads a class named by a connection property and validates that it is assignable to the expected type.
      • Methods inherited from class java.lang.Object

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

      • forName

        public static <T> java.lang.Class<? extends T> forName​(java.lang.String className,
                                                               java.lang.Class<T> expectedClass,
                                                               ClassLoaderStrategy strategy,
                                                               java.lang.ClassLoader driverClassLoader)
                                                        throws java.lang.ClassNotFoundException
        Loads a class named by a connection property and validates that it is assignable to the expected type. The class is loaded with initialize=false so that its static initialiser does not run until the type check has passed.

        The classloaders to try, and their order, come from strategy. The first classloader that resolves the name wins; a ClassNotFoundException from one classloader falls through to the next. A class that resolves but is not a subtype of expectedClass fails fast with a ClassCastException rather than falling through.

        Type Parameters:
        T - the expected type
        Parameters:
        className - the name of the class to load
        expectedClass - the expected superclass or interface
        strategy - the order in which to try the classloaders
        driverClassLoader - the driver's own classloader
        Returns:
        the loaded class as a subclass of the expected type
        Throws:
        java.lang.ClassNotFoundException - if none of the classloaders can find the class
      • forName

        @Deprecated
        public static <T> java.lang.Class<? extends T> forName​(java.lang.String className,
                                                               java.lang.Class<T> expectedClass,
                                                               java.lang.ClassLoader classLoader)
                                                        throws java.lang.ClassNotFoundException
        Deprecated.
        use forName(String, Class, ClassLoaderStrategy, ClassLoader), which also lets the caller fall back to the thread context classloader
        Loads a class by name from the given classloader only, validating that it is assignable to the expected type. A null classLoader means the bootstrap classloader, matching Class.forName(String, boolean, ClassLoader).
        Type Parameters:
        T - the expected type
        Parameters:
        className - the name of the class to load
        expectedClass - the expected superclass or interface
        classLoader - the classloader to use
        Returns:
        the loaded class as a subclass of the expected type
        Throws:
        java.lang.ClassNotFoundException - if the class cannot be found