Class ClassUtil

java.lang.Object
net.sf.basedb.util.ClassUtil

public class ClassUtil extends Object
Utility class for Class objects.
Version:
2.0
Author:
Nicklas
Last modified
$Date: 2019-06-14 14:54:55 +0200 (fre, 14 juni 2019) $
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <C> Class<? extends C>
    checkAndLoadClass(ClassLoader loader, String name, boolean publicNoArgConstructor, Class<C> iface, Class<?>... interfaces)
    Check if a specified class exists and, optionally, if it has public no-argument constructor and implements a set of specific interfaces or superclasses.
    static void
    checkClass(Class<?> clazz, boolean publicNoArgConstructor, Class<?>... interfaces)
    Check if a specified class has public no-argument constructor and implements a set of specific interfaces or superclasses.
    static <I> Constructor<I>
    findConstructor(Class<I> clazz, Object[] parameters)
    Find a constructor for the specified class which takes parameters of the specified types taking superclasses and interfaces into account.
    static Method
    findMethod(Class<?> clazz, String name, Class<?>... arguments)
    Find a method in a clazz with the given name that takes the given list of arguments.
    static Set<Class<?>>
    getInterfaces(Class<?> clazz)
    Get all interfaces a class implements, including those implemented by superclasses.
    static boolean
    isAssignable(Class<?>[] classes, Object[] values)
    Check if one array of class objects is assignment compatible with values in another array.
    static <I> I
    newInstance(Class<I> clazz)
    Utility method for creating a new instance from a class with a default no-argument constructor.
    static <I> I
    newInstance(ClassLoader classLoader, String name, Class<I> iface, Class<?>... interfaces)
    Utility method for creating a new instance from a class (given by name) with a default no-argument constructor.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ClassUtil

      public ClassUtil()
  • Method Details

    • newInstance

      public static <I> I newInstance(Class<I> clazz)
      Utility method for creating a new instance from a class with a default no-argument constructor. Exceptions are wrapped in BaseExceptions.
      Since:
      3.14
    • newInstance

      public static <I> I newInstance(ClassLoader classLoader, String name, Class<I> iface, Class<?>... interfaces)
      Utility method for creating a new instance from a class (given by name) with a default no-argument constructor. Exceptions are wrapped in BaseExceptions.
      Since:
      3.16
      See Also:
    • findConstructor

      public static <I> Constructor<I> findConstructor(Class<I> clazz, Object[] parameters)
      Find a constructor for the specified class which takes parameters of the specified types taking superclasses and interfaces into account. This method finds the first declared constructor which can accept parameters of the specified type.
      Parameters:
      clazz - The class to find a constructor for
      parameters - The parameters that are going to be sent to the constructor's Constructor.newInstance(Object[]) method
      Returns:
      A constructor object or null if no constructor was found
    • isAssignable

      public static boolean isAssignable(Class<?>[] classes, Object[] values)
      Check if one array of class objects is assignment compatible with values in another array. Ie. the Class.isAssignableFrom(Class) method returns true for each element in the classes array with respect to the class of the corresponding value in the values array. Null elements in the values array are ignored.
      Parameters:
      classes - The class objects representing the parameters we want to assign values to
      values - The parameter values to assign
      Returns:
      TRUE if all parameters are assignable
    • getInterfaces

      public static Set<Class<?>> getInterfaces(Class<?> clazz)
      Get all interfaces a class implements, including those implemented by superclasses.
      Parameters:
      clazz - The Class to check
      Returns:
      A Set containing the Class objects for the interfaces
    • checkAndLoadClass

      public static <C> Class<? extends C> checkAndLoadClass(ClassLoader loader, String name, boolean publicNoArgConstructor, Class<C> iface, Class<?>... interfaces) throws ClassNotFoundException, NoSuchMethodException, ClassCastException
      Check if a specified class exists and, optionally, if it has public no-argument constructor and implements a set of specific interfaces or superclasses.
      Parameters:
      loader - The classloader used to load the class, or null to use the default classloader
      name - The fully qualified name of class to check
      publicNoArgConstructor - TRUE to check if a public no-argument constructor exists
      interfaces - An array of class objects representing interfaces or superclasses that the class to check must implement or extend
      Returns:
      The Class object for the named class
      Throws:
      ClassNotFoundException - If a class with the specified name isn't found
      NoSuchMethodException - If a public no-argument constructor isn't found
      ClassCastException - If the class doesn't implement or extend all of the interfaces/superclasses
      Since:
      2.5
    • checkClass

      public static void checkClass(Class<?> clazz, boolean publicNoArgConstructor, Class<?>... interfaces) throws NoSuchMethodException, ClassCastException
      Check if a specified class has public no-argument constructor and implements a set of specific interfaces or superclasses.
      Parameters:
      clazz - The clazz to check
      publicNoArgConstructor - TRUE to check if a public no-argument constructor exists
      interfaces - An array of class objects representing interfaces or superclasses that the class to check must implement or extend
      Throws:
      NoSuchMethodException - If a public no-argument constructor isn't found
      ClassCastException - If the class doesn't implement or extend all of the interfaces/superclasses
      Since:
      2.10
    • findMethod

      public static Method findMethod(Class<?> clazz, String name, Class<?>... arguments)
      Find a method in a clazz with the given name that takes the given list of arguments. This method is just calling Class.getMethod(String, Class...) but returns null instead of throwing an exception in case the method is not found.
      Parameters:
      clazz - The class to look for the method in
      name - The name of the method
      arguments - The class types of the arguments
      Returns:
      The method or null if not found
      Since:
      2.7