Class ChangeCollector

java.lang.Object
spoon.support.modelobs.ChangeCollector
Direct Known Subclasses:
SourceFragmentCreator

public class ChangeCollector extends Object
Listens on changes on the spoon model and remembers them
  • Constructor Details

    • ChangeCollector

      public ChangeCollector()
  • Method Details

    • getChangeCollector

      public static ChangeCollector getChangeCollector(Environment env)
      Parameters:
      env - to be checked Environment
      Returns:
      ChangeCollector attached to the `env` or null if there is none
    • runWithoutChangeListener

      public static void runWithoutChangeListener(Environment env, Runnable runnable)
      Allows to run code using change collector switched off. It means that any change of spoon model done by the `runnable` is ignored by the change collector. Note: it is actually needed to wrap CtElement#toString() calls which sometime modifies spoon model. See TestSniperPrinter#testPrintChangedReferenceBuilder()
      Parameters:
      env - Spoon environment
      runnable - the code to be run
    • attachTo

      public ChangeCollector attachTo(Environment env)
      Attaches itself to CtModel to listen to all changes of it's child elements TODO: it would be nicer if we might listen on changes on CtElement
      Parameters:
      env - to be attached to Environment
      Returns:
      this to support fluent API
    • getDirectChanges

      public Set<CtRole> getDirectChanges(CtElement currentElement)
      Parameters:
      currentElement - the CtElement whose changes has to be checked
      Returns:
      set of CtRoles in direct children on of `currentElement` where something has changed since this ChangeCollector was attached The 'directly' means that value of attribute of `currentElement` was changed. Use getChanges(CtElement) to detect changes in child elements too
    • getChanges

      public Set<CtRole> getChanges(CtElement currentElement)
      Return the set of CtRoles for which children have changed from `currentElement` since this ChangeCollector was attached Warning: change in IMPLICIT are ignored
      Parameters:
      currentElement - the CtElement whose changes has to be checked
    • onChange

      protected void onChange(CtElement currentElement, CtRole role)
      Called whenever anything changes in the spoon model
      Parameters:
      currentElement - the modified element
      role - the modified attribute of that element