KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > visitor > ProcessingVisitor


1 package spoon.support.visitor;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Collection JavaDoc;
5 import java.util.ConcurrentModificationException JavaDoc;
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 /**
14  * This visitor implements the code processing engine.
15  *
16  */

17 public class ProcessingVisitor extends CtScanner {
18
19     Factory factory;
20
21     Processor processor;
22
23     /**
24      * The constructor.
25      */

26     public ProcessingVisitor(Factory factory) {
27         this.factory = factory;
28     }
29
30     @SuppressWarnings JavaDoc("unchecked")
31     private boolean canBeProcessed(Processor p, CtElement e) {
32         if (p.getProcessedElementTypes() != null) {
33             for (Object JavaDoc o : p.getProcessedElementTypes()) {
34                 if (((Class JavaDoc) 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 JavaDoc
47     public void scan(Collection JavaDoc<? extends CtElement> elements) {
48         if ((elements != null)) {
49             // Nicolas: If you need to change the next line, talk to the boss
50
// first!
51
for (CtElement e : new ArrayList JavaDoc<CtElement>(elements)) {
52                 scan(e);
53             }
54         }
55     }
56
57     /**
58      * Applies the processing to the given element. To apply the processing,
59      * this method upcalls, for all the registered processor in
60      * {@link Factory#Processor }, the {@link Processor#process(CtElement)}
61      * method if {@link Processor#isToBeProcessed(CtElement)} returns true.
62      */

63     @SuppressWarnings JavaDoc("unchecked")
64     public void scan(CtElement e) {
65         if (e == null) {
66             return;
67         }
68         // try {
69
if (processor.getTraversalStrategy() == TraversalStrategy.PRE_ORDER
70                 && canBeProcessed(processor, e)) {
71             if (processor.isToBeProcessed(e)) {
72                 processor.process(e);
73             }
74         }
75         // } catch (Exception ex2) {
76
// ex2.printStackTrace();
77
// }
78
if (!factory.getEnvironment().isProcessingStopped()) {
79             try {
80                 super.scan(e);
81             } catch (ConcurrentModificationException JavaDoc e1) {
82                 this.scan(e);
83             }
84             // try {
85
if (processor.getTraversalStrategy() == TraversalStrategy.POST_ORDER
86                     && canBeProcessed(processor, e)) {
87                 if (processor.isToBeProcessed(e)) {
88                     processor.process(e);
89                 }
90             }
91             // } catch (Exception ex2) {
92
// ex2.printStackTrace();
93
// }
94
}
95     }
96
97     public void setProcessor(Processor processor) {
98         this.processor = processor;
99     }
100 }
101
Popular Tags