Package org.instancio

Class Select


  • public final class Select
    extends Object
    A collection of static factory methods selecting fields and classes.

    Examples:

    • field(Example.class, "someField") - select some field of Example class
    • all(Example.class) - select all instances of Example class
    • all(GroupableSelector...) - convenience method for combining multiple selectors
    • allStrings() - select all Strings
    • allInts() - select all Integer objects and int primitives
    • Method Detail

      • all

        public static Selector all​(Class<?> type)
        Select all instances of the given type, not including subtypes.

        If the type is a primitive or wrapper, this method only selects the specified type. For example:

        • all(int.class) - selects primitive int but not Integer
        • all(Integer.class) - selects Integer wrapper but not primitive int

        In order to select both, primitive int and wrapper, use the allInts().

        Parameters:
        type - to select
        Returns:
        a selector for given class
      • all

        public static SelectorGroup all​(GroupableSelector... selectors)
        A convenience method for combining multiple selectors.
        Parameters:
        selectors - to combine
        Returns:
        a group containing given selectors
      • field

        public static Selector field​(Class<?> declaringClass,
                                     String fieldName)
        Selects a field of the specified class.
        Parameters:
        declaringClass - class declaring the field
        fieldName - field name to select
        Returns:
        a selector for given field
      • field

        public static Selector field​(String fieldName)
        Selects a field that belongs to the class being created.

        Example

        
             Person person = Instancio.of(Person.class)
                     .ignore(field("fullName")) // Person.fullName
                     .create();
         
        Parameters:
        fieldName - field name to select
        Returns:
        a selector for given field
      • allStrings

        public static Selector allStrings()
        Shorthand for all(String.class).
        Returns:
        selector for all Strings
      • allBytes

        public static Selector allBytes()
        Selects all bytes, primitive and wrapper.
        Returns:
        selector for all bytes
      • allFloats

        public static Selector allFloats()
        Selects all floats, primitive and wrapper.
        Returns:
        selector for all floats
      • allShorts

        public static Selector allShorts()
        Selects all shorts, primitive and wrapper.
        Returns:
        selector for all shorts
      • allInts

        public static Selector allInts()
        Selects all integers, primitive and wrapper.
        Returns:
        selector for all integers
      • allLongs

        public static Selector allLongs()
        Selects all longs, primitive and wrapper.
        Returns:
        selector for all longs
      • allDoubles

        public static Selector allDoubles()
        Selects all doubles, primitive and wrapper.
        Returns:
        selector for all doubles
      • allBooleans

        public static Selector allBooleans()
        Selects all booleans, primitive and wrapper.
        Returns:
        selector for all booleans
      • allChars

        public static Selector allChars()
        Selects all characters, primitive and wrapper.
        Returns:
        selector for all characters
      • scope

        public static Scope scope​(Class<?> targetClass,
                                  String fieldName)
        Creates a scope for narrowing down a selector's target to a field of the specified class.

        For example, the following will set all lists within Person.address object to an empty list.

        
             Person person = Instancio.of(Person.class)
                 .set(all(List.class).within(scope(Person.class, "address")), Collections.emptyList())
                 .create();
         
        Parameters:
        targetClass - of the scope
        fieldName - declared by the target class
        Returns:
        a scope for fine-tuning a selector
        Since:
        1.3.0
      • scope

        public static Scope scope​(Class<?> targetClass)
        Creates a selector scope for narrowing down a selector's target to the specified class.

        For example, assuming a Customer class that has a CustomerConsent class. the following will set all booleans within CustomerConsent to true.

        
             Customer customer = Instancio.of(Customer.class)
                 .set(allBooleans().within(scope(CustomerConsent.class)), true)
                 .create();
         
        Parameters:
        targetClass - of the scope
        Returns:
        a scope for fine-tuning a selector
        Since:
        1.3.0