Class ReflectionUtils

java.lang.Object
org.apache.dubbo.common.utils.ReflectionUtils

public class ReflectionUtils extends Object
A utility class that provides methods for accessing and manipulating private fields and methods of an object. This is useful for white-box testing, where the internal workings of a class need to be tested directly.

Note: Usage of this class should be limited to testing purposes only, as it violates the encapsulation principle.

  • Method Details

    • getField

      public static Object getField(Object source, String fieldName)
      Retrieves the value of the specified field from the given object.
      Parameters:
      source - The object from which to retrieve the field value.
      fieldName - The name of the field to retrieve.
      Returns:
      The value of the specified field in the given object.
      Throws:
      RuntimeException - If the specified field does not exist.
    • invoke

      public static Object invoke(Object source, String methodName, Object... params)
      Invokes the specified method on the given object with the provided parameters.
      Parameters:
      source - The object on which to invoke the method.
      methodName - The name of the method to invoke.
      params - The parameters to pass to the method.
      Returns:
      The result of invoking the specified method on the given object.
    • getClassGenerics

      public static List<Class<?>> getClassGenerics(Class<?> clazz, Class<?> interfaceClass)
      Returns a list of distinct Class objects representing the generics of the given class that implement the given interface.
      Parameters:
      clazz - the class to retrieve the generics for
      interfaceClass - the interface to retrieve the generics for
      Returns:
      a list of distinct Class objects representing the generics of the given class that implement the given interface
    • match

      public static boolean match(Class<?> clazz, Class<?> interfaceClass, Object event)