Class FieldQuery

  • All Implemented Interfaces:
    Iterable<Field>

    public abstract class FieldQuery
    extends Object
    Iterable-like container that searches for fields in class.

    This is straightforward method to search for class fields that have specified characteristics in provided class.

    Instances of this class are immutable, each filtering produces new, modified instance. To obtain unrestricted query, use of(java.lang.Class<?>).

    To obtain results either iterate this class with Iterable.iterator() (or in enhanced-for loop) or use one of stream(), unique(), option() or isPresent().

    All fields present in class are searched, either declared in it directly, or inherited from parent classes.

    Example usage, which processes all non-static, public fields in class "User" that have annotation "Inject" on them. Before passing field to the method, it is marked as accessible.

         FieldQuery.of(User.class)
             .requiringModifier(Modifier.PUBLIC)
             .excludingModifier(Modifier.STATIC)
             .annotatedBy(Inject.class)
             .asAccessible()
             .stream()
             .forEach(this::process);
     
    • Method Detail

      • empty

        public static FieldQuery empty()
        This simple Null Object Pattern for this query.
        Returns:
        Query which will produce no results.
      • of

        public static FieldQuery of​(Class<?> type)
        Queries for fields in specified class.
        Parameters:
        type - class to search fields in.
        Returns:
        query that returns all fields in specified class.
      • join

        public FieldQuery join​(FieldQuery other)
        Joins results of two queries.

        Resulting query will return all fields returned by this query, followed by all fields returned by provided query.

        Parameters:
        other - secondary query that will be joined.
        Returns:
        Query that contains results from this and provided other query.
      • named

        public FieldQuery named​(String name)
        Restricts query to fields with specified name.
        Parameters:
        name - name that member must have to match
        Returns:
        query that filters the same as this query, but returning elements that have specified name
      • nameMatching

        public FieldQuery nameMatching​(Pattern namePattern)
        Restricts query to fields with name that matches specified pattern.
        Parameters:
        namePattern - pattern that member must match to be included in query
        Returns:
        query that filters the same as this query, but returning elements that have specified name
      • filter

        public FieldQuery filter​(Predicate<? super Field> filter)
        Restricts query to fields which matches arbitrary filter.
        Parameters:
        filter - predicate that will determine if this query will contain element
        Returns:
        query returning the same elements as this one, but only if they match the predicate
      • typed

        public FieldQuery typed​(Type type)
        Restricts query to fields with type that is subtype of specified type.

        In other words, this method will restrict query to fields that when get from object, will return instances of specified type (or null).

        Parameters:
        type - type to restrict
        Returns:
        copy of this query that will also filter by type
      • typed

        public FieldQuery typed​(TypeFilter typeFilter)
        Restricts query to fields which have type that matches specified arbitrary type filter.
        Parameters:
        typeFilter - filter of type to restrict
        Returns:
        copy of this query that will also filter by type
      • annotatedWith

        public FieldQuery annotatedWith​(AnnotationFilter annotationFilter)
        Restricts query to fields which are annotated by specified annotation filter.
        Parameters:
        annotationFilter - filter that must match
        Returns:
        query that filters the same as this query, but returning elements that only match specified filter
      • requiringModifier

        public FieldQuery requiringModifier​(int requiredModifier)
        Restricts query to fields that have specified modifier on them.
        Parameters:
        requiredModifier - modifier bits that must be present on member
        Returns:
        query that filters the same as this query, but with modifiers that match provided
      • excludingModifier

        public FieldQuery excludingModifier​(int excludedModifier)
        Restricts query to fields that don't have specified modifier.
        Parameters:
        excludedModifier - modifier bits that must not be present on member
        Returns:
        query that filters the same as this query, but without modifiers that match provided
      • asAccessible

        public FieldQuery asAccessible()
        Makes query mark field as {Field#setAccessible accessible} when iterating.
        Returns:
        query that filters the same as this query, but with accessible flag set
      • annotatedWith

        public Q annotatedWith​(Class<? extends Annotation> annotationClass)
        Restricts query to members that have an annotation of specified class.
        Parameters:
        annotationClass - class of annotation that member must have to match
        Returns:
        query that filters the same as this query, but returning elements that have specified annotation
      • contains

        public boolean contains​(@CompatibleWith("E")
                                Object candidate)
        Checks if provided object is in elements that would be returned by this query.

        This is often faster than checking by iteration, and most queries in library override this method, but this is not guaranteed, as it might not be possible.

        Parameters:
        candidate - object to check if it is contained in this query.
        Returns:
        if object would be returned by the query
      • stream

        public abstract Stream<E> stream()
        Adapts this query to a Stream.
        Returns:
        stream with elements of this query
      • unique

        public final E unique()
        Returns single element that this query contains.
        Returns:
        single element matches by this query
        Throws:
        NoSuchElementException - if this query didn't contain any element
        IllegalArgumentException - if the query contains multiple elements
      • isPresent

        public boolean isPresent()
        Checks if any element was contained in query.
        Returns:
        true if at least one element is present in this query
      • option

        public final Optional<E> option()
        Fetches only element present in this query, or empty optional.
        Returns:
        single element matched by this query, or empty optional.
        Throws:
        IllegalArgumentException - if the query contains multiple elements