Interface InstancioServiceProvider
- generator mappings via
getGeneratorProvider() - subtype mappings via
getTypeResolver() - class instantiation logic via
getTypeInstantiator() - setter resolution via
getSetterMethodResolver()
All of the above are default methods that return null.
Implementations of this class can override one or more methods as needed.
Instancio will invoke each of the above methods only once, therefore,
these methods may contain initialisation logic.
This class uses the ServiceLoader mechanism. Therefore,
implementations must be registered by placing a file named
org.instancio.spi.InstancioServiceProvider under
/META-INF/services. The file must contain the fully-qualified name
of the implementing class.
Note: only InstancioServiceProvider (and not the interfaces
defined within it) can be registered via the ServiceLoader.
- Since:
- 2.11.0
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceProvides customGeneratorclasses.static interfaceResolves setter method based on a given node whenKeys.ASSIGNMENT_TYPEis set toAssignmentType.METHOD.static interfaceProvides custom instantiation logic.static interfaceResolves subtype based on a given class. -
Method Summary
Modifier and TypeMethodDescriptionReturns aGeneratorProviderimplementation.Returns aSetterMethodResolverimplementation.Returns aTypeInstantiatorimplementation.Returns aTypeResolverimplementation.default voidinit(ServiceProviderContext context) An optional method that can be used by implementations that require access to the context information.
-
Method Details
-
init
An optional method that can be used by implementations that require access to the context information.- Parameters:
context- containing additional information- Since:
- 2.12.0
-
getGeneratorProvider
Returns aGeneratorProviderimplementation.Instancio will automatically use generators configured using the provider. As an example, consider the following use-case where
Personobjects need to be created with valid phone numbers. To achieve this, a customPhonegenerator is supplied:Generator<Phone> phoneGenerator = createPhoneGenerator(); Person person = Instancio.of(Person.class) .supply(all(Phone.class), phoneGenerator) .create();To avoid specifying the generator manually, the
Phonegenerator can be registered using theGeneratorProvider. Once registered, thePersoncan be created without specifying the generator explicitly:Person person = Instancio.create(Person.class); // uses Phone generator- Returns:
- a custom generator provider, or
nullif not required - Since:
- 2.11.0
-
getTypeResolver
Returns aTypeResolverimplementation.- Returns:
- a custom type resolver, or
nullif not required - Since:
- 2.11.0
-
getTypeInstantiator
Returns aTypeInstantiatorimplementation.- Returns:
- a custom type instantiator, or
nullif not required - Since:
- 2.11.0
-
getSetterMethodResolver
Returns aSetterMethodResolverimplementation.- Returns:
- a custom setter method resolver, or
nullif not required - Since:
- 3.2.0
-