Class ClassUtil

java.lang.Object
io.vertigo.core.util.ClassUtil

public final class ClassUtil extends Object
The ClassUtil class provides methods to determine the structure of a class or to create instances.
Author:
pchretien
  • Method Details

    • newInstance

      public static Object newInstance(String javaClassName)
      Creates a new untyped instance via class name (empty constructor). Please favor methods returning a typed instance as soon as the type is known.
      Parameters:
      javaClassName - Class name
      Returns:
      New instance
    • newInstance

      public static <J> J newInstance(String javaClassName, Class<J> type)
      Creates a new typed instance via class name (empty constructor).
      Type Parameters:
      J - Type of the returned instance
      Parameters:
      javaClassName - Class name
      type - Returned type
      Returns:
      New instance
    • newInstance

      public static <J> J newInstance(Class<J> clazz)
      Creates a new typed instance via a class (empty constructor).
      Type Parameters:
      J - Type of the returned instance
      Parameters:
      clazz - Class
      Returns:
      New instance
    • newInstance

      public static <J> J newInstance(Constructor<J> constructor, Object[] args)
      Creates a new typed instance via a constructor and its arguments.
      Type Parameters:
      J - Type of the returned instance
      Parameters:
      constructor - Constructor
      args - Construction arguments
      Returns:
      New instance
    • findConstructor

      public static <J> Constructor<J> findConstructor(Class<J> clazz, Class<?>[] parameterTypes)
      Retrieves the constructor corresponding to the given signature.
      Type Parameters:
      J - Class type
      Parameters:
      clazz - Class to search for the constructor
      parameterTypes - Signature of the searched constructor
      Returns:
      Searched constructor
    • classForName

      public static Class<?> classForName(String javaClassName)
      Retrieves an untyped class from its name.
      Parameters:
      javaClassName - Class name
      Returns:
      Java class
    • classForName

      public static <J> Class<? extends J> classForName(String javaClassName, Class<J> type)
      Retrieves a typed class from its name.
      Type Parameters:
      J - Type of the returned instance
      Parameters:
      javaClassName - Class name
      type - Type.
      Returns:
      Java class
    • invoke

      public static Object invoke(Object instance, Method method, Object... args)
      Dynamic invocation of a method on a specific instance.
      Parameters:
      instance - Object
      method - method which is invoked
      args - Args
      Returns:
      value provided as the result by the method
    • set

      public static void set(Object instance, Field field, Object value)
      Dynamic assignment of a field's value (even private).
      Parameters:
      instance - Object on which the method is invoked
      field - Field concerned
      value - New value
    • get

      public static Object get(Object instance, Field field)
      Dynamic retrieval of a field's value.
      Parameters:
      instance - Object on which the method is invoked
      field - Concerned field
      Returns:
      Value
    • findMethod

      public static Method findMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
      Retrieves the method corresponding to the name and signature among the passed methods.
      Parameters:
      clazz - Class on which we are searching for methods
      methodName - Name of the searched method
      parameterTypes - Signature of the searched method
      Returns:
      Searched method
    • getAllFields

      public static Collection<Field> getAllFields(Class<?> clazz, Class<? extends Annotation> annotation)
      Returns all declared fields (including parent fields) annotated for a given class.
      Parameters:
      clazz - Class
      annotation - Expected annotation
      Returns:
      All declared fields (including parent fields)
    • getAllMethods

      public static Collection<Method> getAllMethods(Class<?> clazz, Class<? extends Annotation> annotation)
      Returns all declared methods annotated by the given annotation.
      Parameters:
      clazz - Class
      annotation - Expected annotation
      Returns:
      All declared fields (including parent fields)
    • getAllFields

      public static Collection<Field> getAllFields(Class<?> clazz)
      Returns all declared fields (including parent fields) for a given class.
      Parameters:
      clazz - Class
      Returns:
      All declared fields (including parent fields)
    • getAllMethods

      public static Collection<Method> getAllMethods(Class<?> clazz)
      Returns all declared methods for a given class (including parent methods).
      Parameters:
      clazz - Class
      Returns:
      All declared methods (including parent methods)
    • getAllInterfaces

      public static Set<Class<?>> getAllInterfaces(Class<?> clazz)
      Returns all interfaces (including those of the parents) for a given class.
      Parameters:
      clazz - Class
      Returns:
      All implemented interfaces
    • getGeneric

      public static Class<?> getGeneric(Constructor<?> constructor, int i)
      Retrieves the generic type of a parameterized field. It is expected that there is ONE and only ONE declared generic. Example: List => Car Option => Car
      Parameters:
      constructor - Constructor
      i - Index of the parameter in the component
      Returns:
      Class of the generic type
    • getGeneric

      public static Class<?> getGeneric(Method method, int i)
      Retrieves the generic type of a parameterized method. It is expected that there is ONE and only ONE declared generic. Example: List => Car Option => Car
      Parameters:
      method - Method
      i - Index of the parameter in the component
      Returns:
      Class of the generic type
    • getGeneric

      public static Class<?> getGeneric(Field field)
      Retrieves the generic type of a parameterized field. It is expected that there is ONE and only ONE declared generic. Example: List => Car Option => Car
      Parameters:
      field - Field
      Returns:
      Class of the generic type