Class ConstructorQuery<X>
- java.lang.Object
-
- org.perfectable.introspection.query.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 ofstream()
,unique()
,option()
orisPresent()
.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 Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Q
annotatedWith(Class<? extends Annotation> annotationClass)
Restricts query to members that have an annotation of specified class.ConstructorQuery<X>
annotatedWith(AnnotationFilter annotationFilter)
Restricts query to members that annotation filter matches.ConstructorQuery<X>
asAccessible()
Returns query that provides members which haveAccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)
called on them.boolean
contains(Object candidate)
Checks if provided object is in elements that would be returned by this query.ConstructorQuery<X>
excludingModifier(int excludedModifier)
Restricts query to members that do not have specified modifier on them.ConstructorQuery<X>
filter(Predicate<? super Constructor<X>> filter)
Filter query elements by arbitrary predicate.boolean
isPresent()
Checks if any element was contained in query.Iterator<E>
iterator()
ConstructorQuery<X>
named(String name)
Restricts query to members that have specified name, matched exactly.ConstructorQuery<X>
nameMatching(Pattern namePattern)
Restricts query to members that have specified name, matched by pattern.static <X> ConstructorQuery<X>
of(Class<X> type)
Queries for fields in specified class.Optional<E>
option()
Fetches only element present in this query, or empty optional.Q
parameterCount(int parameterCount)
Q
parameters(Type... parameterTypes)
ConstructorQuery<X>
parameters(ParametersFilter parametersFilter)
ConstructorQuery<X>
requiringModifier(int requiredModifier)
Restricts query to members that have specified modifier on them.abstract Stream<E>
stream()
Adapts this query to a Stream.E
unique()
Returns single element that this query contains.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
-
-
-
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
-
parameters
public ConstructorQuery<X> parameters(ParametersFilter parametersFilter)
-
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
-
asAccessible
public ConstructorQuery<X> asAccessible()
Returns query that provides members which haveAccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)
called on them.- Returns:
- query that filters the same as this query, but with accessible flag set
-
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 elementIllegalArgumentException
- 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
-
-