Package org.instancio
Interface LenientSelector
- All Superinterfaces:
TargetSelector
- All Known Subinterfaces:
FieldSelectorBuilder
,GroupableSelector
,PredicateSelector
,ScopeableSelector
,Selector
,TypeSelectorBuilder
A lenient selector does not trigger the "unused selector" error
if it does not match any targets.
- Since:
- 4.4.0
-
Method Summary
Modifier and TypeMethodDescriptionlenient()
Marks this selector as lenient, which prevents the selector from producing "unused selector" error if it does not match any target.
-
Method Details
-
lenient
TargetSelector lenient()Marks this selector as lenient, which prevents the selector from producing "unused selector" error if it does not match any target. This provides an alternative toLenientMode.lenient()
, which treats all selectors as lenient (not recommended).This method can be useful when using Instancio to create a generic
Model
, or in a helper method for creating objects of arbitrary types.For example, the following method will set all
lastUpdated
fields to a date in the past:static <T> Model<T> baseModel(Class<T> klass) { TargetSelector lastUpdated = Select.fields() .ofType(Instant.class).named("lastUpdated").lenient(); return Instancio.of(klass) .generate(lastUpdated, gen -> gen.temporal().instant().past()) .toModel(); }
Marking the selector as lenient will prevent the unused selector error if a given
klass
does not have alastUpdated
field.See also: Selector Strictness section of the User Guide.
- Returns:
- a lenient selector
- Since:
- 4.4.0
- See Also:
-