Class CtQueryImpl

java.lang.Object
spoon.reflect.visitor.chain.CtQueryImpl
All Implemented Interfaces:
CtQuery, CtQueryable

public class CtQueryImpl extends Object implements CtQuery
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 Details

    • CtQueryImpl

      public CtQueryImpl(Object... input)
  • Method Details

    • getInputs

      public List<Object> getInputs()
      Returns:
      list of elements which will be used as input of the query
    • setInput

      public CtQueryImpl setInput(Object... input)
      Description copied from interface: CtQuery
      Sets (binds) the input of the query. If the query is created by 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.
      Specified by:
      setInput in interface CtQuery
      Returns:
      this to support fluent API
    • addInput

      public CtQueryImpl addInput(Object... input)
      adds list of elements which will be used as input of the query too
      Parameters:
      input -
      Returns:
      this to support fluent API
    • addInput

      public CtQueryImpl addInput(Iterable<?> input)
    • forEach

      public <R> void forEach(CtConsumer<R> consumer)
      Description copied from interface: CtQuery
      Actually evaluates the query and for each produced output element of the last step, calls `consumer.accept(outputElement)`. This avoids to create useless intermediate lists.
      Specified by:
      forEach in interface CtQuery
      Parameters:
      consumer - The consumer which accepts the results of the query
    • list

      public <R> List<R> list()
      Description copied from interface: CtQuery
      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. So use the type, which matches the results of your query, otherwise the ClassCastException will be thrown when reading the list.
      Specified by:
      list in interface CtQuery
      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

      public <R> List<R> list(Class<R> itemClass)
      Description copied from interface: CtQuery
      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.
      Specified by:
      list in interface CtQuery
      Returns:
      the list of elements collected by the query.
    • first

      public <R> R first()
      Description copied from interface: CtQuery
      Actually 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.
      Specified by:
      first in interface CtQuery
      Returns:
      the first element found by the query.
    • first

      public <R> R first(Class<R> itemClass)
      Description copied from interface: CtQuery
      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.
      Specified by:
      first in interface CtQuery
      Returns:
      the list of elements collected by the query.
    • map

      public <I> CtQueryImpl map(CtConsumableFunction<I> code)
      Description copied from interface: CtQuery
      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.
      Specified by:
      map in interface CtQuery
      Specified by:
      map in interface CtQueryable
      Returns:
      this to support fluent API
      See Also:
      CtQuery.map(CtConsumableFunction)
    • map

      public <I,​ R> CtQueryImpl map(CtFunction<I,​R> function)
      Description copied from interface: CtQuery
      Query 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:
      map in interface CtQuery
      Specified by:
      map in interface CtQueryable
      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

      public <R extends CtElement> CtQueryImpl filterChildren(Filter<R> filter)
      Description copied from interface: CtQuery
      Recursively 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 as 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.
      Specified by:
      filterChildren in interface CtQuery
      Specified by:
      filterChildren in interface CtQueryable
      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

      public <R extends CtElement> CtQueryImpl select(Filter<R> filter)
      Description copied from interface: CtQuery
      The matched element for which (filter.matches(element)==true) is sent to the next query step. The elements which throw ClassCastException during Filter.matches(CtElement) are considered as **not matching**, ie. are excluded.
      Specified by:
      select in interface CtQuery
      Parameters:
      filter - used to detect if input element can pass to next query step
      Returns:
      this to support fluent API
    • isTerminated

      public boolean isTerminated()
      Specified by:
      isTerminated in interface CtQuery
      Returns:
      true if the evaluation has been terminated.
    • terminate

      public void terminate()
      Description copied from interface: CtQuery
      Terminates the evaluation of this query. The query still returns all results collected before termination. This method should not throw an exception.
      Specified by:
      terminate in interface CtQuery
    • evaluate

      public <I,​ R> void evaluate(I input, CtConsumer<R> outputConsumer)
      Evaluates this query, ignoring bound input - if any
      Parameters:
      input - represents the input element of the first mapping function of this query
      outputConsumer - method accept of the outputConsumer is called for each element produced by last mapping function of this query
    • name

      public CtQueryImpl name(String name)
      Description copied from interface: CtQuery
      Sets the name of current query, to identify the current step during debugging of a query
      Specified by:
      name in interface CtQuery
      Returns:
      this to support fluent API
    • failurePolicy

      public CtQueryImpl failurePolicy(QueryFailurePolicy policy)
      Description copied from interface: CtQuery
      Defines whether this query will throw 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 mismatch
      Note: The CtQueryable.filterChildren(Filter) step never throws ClassCastException
      Specified by:
      failurePolicy in interface CtQuery
      Parameters:
      policy - the policy
      Returns:
      this to support fluent API
    • stepFailurePolicy

      public CtQueryImpl stepFailurePolicy(QueryFailurePolicy policy)
    • logging

      public 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. Reason: Query chains are used internally during writing of log messages too. So it would write logs for ever...
    • handleListenerSetQuery

      protected void handleListenerSetQuery(Object target)
    • reset

      protected void reset()
      Called before query is evaluated again