Class FieldInitializer

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

public class FieldInitializer extends Object
Initialize a field with type instance if a default constructor can be found.

If the given field is already initialized, then the actual instance is returned. This initializer doesn't work with inner classes, local classes, interfaces or abstract types.

  • Field Details

  • Constructor Details

    • FieldInitializer

      public FieldInitializer(Object fieldOwner, Field field)
      Prepare initializer with the given field on the given instance.

      This constructor fail fast if the field type cannot be handled.

      Parameters:
      fieldOwner - Instance of the test.
      field - Field to be initialize.
    • FieldInitializer

      public FieldInitializer(Object fieldOwner, Field field, FieldInitializer.ConstructorArgumentResolver argResolver)
      Prepare initializer with the given field on the given instance.

      This constructor fail fast if the field type cannot be handled.

      Parameters:
      fieldOwner - Instance of the test.
      field - Field to be initialize.
      argResolver - Constructor parameters resolver
    • FieldInitializer

      private FieldInitializer(Object fieldOwner, Field field, FieldInitializer.ConstructorInstantiator instantiator)
  • Method Details

    • initialize

      public FieldInitializationReport initialize()
      Initialize field if not initialized and return the actual instance.
      Returns:
      Actual field instance.
    • checkNotLocal

      private void checkNotLocal(Field field)
    • checkNotInner

      private void checkNotInner(Field field)
    • checkNotInterface

      private void checkNotInterface(Field field)
    • checkNotAbstract

      private void checkNotAbstract(Field field)
    • checkNotEnum

      private void checkNotEnum(Field field)
    • acquireFieldInstance

      private FieldInitializationReport acquireFieldInstance() throws IllegalAccessException
      Throws:
      IllegalAccessException