Interface TypeResolver
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
-
Method Summary
-
Method Details
-
resolve
Resolves a subtype for a given type.- Parameters:
type
- to resolve- Returns:
- an
Optional
containing a subtype, or an empty result if unresolved. - Since:
- 1.6.0
-