Class ElementNode

java.lang.Object
spoon.pattern.internal.node.AbstractNode
spoon.pattern.internal.node.ElementNode
All Implemented Interfaces:
Matchers, PrimitiveMatcher, RepeatableMatcher, RootNode

public class ElementNode extends AbstractNode
Generates/Matches a copy of a single CtElement AST node with all it's children (whole AST tree of the root CtElement)
  • Constructor Details

    • ElementNode

      public ElementNode(MetamodelConcept elementType, CtElement templateElement)
      Parameters:
      elementType - The type of Spoon node which has to be generated/matched by this ElementNode
      templateElement - - optional ref to template element which was used to created this ElementNode. It is used e.g. to generate generatedBy comment
  • Method Details

    • create

      public static ElementNode create(CtElement element, Map<CtElement,​RootNode> patternElementToSubstRequests)
      Creates an implicit ElementNode, which contains all non derived attributes of `element` and all it's children
      Parameters:
      element - source element, which is used to initialize ElementNode
      patternElementToSubstRequests - the Map, which will receive mapping between `element` and it's children and newly created tree of ElementNodes
      Returns:
      a tree of ElementNodes, which reflects tree of `element`
    • create

      public static ListOfNodes create(List<?> objects, Map<CtElement,​RootNode> patternElementToSubstRequests)
      Same like create(CtElement, Map) but with List of elements or primitive objects
      Parameters:
      objects - List of objects which has to be transformed to nodes
      patternElementToSubstRequests - mapping between CtElement from `objects` to created `node`
      Returns:
      a list of trees of nodes, which reflects list of `objects`
    • create

      public static ListOfNodes create(Set<?> templates, Map<CtElement,​RootNode> patternElementToSubstRequests)
      Same like create(CtElement, Map) but with Set of elements or primitive objects
      Parameters:
      templates - Set of objects which has to be transformed to nodes
      patternElementToSubstRequests - mapping between CtElement from `templates` to created `node`
      Returns:
      a list of trees of nodes, which reflects Set of `templates`
    • create

      public static ListOfNodes create(Map<String,​?> map, Map<CtElement,​RootNode> patternElementToSubstRequests)
      Same like create(CtElement, Map) but with Map of String to elements or primitive objects
      Parameters:
      map - Map of objects which has to be transformed to nodes
      patternElementToSubstRequests - mapping between CtElement from `map` to created `node`
      Returns:
      a list of MapEntryNodes, which reflects `map`
    • replaceNode

      public boolean replaceNode(RootNode oldNode, RootNode newNode)
      Description copied from interface: RootNode
      Call it to modify Pattern structure. It is actually called mainly by PatternBuilder. TODO: May be we can move this method into some internal interface?
      Parameters:
      oldNode - old RootNode
      newNode - new RootNode
      Returns:
      a true if `oldNode` was found in this RootNode or it's children and replaced by `newNode` false if `oldNode` was not found
    • getRoleToNode

      public Map<MetamodelProperty,​RootNode> getRoleToNode()
    • getNodeOfRole

      public RootNode getNodeOfRole(CtRole attributeRole)
    • setNodeOfRole

      public RootNode setNodeOfRole(CtRole role, RootNode newAttrNode)
    • getOrCreateNodeOfRole

      public RootNode getOrCreateNodeOfRole(CtRole role, Map<CtElement,​RootNode> patternElementToSubstRequests)
      Parameters:
      role -
      Returns:
      a RootNode, which exists on the `role` or creates implicit container for that role
    • getValueOfRole

      public <T> T getValueOfRole(CtRole role, Class<T> type)
      Parameters:
      role - to be returned CtRole
      type - required type of returned value
      Returns:
      value of ConstantNode on the `role` attribute of this ElementNode or null if there is none or has different type
    • forEachParameterInfo

      public void forEachParameterInfo(BiConsumer<ParameterInfo,​RootNode> consumer)
      Description copied from interface: RootNode
      Calls consumer for each pair of parameter definition (ParameterInfo) and RootNode, which uses it
      Parameters:
      consumer - the receiver of pairs of ParameterInfo and RootNode
    • generateTargets

      public <U> void generateTargets(DefaultGenerator generator, ResultHolder<U> result, ImmutableMap parameters)
      Description copied from interface: RootNode
      Generates zero, one or more target depending on kind of this RootNode, expected `result` and input `parameters`
      Parameters:
      generator - Generator which drives generation process
      result - holder for the generated objects
      parameters - a ImmutableMap holding parameters
    • generateSingleNodeAttributes

      protected void generateSingleNodeAttributes(DefaultGenerator generator, CtElement clone, ImmutableMap parameters)
    • matchTarget

      public ImmutableMap matchTarget(Object target, ImmutableMap parameters)
      Parameters:
      target - - to be matched element
      parameters - will receive the matching parameter values
      Returns:
      true if `element` matches with pattern of this matcher
    • matchesRole

      protected ImmutableMap matchesRole(ImmutableMap parameters, CtElement target, MetamodelProperty mmField, RootNode attrNode)
    • toString

      public String toString()
      Overrides:
      toString in class AbstractNode
    • getElementType

      public MetamodelConcept getElementType()
    • setElementType

      public void setElementType(MetamodelConcept elementType)
    • getMatchingStrategy

      public Quantifier getMatchingStrategy()
      Description copied from interface: RepeatableMatcher
      If two RepeatableMatchers in a list are matching the same element, then returned Quantifier defines how resolve this conflict
      Returns:
      Quantifier
    • isTryNextMatch

      public boolean isTryNextMatch(ImmutableMap parameters)
      Parameters:
      parameters - matching parameters
      Returns:
      true if this ValueResolver should be processed again to match next target in the state defined by current `parameters`.
    • matchAllWith

      public TobeMatched matchAllWith(TobeMatched tobeMatched)
      Description copied from interface: Matchers
      Matches all matchers of this Matchers instance with `targets`
      Specified by:
      matchAllWith in interface Matchers
      Specified by:
      matchAllWith in interface RootNode
      Parameters:
      tobeMatched - to be matched target nodes and input parameters
      Returns:
      TobeMatched with targets which remained after all RootNodes were matched + matched parameters
    • matchTargets

      public TobeMatched matchTargets(TobeMatched targets, Matchers next)
      Specified by:
      matchTargets in interface RootNode
      Parameters:
      targets - to be matched target nodes and input parameters
      next - Chain of matchers which has to be processed after this RootNode
      Returns:
      new parameters and container with remaining targets