Class AnnotatedField<A extends java.lang.annotation.Annotation,​T>

  • Type Parameters:
    A - annotation type
    T - value type

    public class AnnotatedField<A extends java.lang.annotation.Annotation,​T>
    extends java.lang.Object
    Annotated field wrapper. Used to simplify work with test fields by hiding all required reflection.
    Since:
    07.02.2025
    • Constructor Summary

      Constructors 
      Constructor Description
      AnnotatedField​(A annotation, java.lang.reflect.Field field, java.lang.Class<?> testClass)
      Create annotated field for exact field and annotation.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T checkValueNotChanged​(org.junit.jupiter.api.extension.TestInstances instance)
      Validates that field value not changed (it is the same value as was injected).
      void clearCustomData()
      Clear custom state.
      java.lang.Object findRequiredInstance​(org.junit.jupiter.api.extension.TestInstances instances)
      In case of nested tests, test instances would contain multiple test instances.
      A getAnnotation()  
      java.lang.Object getCachedValue()
      Use this method to avoid duplicate value readings when you're SURE that value was already resolved.
      <K> K getCustomData​(java.lang.String key)
      Get custom value.
      java.lang.Class<?> getDeclaringClass()  
      java.lang.reflect.Field getField()  
      java.lang.String getName()  
      java.lang.Class<T> getType()  
      java.util.List<java.lang.Class<?>> getTypeParameters()
      For example if the field type is: RootType<Param1, Param2>.
      T getValue​(java.lang.Object instance)  
      T getValue​(org.junit.jupiter.api.extension.TestInstances instances)
      In case of nested test, there would be root class instance and nested instance.
      boolean isCompatible​(java.lang.Object instance)
      Required to prevent incorrect usage (field resolution with a wrong instance).
      boolean isCustomDataSet​(java.lang.String key)
      Note: if key set with null value - it would be considered as false.
      boolean isStatic()  
      boolean isTestOwnField()  
      void requireNonStatic()
      Validation option: throw error if field is static.
      void requireStatic()
      Validation option: throw error if field is not static.
      void setCustomData​(java.lang.String key, java.lang.Object value)
      Apply custom value for the field object.
      void setValue​(java.lang.Object instance, T value)  
      void setValue​(org.junit.jupiter.api.extension.TestInstances instances, T value)
      In case of nested test, there would be root class instance and nested instance.
      java.lang.String toString()  
      java.lang.String toStringField()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • AnnotatedField

        public AnnotatedField​(A annotation,
                              java.lang.reflect.Field field,
                              java.lang.Class<?> testClass)
        Create annotated field for exact field and annotation.
        Parameters:
        annotation - field annotation
        field - annotated field
        testClass - test class where field was searcher
    • Method Detail

      • getAnnotation

        public A getAnnotation()
        Returns:
        field annotation instance (type of annotation defined by initial fields search)
      • getType

        public java.lang.Class<T> getType()
        Returns:
        field class type
      • getTypeParameters

        public java.util.List<java.lang.Class<?>> getTypeParameters()
        For example if the field type is: RootType<Param1, Param2>. Then the method would return [Param1, Param2].

        Implementation does not expect not resolved variables (simple case).

        Returns:
        type arguments
      • getDeclaringClass

        public java.lang.Class<?> getDeclaringClass()
        Returns:
        class that declares field
      • getName

        public java.lang.String getName()
        Returns:
        field name
      • getField

        public java.lang.reflect.Field getField()
        Returns:
        field instance
      • getValue

        public T getValue​(java.lang.Object instance)
        Parameters:
        instance - test instance (or null for static field)
        Returns:
        test field value
        Throws:
        java.lang.IllegalStateException - if non-static field resolved with null instance or other error appear
      • getValue

        public T getValue​(org.junit.jupiter.api.extension.TestInstances instances)
        In case of nested test, there would be root class instance and nested instance. It is important to select the correct instance for field manipulation: the correct test instance would be selected by preserved test class.
        Parameters:
        instances - test instances
        Returns:
        field value
      • setValue

        public void setValue​(java.lang.Object instance,
                             T value)
        Parameters:
        instance - test instance (or null for static field)
        value - field value
        Throws:
        java.lang.IllegalStateException - if non-static field set with null instance or other error appear
      • setValue

        public void setValue​(org.junit.jupiter.api.extension.TestInstances instances,
                             T value)
        In case of nested test, there would be root class instance and nested instance. It is important to select the correct instance for field manipulation: the correct test instance would be selected by preserved test class.
        Parameters:
        instances - test instances
        value - value to set
      • isStatic

        public boolean isStatic()
        Returns:
        true if field is static
      • isTestOwnField

        public boolean isTestOwnField()
        Returns:
        true if field declared directly in test class (not in base class)
      • requireStatic

        public void requireStatic()
        Validation option: throw error if field is not static.
      • requireNonStatic

        public void requireNonStatic()
        Validation option: throw error if field is static.
      • getCachedValue

        public java.lang.Object getCachedValue()
        Use this method to avoid duplicate value readings when you're SURE that value was already resolved.
        Returns:
        last obtained or set value
      • isCompatible

        public boolean isCompatible​(java.lang.Object instance)
        Required to prevent incorrect usage (field resolution with a wrong instance).
        Parameters:
        instance - instance to check
        Returns:
        true if the provided instance is a field class instance, false otherwise
      • findRequiredInstance

        public java.lang.Object findRequiredInstance​(org.junit.jupiter.api.extension.TestInstances instances)
        In case of nested tests, test instances would contain multiple test instances. It is important to select the correct one (using preserved original test class).
        Parameters:
        instances - test instances
        Returns:
        test instance or null
        Throws:
        java.lang.IllegalStateException - if a test instances object provided but does not contain the required test instance
      • setCustomData

        public void setCustomData​(java.lang.String key,
                                  java.lang.Object value)
        Apply custom value for the field object. Useful during field processing to mark is as processed or assign an additional state.
        Parameters:
        key - key
        value - value
      • getCustomData

        public <K> K getCustomData​(java.lang.String key)
        Get custom value.
        Type Parameters:
        K - value type
        Parameters:
        key - key
        Returns:
        value or null
      • isCustomDataSet

        public boolean isCustomDataSet​(java.lang.String key)
        Note: if key set with null value - it would be considered as false.
        Parameters:
        key - key
        Returns:
        true if non null custom value set
      • clearCustomData

        public void clearCustomData()
        Clear custom state.
      • toStringField

        public java.lang.String toStringField()
        Returns:
        to string field with class and reduced package
      • checkValueNotChanged

        public T checkValueNotChanged​(org.junit.jupiter.api.extension.TestInstances instance)
        Validates that field value not changed (it is the same value as was injected).
        Parameters:
        instance - test instance
        Returns:
        value
        Throws:
        java.lang.IllegalStateException - if field value differs from the injected value
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object