Package org.instancio

Interface FieldSelectorBuilder

All Superinterfaces:
TargetSelector
All Known Implementing Classes:
FieldSelectorBuilderImpl

public interface FieldSelectorBuilder extends TargetSelector
A builder for constructing predicate-based field selectors.

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 Details

    • named

      FieldSelectorBuilder named(String fieldName)
      Matches fields with the specified name.
      Parameters:
      fieldName - exact field name to match
      Returns:
      selector builder
      Since:
      1.6.0
      See Also:
    • matching

      FieldSelectorBuilder matching(String regex)
      Matches field names using the specified regular expression.
      Parameters:
      regex - for matching field names
      Returns:
      selector builder
      Since:
      2.2.0
      See Also:
    • ofType

      FieldSelectorBuilder ofType(Class<?> fieldType)
      Matches fields against the specified type, including subtypes. For example, selecting fields ofType(Collection.class} will match fields declared as List or Set.

      Note: this method will not match fields declared as a TypeVariable. For instance, in the following snippet, even though T is bound to String type, the selector ofType(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

      FieldSelectorBuilder declaredIn(Class<?> type)
      Matches fields declared in the specified class.
      Parameters:
      type - class that declares target field(s)
      Returns:
      selector builder
      Since:
      1.6.0
    • annotated

      <A extends Annotation> FieldSelectorBuilder annotated(Class<? extends A> annotation)
      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