Class ConstructorQuery<X>

  • Type Parameters:
    X - class in which to search constructor
    All Implemented Interfaces:
    Iterable<Constructor<X>>

    public abstract class ConstructorQuery<X>
    extends Object
    Iterable-like container that searches for constructors of a class.

    This is straightforward method to search for class constructor 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<X>).

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

    Example usage, which gets a public constructor in class "UserService" that have annotation "Inject" on them. Before returning constructor, it is marked as Constructor.setAccessible(boolean) accessible}.

         ConstructorQuery.of(UserService.class)
             .requiringModifier(Modifier.PUBLIC)
             .annotatedBy(Inject.class)
             .asAccessible()
             .unique()
     
    • Method Detail

      • of

        public static <X> ConstructorQuery<X> of​(Class<X> type)
        Queries for fields in specified class.
        Type Parameters:
        X - type of constructed objects
        Parameters:
        type - class to search constructors in
        Returns:
        query that returns all constructors in specified class.
      • named

        public ConstructorQuery<X> named​(String name)
        Restricts query to members that have specified name, matched exactly.
        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 ConstructorQuery<X> nameMatching​(Pattern namePattern)
        Restricts query to members that have specified name, matched by 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 ConstructorQuery<X> filter​(Predicate<? super Constructor<X>> filter)
        Filter query elements by arbitrary predicate.

        Immutability warning: queries are assumed as immutable, but this method allows providing any Predicate, which will be held in resulting query. Therefore, if stateful predicate is provided, immutability of resulting query will be compromised. However, this is rarely the case, because most of the filters provided will be non-capturing lambdas or static method references.

        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
      • annotatedWith

        public ConstructorQuery<X> annotatedWith​(AnnotationFilter annotationFilter)
        Restricts query to members that annotation filter matches.
        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 ConstructorQuery<X> requiringModifier​(int requiredModifier)
        Restricts query to members that have specified modifier on them.

        Use Modifier to select modifiers.

        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 ConstructorQuery<X> excludingModifier​(int excludedModifier)
        Restricts query to members that do not have specified modifier on them.

        Use Modifier to select modifiers.

        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
      • parameters

        public Q parameters​(Type... parameterTypes)
      • parameterCount

        public Q parameterCount​(int parameterCount)
      • 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