Package spoon.pattern

Class 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 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()
      Map of parameter names to ParameterInfo for each parameter of this Pattern
    • generator

      public Generator generator()
      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)
      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.
      root - the root of to be searched AST. It can be a CtElement or List, Set, Map of CtElements
      List of Match
    • print

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

      public String toString()
      toString in class Object