Class ReflectionHelper

java.lang.Object
org.citrusframework.util.ReflectionHelper

public class ReflectionHelper extends Object
Helper for working with reflection on classes.

This code is based on org.apache.camel.util.ReflectionHelper class.

  • Method Details

    • doWithClasses

      public static void doWithClasses(Class<?> clazz, ReflectionHelper.ClassCallback cc) throws IllegalArgumentException
      Perform the given callback operation on the nested (inner) classes.
      Parameters:
      clazz - class to start looking at
      cc - the callback to invoke for each inner class (excluding the class itself)
      Throws:
      IllegalArgumentException
    • doWithFields

      public static void doWithFields(Class<?> clazz, ReflectionHelper.FieldCallback fc) throws IllegalArgumentException
      Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.
      Parameters:
      clazz - the target class to analyze
      fc - the callback to invoke for each field
      Throws:
      IllegalArgumentException
    • doWithMethods

      public static void doWithMethods(Class<?> clazz, ReflectionHelper.MethodCallback mc) throws IllegalArgumentException
      Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).

      Important: This method does not take the bridge methods into account.

      Parameters:
      clazz - class to start looking at
      mc - the callback to invoke for each method
      Throws:
      IllegalArgumentException
    • findField

      public static Field findField(Class<?> clazz, String name)
      Attempt to find a field on the supplied Class with the supplied name and/or type. Searches all superclasses up to Object.
      Parameters:
      clazz - the class to introspect
      name - the name of the field (may be null if type is specified)
      Returns:
      the corresponding Field object, or null if not found
    • findMethod

      public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes)
      Attempt to find a Method on the supplied class with the supplied name and parameter types. Searches all superclasses up to Object.

      Returns null if no Method can be found.

      Parameters:
      clazz - the class to introspect
      name - the name of the method
      paramTypes - the parameter types of the method (may be null to indicate any signature)
      Returns:
      the Method object, or null if none found
    • invokeMethod

      public static Object invokeMethod(Method method, Object target, Object... args)
      Invoke the specified Method against the supplied target object with the supplied arguments. The target object can be null when invoking a static Method.
      Parameters:
      method - the method to invoke
      target - the target object to invoke the method on
      args - the invocation arguments (maybe null)
      Returns:
      the invocation result, if any
    • setField

      public static void setField(Field f, Object instance, Object value)
    • getField

      public static Object getField(Field f, Object instance)