Class InjectionsImpl
java.lang.Object
io.github.cjstehno.testthings.inject.InjectionsImpl
- All Implemented Interfaces:
Injections
Default Injections implementation.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<T> T
apply
(T instance) Applies the configured injections to the given instance.Allows in-place modification of a value reference from the target instance.Injects a value into the setters setting the specified type - if a setter does not exist, it will try to inject directly into a field of the type.IfpreferSetter
istrue
, an attempt will be made to inject the value using a setter method for the named property, otherwise the field itself will be injected.Updates a field value by applying given function to the current value.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.github.cjstehno.testthings.inject.Injections
modifyField, modifyProperty, setField, setField, setProperty, setProperty, update, updateField, updateProperty
-
Constructor Details
-
InjectionsImpl
public InjectionsImpl()
-
-
Method Details
-
set
Description copied from interface:Injections
IfpreferSetter
istrue
, an attempt will be made to inject the value using a setter method for the named property, otherwise the field itself will be injected. If thepreferSetter
value isfalse
the field will be directly injected.- Specified by:
set
in interfaceInjections
- Parameters:
name
- the name of the property or fieldvalue
- the value (may be a Randomizer).preferSetter
- whether to prefer the setter over direct injection- Returns:
- the configured Injections instance
-
set
Description copied from interface:Injections
Injects a value into the setters setting the specified type - if a setter does not exist, it will try to inject directly into a field of the type. If a setter and a field both match, it will only inject one of them, not both.- Specified by:
set
in interfaceInjections
- Parameters:
type
- the field typevalue
- the value to injectsetter
- prefers using the setter if it exists- Returns:
- the configured injections instance
-
update
public Injections update(String name, Function<Object, Object> updater, boolean preferSetter, boolean preferGetter) Description copied from interface:Injections
Updates a field value by applying given function to the current value. If thepreferProps
parameter istrue
, the injector will first attempt to use the named property before defaulting update the field directly.If the updater function returns a Randomizer, it will be used to generate the updated value.
- Specified by:
update
in interfaceInjections
- Parameters:
name
- the property or field nameupdater
- the updater functionpreferSetter
- will try to use the setter rather than go directly to the fieldpreferGetter
- will try to use the getter rather than go directly to the field- Returns:
- the instance to the Injections implementation
-
modify
Description copied from interface:Injections
Allows in-place modification of a value reference from the target instance. The target instance will be passed to the provided consumer for modification.- Specified by:
modify
in interfaceInjections
- Parameters:
name
- the namemodifier
- the modifierpreferGetter
- will try to use the getter rather than going directly to the field- Returns:
- the instance of the Injections implementation
-
apply
Applies the configured injections to the given instance.- Type Parameters:
T
- the type of the target instance- Parameters:
instance
- the target instance- Returns:
- the updated instance (reference to the incoming instance)
- Throws:
ReflectiveOperationException
- if there is a problem injecting
-