Class TemplateMatcher

java.lang.Object
spoon.template.TemplateMatcher
All Implemented Interfaces:
Filter<CtElement>

public class TemplateMatcher extends Object implements Filter<CtElement>
This class defines an engine for matching a template to pieces of code.
  • Constructor Details

    • TemplateMatcher

      public TemplateMatcher(CtElement templateRoot)
      Constructs a matcher for a given template.
      Parameters:
      templateRoot - the template to match against
    • TemplateMatcher

      public TemplateMatcher(CtElement templateRoot, CtClass<?> templateType)
      Constructs a matcher for a given template. All parameters must be declared using Template fields.
      Parameters:
      templateRoot - the template model to match against. It must be a child of `templateType`
      templateType - the class of the template, which contains all the template parameters
  • Method Details

    • matches

      public boolean matches(CtElement element)
      Description copied from interface: Filter
      Tells if the given element matches.
      Specified by:
      matches in interface Filter<CtElement>
      Parameters:
      element - - the element to be checked for a match. Parameter element is never null if Query is used.
    • getMatches

      public ImmutableMap getMatches()
      Returns all the matches where the keys are the corresponding template parameters. The matches(CtElement) method must have been called before and must return true. Otherwise it returns null.
    • find

      public <T extends CtElement> List<T> find(CtElement targetRoot)
      Finds all target program sub-trees that correspond to a template.
      Parameters:
      targetRoot - the target to be tested for match
      Returns:
      the matched elements
    • forEachMatch

      public void forEachMatch(CtElement rootElement, CtConsumer<Match> consumer)
      Finds all target program sub-trees that correspond to a template and calls consumer.accept(matchingElement, )
      Parameters:
      rootElement - the root of to be searched AST
      consumer - the receiver of matches