Class ClassUtils


  • public final class ClassUtils
    extends java.lang.Object
    Operations on classes using reflection.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void assertInterfaceWithOnlyVoidMethods​(java.lang.Class<?> clazz)
      Asserts that the specified class is an interface which has only methods with no return value.
      static <T,​C>
      T
      create​(java.lang.Class<T> superClazz, java.lang.Class<C> clazz, java.lang.Object... arguments)
      Creates a new instance of a class specified by its fully-qualified name.
      static <T> T create​(java.lang.Class<T> superClazz, java.lang.String className, java.lang.Object... arguments)
      Creates a new instance of a class specified by its fully-qualified name.
      static <T> T[] createArray​(java.lang.Class<T> clazz, int len)
      Creates an array of given Class type and of given len.
      static <T> T createInstance​(java.lang.Class<T> clazz)
      Creates a new instance of given clazz and get rid of any checked exception.
      static java.util.Collection<java.lang.Class<?>> gatherAllCastableClassesAndInterfacesFor​(java.lang.Object object)
      Gathers all classes and interfaces the specified object can be casted to.
      static <C> boolean hasConstructor​(java.lang.Class<C> clazz, java.lang.Object... arguments)
      Returns true, if the clazz has a constructor with the given arguments.
      static java.lang.Object invokeMethod​(java.lang.reflect.Method method, java.lang.Object obj, java.lang.Object... args)
      Invokes given method and get rid of any checked exception.
      static java.util.List<java.lang.Class<?>> listClasses​(java.lang.String packageName, IClassFilter classFilterOrNull)
      Lists all the classes in the given packageName that passes the given IClassFilter.
      static boolean setFieldValue​(java.lang.Object object, java.lang.String fieldName, java.lang.Object newValue)
      Sets declared field named fieldName of given object to given new value newValue.
      static java.lang.reflect.Field tryGetDeclaredField​(java.lang.Class<?> c, java.lang.String fieldName)
      Gets declared field named fieldName in given class or superclass of it.
      • Methods inherited from class java.lang.Object

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

      • gatherAllCastableClassesAndInterfacesFor

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

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

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

        public static final <T,​C> T create​(java.lang.Class<T> superClazz,
                                                 java.lang.Class<C> clazz,
                                                 java.lang.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​(java.lang.Class<T> superClazz,
                                         java.lang.String className,
                                         java.lang.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​(java.lang.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 java.lang.Object invokeMethod​(java.lang.reflect.Method method,
                                                          java.lang.Object obj,
                                                          java.lang.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​(java.lang.Object object,
                                                  java.lang.String fieldName,
                                                  java.lang.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 java.lang.reflect.Field tryGetDeclaredField​(java.lang.Class<?> c,
                                                                        java.lang.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.
      • createArray

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

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

        This method does not work recursively.