Package spoon.reflect.visitor.chain
Class CtQueryImpl
java.lang.Object
spoon.reflect.visitor.chain.CtQueryImpl
- All Implemented Interfaces:
CtQuery,CtQueryable
The facade of
CtQuery which represents a query bound to the CtElement,
which is the constant input of this query.
It is used by CtElement implementations of CtQueryable.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadds list of elements which will be used as input of the query too<I, R> voidevaluate(I input, CtConsumer<R> outputConsumer)Evaluates this query, ignoring bound input - if anyfailurePolicy(QueryFailurePolicy policy)Defines whether this query will throwClassCastExceptionwhen the output of the previous step cannot be cast to type of input of next step.<R extends CtElement>
CtQueryImplfilterChildren(Filter<R> filter)Recursively scans all children elements of an input element.<R> Rfirst()Actually evaluates the query and returns first elements produced in the last step.
After the first element is found, the query evaluation is terminated.<R> RSame asCtQuery.first(), but with static typing on the return type and the final filtering, which matches only the first result, which is assignable from that return type.<R> voidforEach(CtConsumer<R> consumer)Actually evaluates the query and for each produced output element of the last step, calls `consumer.accept(outputElement)`.protected voidhandleListenerSetQuery(Object target)boolean<R> List<R>list()Actually evaluates the query and returns all the elements produced in the last step.
Note: The type R of the list is not checked by the query.<R> List<R>Same asCtQuery.list(), but with static typing on the return type and the final filtering, which matches only results, which are assignable from that return type.logging(boolean logging)Enable/disable logging for this query Note: it is not possible to enable logging of all queries globally by Launcher.LOGGER.isDebugEnabled() because it causes StackOverflow.<I> CtQueryImplmap(CtConsumableFunction<I> code)Same asCtQuery.map(CtFunction), but the returned object is not handled by java's return statement, but by a call toCtConsumer.accept(Object), this allows efficient and easy to write chained processing, seeCtConsumableFunction.<I, R> CtQueryImplmap(CtFunction<I,R> function)Query elements based on a function, the behavior depends on the return type of the function.Sets the name of current query, to identify the current step during debugging of a queryprotected voidreset()Called before query is evaluated again<R extends CtElement>
CtQueryImplThe matched element for which (filter.matches(element)==true) is sent to the next query step.Sets (binds) the input of the query.stepFailurePolicy(QueryFailurePolicy policy)voidTerminates the evaluation of this query.
-
Constructor Details
-
CtQueryImpl
-
-
Method Details
-
getInputs
- Returns:
- list of elements which will be used as input of the query
-
setInput
Description copied from interface:CtQuerySets (binds) the input of the query. If the query is created byCtQueryable.map(spoon.reflect.visitor.chain.CtFunction<I, R>)orCtQueryable.filterChildren(Filter), then the query is already bound to this element. A new call ofCtQuery.setInput(Object...)is always possible, it resets the current binding and sets the new one. -
addInput
adds list of elements which will be used as input of the query too- Parameters:
input-- Returns:
- this to support fluent API
-
addInput
-
forEach
Description copied from interface:CtQueryActually evaluates the query and for each produced output element of the last step, calls `consumer.accept(outputElement)`. This avoids to create useless intermediate lists. -
list
Description copied from interface:CtQueryActually evaluates the query and returns all the elements produced in the last step.
Note: The type R of the list is not checked by the query. So use the type, which matches the results of your query, otherwise the ClassCastException will be thrown when reading the list.- Specified by:
listin interfaceCtQuery- Returns:
- the list of elements collected by the query.
- See Also:
for an efficient way of manipulating the elements without creating an intermediate list.
-
list
Description copied from interface:CtQuerySame asCtQuery.list(), but with static typing on the return type and the final filtering, which matches only results, which are assignable from that return type. -
first
public <R> R first()Description copied from interface:CtQueryActually evaluates the query and returns first elements produced in the last step.
After the first element is found, the query evaluation is terminated. Note: The return type R is not checked by the query. So use the type, which matches the results of your query, otherwise the ClassCastException will be thrown. -
first
Description copied from interface:CtQuerySame asCtQuery.first(), but with static typing on the return type and the final filtering, which matches only the first result, which is assignable from that return type. -
map
Description copied from interface:CtQuerySame asCtQuery.map(CtFunction), but the returned object is not handled by java's return statement, but by a call toCtConsumer.accept(Object), this allows efficient and easy to write chained processing, seeCtConsumableFunction.- Specified by:
mapin interfaceCtQuery- Specified by:
mapin interfaceCtQueryable- Returns:
- this to support fluent API
- See Also:
CtQuery.map(CtConsumableFunction)
-
map
Description copied from interface:CtQueryQuery elements based on a function, the behavior depends on the return type of the function.Return type of `function` Behavior BooleanSelect elements if the returned value of `function` is true (as for Filter).? extends ObjectSend the returned value of `function` to the next step IterableSend each item of the collection to the next step Object[]Send each item of the array to the next step - Specified by:
mapin interfaceCtQuery- Specified by:
mapin interfaceCtQueryable- Parameters:
function- a Function with one parameter of type I returning a value of type R- Returns:
- this to support fluent API
- See Also:
CtQuery.map(CtFunction)
-
filterChildren
Description copied from interface:CtQueryRecursively scans all children elements of an input element. The matched child element for which (filter.matches(element)==true) are sent to the next query step. Essentially the same asCtElement.getElements(Filter)but more powerful, because it can be chained with other subsequent queries. Note: the input element (the root of the query, `this` if you're inCtElement) is also checked and may thus be also sent to the next step. The elements which throwClassCastExceptionduringFilter.matches(CtElement)are considered as **not matching**, ie. are excluded.- Specified by:
filterChildrenin interfaceCtQuery- Specified by:
filterChildrenin interfaceCtQueryable- Parameters:
filter- used to filter scanned children elements of the AST tree. If null then all children elements pass to next step.- Returns:
- this to support fluent API
- See Also:
CtQuery.filterChildren(Filter)
-
select
Description copied from interface:CtQueryThe matched element for which (filter.matches(element)==true) is sent to the next query step. The elements which throwClassCastExceptionduringFilter.matches(CtElement)are considered as **not matching**, ie. are excluded. -
isTerminated
public boolean isTerminated()- Specified by:
isTerminatedin interfaceCtQuery- Returns:
- true if the evaluation has been terminated.
-
terminate
public void terminate()Description copied from interface:CtQueryTerminates the evaluation of this query. The query still returns all results collected before termination. This method should not throw an exception. -
evaluate
Evaluates this query, ignoring bound input - if any- Parameters:
input- represents the input element of the first mapping function of this queryoutputConsumer- method accept of the outputConsumer is called for each element produced by last mapping function of this query
-
name
Description copied from interface:CtQuerySets the name of current query, to identify the current step during debugging of a query -
failurePolicy
Description copied from interface:CtQueryDefines whether this query will throwClassCastExceptionwhen the output of the previous step cannot be cast to type of input of next step. The default value isQueryFailurePolicy.FAIL, which means than exception is thrown when there is a mismatch
Note: TheCtQueryable.filterChildren(Filter)step never throwsClassCastException- Specified by:
failurePolicyin interfaceCtQuery- Parameters:
policy- the policy- Returns:
- this to support fluent API
-
stepFailurePolicy
-
logging
Enable/disable logging for this query Note: it is not possible to enable logging of all queries globally by Launcher.LOGGER.isDebugEnabled() because it causes StackOverflow. Reason: Query chains are used internally during writing of log messages too. So it would write logs for ever... -
handleListenerSetQuery
-
reset
protected void reset()Called before query is evaluated again
-