Class Reflections

java.lang.Object
org.silverpeas.kernel.test.util.Reflections

public final class Reflections extends Object
Common reflection utility operations for testing purpose.
Author:
mmoquillon
  • Method Details

    • loopInheritance

      public static void loopInheritance(@NonNull Class<?> fromType, @NonNull Reflections.TypeConsumer consumer)
      Browses the inheritance graph of the specified type up to his root parent and applies for each parent type the specified type consumer.
      Parameters:
      fromType - the type from which the inheritance graph is browsed
      consumer - a function to apply to each type in the inheritance graph
    • getDeclaredAnnotations

      @NonNull public static Annotation[] getDeclaredAnnotations(@NonNull AnnotatedElement element, @NonNull Class<? extends Annotation> annotationType)
      Gets all the annotations declared on the specified element and that satisfies the given annotation type.
      Parameters:
      element - the annotated element.
      annotationType - the type of annotation the annotations to return have to satisfy.
      Returns:
      an array with the annotations satisfying the given type.
    • instantiate

      @NonNull public static <T> T instantiate(@NonNull Class<T> clazz) throws SilverpeasReflectionException
      Constructs an instance of the specified concrete class by using its default non-arguments constructor. The constructor can be private, package-private, protected or public.
      Type Parameters:
      T - the type of the object to construct.
      Parameters:
      clazz - the class to instantiate.
      Returns:
      the new instance of the given class.
      Throws:
      SilverpeasReflectionException - if an error occurs while instantiating the specified class.
    • setStaticField

      public static void setStaticField(@NonNull Class<?> type, @NonNull Field field, @Nullable Object value) throws SilverpeasReflectionException
      Sets the specified value to the given static field of the specified class. The field can be also final. This method isn't recommended to be used as it bypasses field accessibility of classes and as such it breaks the encapsulation principle. This method should be used only for specific testing purpose. When this method is required in tests, this means either the design of the related class is broken and hence it should be refactored or the test isn't correctly written or prepared and hence requires a rewriting.
      Parameters:
      type - the type having the field to set with the given value.
      field - the field of the class to set with the given value.
      value - the value to set.
      Throws:
      SilverpeasReflectionException - if an error occurs while setting the field of the object with the value.
    • setStaticField

      public static void setStaticField(@NonNull Class<?> type, @NonNull String fieldName, @Nullable Object value) throws SilverpeasReflectionException
      Sets the specified value to the static field with the specified name in the given class. The field can be also final. This method isn't recommended to be used as it bypasses field accessibility of classes and as such it breaks the encapsulation principle. This method should be used only for specific testing purpose. When this method is required in tests, this means either the design of the related class is broken and hence it should be refactored or the test isn't correctly written or prepared and hence requires a rewriting.
      Parameters:
      type - the type having the field to set with the given value.
      fieldName - the name of the field of the class to set with the given value.
      value - the value to set.
      Throws:
      SilverpeasReflectionException - if an error occurs while setting the field of the object with the value.
    • setField

      public static void setField(@NonNull Object object, @NonNull Field field, @Nullable Object value) throws SilverpeasReflectionException
      Sets the specified value to the given field of the specified object. The object can be either an instance of a class or a class itself. Latter means the field is a static one.
      Parameters:
      object - the object having the field to set with the given value. Can be either an instance of a class or a class itself (in this case the field must be a static one).
      field - the field of the object to set with the given value.
      value - the value to set.
      Throws:
      SilverpeasReflectionException - if an error occurs while setting the field of the object with the value.
    • setField

      public static void setField(@NonNull Object object, @NonNull String fieldName, @Nullable Object value) throws SilverpeasReflectionException
      Sets the specified value to the given field of the specified object. The object can be either an instance of a class or a class itself. Latter means the field is a static one.
      Parameters:
      object - the object having the field to set with the given value. Can be either an instance of a class or a class itself (in this case the field must be a static one).
      fieldName - the name of the field of the object to set with the given value.
      value - the value to set.
      Throws:
      SilverpeasReflectionException - if an error occurs while setting the field of the object with the value.
    • setField

      public static void setField(@NonNull Class<?> type, @NonNull Object object, @NonNull Field field, @Nullable Object value) throws SilverpeasReflectionException
      Sets the specified value to the given field of the specified object inherited from the given type of the object.
      Parameters:
      type - a type the object satisfies and that is extended by its class.
      object - the object having the field to set with the given value.
      field - the field of the object to set with the given value.
      value - the value to set.
      Throws:
      SilverpeasReflectionException - if an error occurs while setting the field of the object with the value.