public class CtQueryImpl extends Object implements CtQuery
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 and Description |
---|
CtQueryImpl(Object... input) |
Modifier and Type | Method and Description |
---|---|
CtQueryImpl |
addInput(Iterable<?> input) |
CtQueryImpl |
addInput(Object... input)
adds list of elements which will be used as input of the query too
|
<I,R> void |
evaluate(I input,
CtConsumer<R> outputConsumer)
Evaluates this query, ignoring bound input - if any
|
CtQueryImpl |
failurePolicy(QueryFailurePolicy policy)
Defines whether this query will throw
ClassCastException
when the output of the previous step cannot be cast to type of input of next step. |
<R extends CtElement> |
filterChildren(Filter<R> filter)
Recursively scans all children elements of an input element.
|
<R> R |
first()
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> R |
first(Class<R> itemClass)
Same as
CtQuery.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> void |
forEach(CtConsumer<R> consumer)
Actually evaluates the query and for each produced output element of the last step,
calls `consumer.accept(outputElement)`.
|
List<Object> |
getInputs() |
protected void |
handleListenerSetQuery(Object target) |
boolean |
isTerminated() |
<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> |
list(Class<R> itemClass)
Same as
CtQuery.list() , but with static typing on the return type
and the final filtering, which matches only results, which are assignable from that return type. |
CtQueryImpl |
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> CtQueryImpl |
map(CtConsumableFunction<I> code)
Same as
CtQuery.map(CtFunction) , but the returned object is not handled
by java's return statement, but by a call to CtConsumer.accept(Object) , this
allows efficient and easy to write chained processing, see CtConsumableFunction . |
<I,R> CtQueryImpl |
map(CtFunction<I,R> function)
Query elements based on a function, the behavior depends on the return type of the function.
|
CtQueryImpl |
name(String name)
Sets the name of current query, to identify the current step during debugging of a query
|
protected void |
reset()
Called before query is evaluated again
|
<R extends CtElement> |
select(Filter<R> filter)
The matched element for which (filter.matches(element)==true) is sent to the next query step.
|
CtQueryImpl |
setInput(Object... input)
Sets (binds) the input of the query.
|
CtQueryImpl |
stepFailurePolicy(QueryFailurePolicy policy) |
void |
terminate()
Terminates the evaluation of this query.
|
public CtQueryImpl(Object... input)
public List<Object> getInputs()
public CtQueryImpl setInput(Object... input)
CtQuery
CtQueryable.map(spoon.reflect.visitor.chain.CtFunction<I, R>)
or CtQueryable.filterChildren(Filter)
,
then the query is already bound to this element.
A new call of CtQuery.setInput(Object...)
is always possible, it resets the current binding and sets the new one.public CtQueryImpl addInput(Object... input)
input
- public CtQueryImpl addInput(Iterable<?> input)
public <R> void forEach(CtConsumer<R> consumer)
CtQuery
public <R> List<R> list()
CtQuery
list
in interface CtQuery
for an efficient way of manipulating the elements without creating an intermediate list.
public <R> List<R> list(Class<R> itemClass)
CtQuery
CtQuery.list()
, but with static typing on the return type
and the final filtering, which matches only results, which are assignable from that return type.public <R> R first()
CtQuery
public <R> R first(Class<R> itemClass)
CtQuery
CtQuery.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.public <I> CtQueryImpl map(CtConsumableFunction<I> code)
CtQuery
CtQuery.map(CtFunction)
, but the returned object is not handled
by java's return statement, but by a call to CtConsumer.accept(Object)
, this
allows efficient and easy to write chained processing, see CtConsumableFunction
.map
in interface CtQuery
map
in interface CtQueryable
CtQuery.map(CtConsumableFunction)
public <I,R> CtQueryImpl map(CtFunction<I,R> function)
CtQuery
Return type of `function` | Behavior |
Boolean | Select elements if the returned value of `function` is true (as for Filter ). |
? extends Object | Send the returned value of `function` to the next step |
Iterable | Send each item of the collection to the next step |
Object [] | Send each item of the array to the next step |
map
in interface CtQuery
map
in interface CtQueryable
function
- a Function with one parameter of type I returning a value of type RCtQuery.map(CtFunction)
public <R extends CtElement> CtQueryImpl filterChildren(Filter<R> filter)
CtQuery
CtElement.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 in CtElement
) is also checked and may thus be also sent to the next step.
The elements which throw ClassCastException
during Filter.matches(CtElement)
are considered as **not matching**, ie. are excluded.filterChildren
in interface CtQuery
filterChildren
in interface CtQueryable
filter
- used to filter scanned children elements of the AST tree.
If null then all children elements pass to next step.CtQuery.filterChildren(Filter)
public <R extends CtElement> CtQueryImpl select(Filter<R> filter)
CtQuery
ClassCastException
during Filter.matches(CtElement)
are considered as **not matching**, ie. are excluded.public boolean isTerminated()
isTerminated
in interface CtQuery
public void terminate()
CtQuery
public <I,R> void evaluate(I input, CtConsumer<R> outputConsumer)
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 querypublic CtQueryImpl name(String name)
CtQuery
public CtQueryImpl failurePolicy(QueryFailurePolicy policy)
CtQuery
ClassCastException
when the output of the previous step cannot be cast to type of input of next step.
The default value is QueryFailurePolicy.FAIL
, which means than exception is thrown when there is a mismatchCtQueryable.filterChildren(Filter)
step never throws ClassCastException
failurePolicy
in interface CtQuery
policy
- the policypublic CtQueryImpl stepFailurePolicy(QueryFailurePolicy policy)
public CtQueryImpl logging(boolean logging)
protected void handleListenerSetQuery(Object target)
protected void reset()
Copyright © 2007–2019 Inria. All rights reserved.