Interface TypeResolver


@Deprecated public interface TypeResolver
Deprecated.
use InstancioServiceProvider instead. This class will be removed in version 3.0.0.
A Service Provider Interface for mapping types to subtypes.

Implementations of this class can be registered by placing a file named org.instancio.spi.TypeResolver under /META-INF/services. The file must contain the fully-qualified name of the implementing class.

The primary use case for this class is to map types to subtypes dynamically at runtime. Similar functionality can be achieved using Settings as follows:


   Settings settings = Settings.create()
       .mapType(AbstractWidget.class, ConcreteWidget.class);

   AbstractWidget widget = Instancio.of(AbstractWidget.class)
       .withSettings(settings)
       .create();

   assertThat(widget).isExactlyInstanceOf(ConcreteWidget.class);
 

However, the above approach requires passing in the Settings as an argument, or using settings injection via the @WithSettings annotation and InstancioExtension for JUnit Jupiter.

In situations where the above is too cumbersome, custom TypeResolver can be registered to resolves subtypes. Once registered, the resolution will be done automatically, for example:


   AbstractWidget widget = Instancio.create(AbstractWidget.class);
   assertThat(widget).isExactlyInstanceOf(ConcreteWidget.class);
 

Using the SPI also allows providing resolver implementations that perform resolution by scanning the classpath.

Since:
1.6.0