Class ClassUtils

java.lang.Object
org.postgresql.util.ClassUtils

public final class ClassUtils extends Object
Utility class for safe class loading operations.
  • Method Details

    • forName

      public static <T> Class<? extends T> forName(String className, Class<T> expectedClass, ClassLoaderStrategy strategy, ClassLoader driverClassLoader) throws 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:
      ClassNotFoundException - if none of the classloaders can find the class
    • forName

      @Deprecated public static <T> Class<? extends T> forName(String className, Class<T> expectedClass, ClassLoader classLoader) throws 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:
      ClassNotFoundException - if the class cannot be found