Interface InstancioServiceProvider.TypeResolver

Enclosing interface:
InstancioServiceProvider

public static interface InstancioServiceProvider.TypeResolver
Resolves subtype based on a given class.

An implementation of this interface can be returned via the InstancioServiceProvider.getTypeResolver() method.

Since:
2.11.0
  • Method Summary

    Modifier and Type
    Method
    Description
    getSubtype(Class<?> type)
    Returns a subtype for the given type.
  • Method Details

    • getSubtype

      Class<?> getSubtype(Class<?> type)
      Returns a subtype for the given type.

      Similar functionality can be achieved using:

      However, the methods above require specifying the subtypes manually. This class allows subtype resolution to be done automatically. For example, if this method maps an abstract Animal class to a concrete Dog class, then Instancio will generate objects based on this mapping:

      
         Animal animal = Instancio.create(Animal.class);
         assertThat(animal).isExactlyInstanceOf(Dog.class);
       
      Parameters:
      type - the type to map to a subtype
      Returns:
      the subtype class, or null if no subtype is defined
      Throws:
      InstancioSpiException - if the returned class is not a subtype of the argument class
      Since:
      2.11.0