Interface FieldSelectorBuilder
- All Superinterfaces:
ConvertibleToScope
,DepthPredicateSelector
,DepthSelector
,GroupableSelector
,LenientSelector
,ScopeableSelector
,TargetSelector
,WithinScope
An instance of the builder can be obtained using Select.fields()
.
Other methods from this class can be chained to form logical AND
relationships, for example:
fields().ofType(String.class).annotated(Foo.class).annotated(Bar.class)
will match String fields annotated @Foo
and @Foo
.
- Since:
- 1.6.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescription<A extends Annotation>
FieldSelectorBuilderMatches fields annotated with the specified annotation, ignoring inherited annotations.atDepth
(int depth) Restricts this selector's target(s) to the specified depth.Restricts this selector's target(s) to a depth that satisfies the givenpredicate
.declaredIn
(Class<?> type) Matches fields declared in the specified class.Matches field names using the specified regular expression.Matches fields with the specified name.Matches fields against the specified type, including subtypes.toScope()
Scope representation of a selector.Specifies the scope for this selector in order to narrow down its target.Methods inherited from interface org.instancio.ScopeableSelector
lenient
-
Method Details
-
named
Matches fields with the specified name.- Parameters:
fieldName
- exact field name to match- Returns:
- selector builder
- Since:
- 1.6.0
- See Also:
-
matching
Matches field names using the specified regular expression.- Parameters:
regex
- for matching field names- Returns:
- selector builder
- Since:
- 2.2.0
- See Also:
-
ofType
Matches fields against the specified type, including subtypes. For example, selecting fieldsofType(Collection.class
} will match fields declared asList
orSet
.Note: this method will not match fields declared as a
TypeVariable
. For instance, in the following snippet, even thoughT
is bound toString
type, the selectorofType(String.class)
will not match the field. In this case, targeting the field by name would be a better option.class Item<T> { private T value; } Item<String> result = Instancio.of(new TypeToken<Item<String>>() {}) .set(fields().ofType(String.class), "foo") // will not match Item.value .create(); assertThat(result.getValue()).isNotEqualTo("foo");
- Parameters:
fieldType
- field type to match- Returns:
- selector builder
- Since:
- 1.6.0
-
declaredIn
Matches fields declared in the specified class.- Parameters:
type
- class that declares target field(s)- Returns:
- selector builder
- Since:
- 1.6.0
-
annotated
Matches fields annotated with the specified annotation, ignoring inherited annotations.The method can be chained to require multiple annotations.
- Type Parameters:
A
- annotation type- Parameters:
annotation
- declared annotation to match- Returns:
- selector builder
- Since:
- 1.6.0
-
atDepth
Restricts this selector's target(s) to the specified depth.When a selector
atDepth(N)
is convertedtoScope()
, the semantics ofSelector.within(Scope...)
method still hold, meaning: at depth equal to or greater thanN
.- Specified by:
atDepth
in interfaceDepthSelector
- Parameters:
depth
- the depth at which selector applies- Returns:
- selector restricted to the specified depth
- Since:
- 2.14.0
-
atDepth
Restricts this selector's target(s) to a depth that satisfies the givenpredicate
.- Specified by:
atDepth
in interfaceDepthPredicateSelector
- Parameters:
atDepth
- specifying the depth- Returns:
- selector restricted to the specified depth
- Since:
- 2.14.0
-
within
Specifies the scope for this selector in order to narrow down its target.For example, given the following classes:
record Phone(String countryCode, String number) {} record Person(Phone home, Phone cell) {}
setting
home
andcell
phone numbers to different values would require differentiating between twofield(Phone::number)
selectors. This can be achieved using scopes as follows:Scope homePhone = field(Person::home).toScope(); Scope cellPhone = field(Person::cell).toScope(); Person person = Instancio.of(Person.class) .set(field(Phone::number).within(homePhone), "777-88-99") .set(field(Phone::number).within(cellPhone), "123-45-67") .create();
See Selector Scopes section of the user guide for details.
- Specified by:
within
in interfaceScopeableSelector
- Specified by:
within
in interfaceWithinScope
- Parameters:
scopes
- one or more scopes to apply- Returns:
- a selector with the specified scope
- Since:
- 4.2.0
-
toScope
Scope representation of a selector.- Specified by:
toScope
in interfaceConvertibleToScope
- Returns:
- scope
- Since:
- 4.2.0
-