Class MethodQuery
- java.lang.Object
-
- org.perfectable.introspection.query.MethodQuery
-
public abstract class MethodQuery extends Object
Iterable-like container that searches for methods in class.This is straightforward way to search for methods 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 ofstream()
,unique()
,option()
orisPresent()
.All methods present in class are searched, either declared in it directly, or inherited from parent classes. This means that even methods that are overridden in the inheritance chain will be returned, so methods with the same signature but different declaring class might be present multiple times. To avoid this behavior, use
notOverridden()
.Example usage, which injects all public instance methods in class "UserService" that have annotation "Inject" on them, have one parameter, and does not return value (is void). Before passing field to the method, it is marked as
accessible
.MethodQuery.of(UserService.class) .requiringModifier(Modifier.PUBLIC) .excludingModifier(Modifier.STATIC) .annotatedBy(Inject.class) .parameters(ParametersFilter.count(1)) .returningVoid() .asAccessible() .stream() .forEach(this::inject);
-
-
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.MethodQuery
annotatedWith(AnnotationFilter annotationFilter)
Restricts query to members that annotation filter matches.MethodQuery
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.MethodQuery
excludingModifier(int excludedModifier)
Restricts query to members that do not have specified modifier on them.MethodQuery
filter(Predicate<? super Method> filter)
Filter query elements by arbitrary predicate.boolean
isPresent()
Checks if any element was contained in query.Iterator<E>
iterator()
MethodQuery
named(String name)
Restricts query to members that have specified name, matched exactly.MethodQuery
nameMatching(Pattern namePattern)
Restricts query to members that have specified name, matched by pattern.MethodQuery
notOverridden()
Restricts query for methods that are not overridden by other in the inheritance chain.static MethodQuery
of(Class<?> 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)
MethodQuery
parameters(ParametersFilter parametersFilter)
MethodQuery
requiringModifier(int requiredModifier)
Restricts query to members that have specified modifier on them.MethodQuery
returning(Type type)
Restricts query to methods that has a return type that is subtype of specified type.MethodQuery
returning(TypeFilter typeFilter)
Restricts query to methods that has a return type that match the filter.MethodQuery
returningVoid()
Restricts query to methods that doesn't return a value, i.e.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 MethodQuery of(Class<?> type)
Queries for fields in specified class.- Parameters:
type
- class to search methods in- Returns:
- query that returns all constructors in specified class.
-
named
public MethodQuery 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 MethodQuery 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 MethodQuery filter(Predicate<? super Method> 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 MethodQuery parameters(ParametersFilter parametersFilter)
-
returning
public MethodQuery returning(Type type)
Restricts query to methods that has a return type that is subtype of specified type.This methods accepts every type, even
Void.TYPE
. In latter case, it will filter for methods that does not return a value, i.e. void methods. For readability, usereturningVoid()
.- Parameters:
type
- return type to restrict- Returns:
- copy of this query that will also filter by return type
-
returning
public MethodQuery returning(TypeFilter typeFilter)
Restricts query to methods that has a return type that match the filter.- Parameters:
typeFilter
- filter to restrict method return type with- Returns:
- copy of this query that will also filter by return type
-
returningVoid
public MethodQuery returningVoid()
Restricts query to methods that doesn't return a value, i.e. are void.- API Note:
- This method could be named "notReturning", but its a matter of opinion if it would be more confusing than current name. The void method actually returns, ends normally.
- Returns:
- copy of this query that will filter methods that have void return
-
notOverridden
public MethodQuery notOverridden()
Restricts query for methods that are not overridden by other in the inheritance chain.Normally, the query returns methods that are overridden in the inheritance chain - methods with the same signature but different declaring class might be present multiple times. Using this method will change that behavior.
- Returns:
- copy of this query that will filter methods that are not overridden
-
annotatedWith
public MethodQuery 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 MethodQuery 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 MethodQuery 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 MethodQuery 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
-
-