Package spoon.pattern

Class Pattern

java.lang.Object
spoon.pattern.Pattern

public class Pattern extends Object
Represents a pattern for matching code. A pattern is composed of a list of AST models, where a model is an AST with some nodes being "pattern parameters". Main documentation at http://spoon.gforge.inria.fr/pattern.html. Instances can created with PatternBuilder. The Pattern can also be used to generate new code where (Pattern) + (pattern parameters) => (copy of pattern where parameters are replaced by parameter values) This is done with generator() and it's methods Differences with TemplateMatcher: - it can match sequences of elements - it can match inlined elements
  • Method Details

    • getParameterInfos

      public Map<String,​ParameterInfo> getParameterInfos()
      Returns:
      Map of parameter names to ParameterInfo for each parameter of this Pattern
    • generator

      public Generator generator()
      Returns:
      a Generator, which can be used to generate a code based on this Pattern
    • forEachMatch

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

      public List<Match> getMatches(CtElement root)
      Finds all target program sub-trees that correspond to this pattern and returns them.
      Parameters:
      root - the root of to be searched AST. It can be a CtElement or List, Set, Map of CtElements
      Returns:
      List of Match
    • print

      public String print(boolean addParameterComments)
      Parameters:
      addParameterComments - if true then it adds comments with parameter names
      Returns:
      pattern printed as java sources
    • toString

      public String toString()
      Overrides:
      toString in class Object