1 package spoon.support.visitor; 2 3 import java.util.ArrayList ; 4 import java.util.Collection ; 5 import java.util.ConcurrentModificationException ; 6 7 import spoon.processing.Processor; 8 import spoon.processing.TraversalStrategy; 9 import spoon.reflect.Factory; 10 import spoon.reflect.declaration.CtElement; 11 import spoon.reflect.visitor.CtScanner; 12 13 17 public class ProcessingVisitor extends CtScanner { 18 19 Factory factory; 20 21 Processor processor; 22 23 26 public ProcessingVisitor(Factory factory) { 27 this.factory = factory; 28 } 29 30 @SuppressWarnings ("unchecked") 31 private boolean canBeProcessed(Processor p, CtElement e) { 32 if (p.getProcessedElementTypes() != null) { 33 for (Object o : p.getProcessedElementTypes()) { 34 if (((Class ) o).isAssignableFrom(e.getClass())) { 35 return true; 36 } 37 } 38 } 39 return false; 40 } 41 42 public Processor getProcessor() { 43 return processor; 44 } 45 46 @Override 47 public void scan(Collection <? extends CtElement> elements) { 48 if ((elements != null)) { 49 for (CtElement e : new ArrayList <CtElement>(elements)) { 52 scan(e); 53 } 54 } 55 } 56 57 63 @SuppressWarnings ("unchecked") 64 public void scan(CtElement e) { 65 if (e == null) { 66 return; 67 } 68 if (processor.getTraversalStrategy() == TraversalStrategy.PRE_ORDER 70 && canBeProcessed(processor, e)) { 71 if (processor.isToBeProcessed(e)) { 72 processor.process(e); 73 } 74 } 75 if (!factory.getEnvironment().isProcessingStopped()) { 79 try { 80 super.scan(e); 81 } catch (ConcurrentModificationException e1) { 82 this.scan(e); 83 } 84 if (processor.getTraversalStrategy() == TraversalStrategy.POST_ORDER 86 && canBeProcessed(processor, e)) { 87 if (processor.isToBeProcessed(e)) { 88 processor.process(e); 89 } 90 } 91 } 95 } 96 97 public void setProcessor(Processor processor) { 98 this.processor = processor; 99 } 100 } 101 | Popular Tags |