Class ClassUtils

java.lang.Object
ch.systemsx.cisd.common.reflection.ClassUtils

public final class ClassUtils extends Object
Operations on classes using reflection.
  • Method Details

    • gatherAllCastableClassesAndInterfacesFor

      public static final Collection<Class<?>> gatherAllCastableClassesAndInterfacesFor(Object object)
      Gathers all classes and interfaces the specified object can be casted to.
    • assertInterfaceWithOnlyVoidMethods

      public static void assertInterfaceWithOnlyVoidMethods(Class<?> clazz)
      Asserts that the specified class is an interface which has only methods with no return value.
      Throws:
      AssertionError - if it isn't an interface or at least one method has a return value.
    • hasConstructor

      public static final <C> boolean hasConstructor(Class<C> clazz, Object... arguments)
      Returns true, if the clazz has a constructor with the given arguments.
    • create

      public static final <T, C> T create(Class<T> superClazz, Class<C> clazz, Object... arguments)
      Creates a new instance of a class specified by its fully-qualified name.
      Parameters:
      superClazz - Super class className has to be implemented or extended.
      clazz - Fully-qualified class.
      arguments - Optional constructor arguments. If (Object[]) is an empty array, then the default constructor will be used.
      Returns:
      an instance of type interface.
    • create

      public static final <T> T create(Class<T> superClazz, String className, Object... arguments)
      Creates a new instance of a class specified by its fully-qualified name.
      Parameters:
      superClazz - Super class className has to be implemented or extended.
      className - Fully-qualified class name.
      arguments - Optional constructor arguments. If (Object[]) null then the empty constructor will be used. Note that (Object) null is not interpreted as null arguments but rather as new Object[]{null}.
      Returns:
      an instance of type interface.
    • createInstance

      public static final <T> T createInstance(Class<T> clazz)
      Creates a new instance of given clazz and get rid of any checked exception.

      Wraps any checked exception in a CheckedExceptionTunnel.

    • invokeMethod

      public static final Object invokeMethod(Method method, Object obj, Object... args)
      Invokes given method and get rid of any checked exception.

      Wraps any checked exception in a CheckedExceptionTunnel.

    • setFieldValue

      public static final boolean setFieldValue(Object object, String fieldName, Object newValue)
      Sets declared field named fieldName of given object to given new value newValue.

      This is useful when you want to set a private field on which you do not have access. Note that this method should only be used in very special cases. You should consider it as a hack.

      Returns:
      a true if fieldName has been modified.
    • tryGetDeclaredField

      public static final Field tryGetDeclaredField(Class<?> c, String fieldName)
      Gets declared field named fieldName in given class or superclass of it.

      Before returning it, it call Field.setAccessible(boolean) with true.

      Returns:
      null if given fieldName could not be found or it couldn't be made accessible.
    • createArray

      public static final <T> T[] createArray(Class<T> clazz, int len)
      Creates an array of given Class type and of given len.
    • listClasses

      public static final List<Class<?>> listClasses(String packageName, IClassFilter classFilterOrNull)
      Lists all the classes in the given packageName that passes the given IClassFilter.

      This method does not work recursively.