Class InstanceField

java.lang.Object
org.mockito.internal.util.reflection.InstanceField

public class InstanceField extends Object
Represents an accessible instance field. Contains the instance reference on which the field can be read and write.
  • Field Details

    • field

      private final Field field
    • instance

      private final Object instance
    • fieldReader

      private FieldReader fieldReader
  • Constructor Details

    • InstanceField

      public InstanceField(Field field, Object instance)
      Create a new InstanceField.
      Parameters:
      field - The field that should be accessed, note that no checks are performed to ensure the field belong to this instance class.
      instance - The instance from which the field shall be accessed.
  • Method Details

    • read

      public Object read()
      Safely read the field.
      Returns:
      the field value.
      See Also:
    • set

      public void set(Object value)
      Set the given value to the field of this instance.
      Parameters:
      value - The value that should be written to the field.
    • isNull

      public boolean isNull()
      Check that the field is not null.
      Returns:
      true if null, else false.
    • isAnnotatedBy

      public boolean isAnnotatedBy(Class<? extends Annotation> annotationClass)
      Check if the field is annotated by the given annotation.
      Parameters:
      annotationClass - The annotation type to check.
      Returns:
      true if the field is annotated by this annotation, else false.
    • isSynthetic

      public boolean isSynthetic()
      Check if the field is synthetic.
      Returns:
      true if the field is synthetic, else false.
    • annotation

      public <A extends Annotation> A annotation(Class<A> annotationClass)
      Returns the annotation instance for the given annotation type.
      Type Parameters:
      A - Type of the annotation.
      Parameters:
      annotationClass - Tha annotation type to retrieve.
      Returns:
      The annotation instance.
    • jdkField

      public Field jdkField()
      Returns the JDK Field instance.
      Returns:
      The actual Field instance.
    • reader

      private FieldReader reader()
    • name

      public String name()
      Returns the name of the field.
      Returns:
      Name of the field.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object