KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > RuntimeProcessingManager


1 package spoon.support;
2
3 import java.util.Collection JavaDoc;
4 import java.util.LinkedList JavaDoc;
5 import java.util.List JavaDoc;
6
7 import spoon.processing.ProcessingManager;
8 import spoon.processing.Processor;
9 import spoon.processing.Severity;
10 import spoon.reflect.Factory;
11 import spoon.reflect.declaration.CtElement;
12 import spoon.reflect.declaration.CtNamedElement;
13 import spoon.support.util.Timer;
14 import spoon.support.visitor.ProcessingVisitor;
15
16 /**
17  * This processing manager implements a blocking processing policy that consists
18  * of applying the processors in a FIFO order until no processors remain to be
19  * applied.
20  */

21 public class RuntimeProcessingManager implements ProcessingManager {
22     Processor<?> current;
23
24     Factory factory;
25
26     List JavaDoc<Processor> processors;
27
28     ProcessingVisitor visitor;
29
30     /**
31      * Creates a new processing manager that maintains a queue of processors to
32      * be applied to a given factory.
33      *
34      * @param factory
35      * the factory on which the processing applies (contains the
36      * meta-model)
37      */

38     public RuntimeProcessingManager(Factory factory) {
39         super();
40         setFactory(factory);
41     }
42
43     public void addProcessor(Class JavaDoc<? extends Processor> type) {
44         try {
45             Processor<?> p = type.newInstance();
46             p.setFactory(factory);
47             p.init();
48             addProcessor(p);
49         } catch (Exception JavaDoc e) {
50             factory
51                     .getEnvironment()
52                     .report(
53                             null,
54                             Severity.ERROR,
55                             "Unable to instantiate processor \""
56                                     + type.getName()
57                                     + "\" - Your processor should have a constructor with no arguments");
58         }
59     }
60
61     public boolean addProcessor(Processor p) {
62         p.setFactory(getFactory());
63         return getProcessors().add(p);
64     }
65
66     @SuppressWarnings JavaDoc("unchecked")
67     public void addProcessor(String JavaDoc qualifiedName) {
68         try {
69             addProcessor((Class JavaDoc<? extends Processor>) Class
70                     .forName(qualifiedName));
71         } catch (ClassNotFoundException JavaDoc e) {
72             factory.getEnvironment().report(
73                     null,
74                     Severity.ERROR,
75                     "Unable to load processor \"" + qualifiedName
76                             + "\" - Check your classpath");
77         }
78     }
79
80     public Processor<?> getCurrentProcessor() {
81         return current;
82     }
83
84     public Factory getFactory() {
85         return factory;
86     }
87
88     public List JavaDoc<Processor> getProcessors() {
89         if (processors == null) {
90             processors = new LinkedList JavaDoc<Processor>();
91         }
92         return processors;
93     }
94
95     private ProcessingVisitor getVisitor() {
96         if (visitor == null)
97             visitor = new ProcessingVisitor(getFactory());
98         return visitor;
99     }
100
101     public boolean isToBeApplied(Class JavaDoc<? extends Processor> type) {
102         for (Processor p : getProcessors()) {
103             if (p.getClass() == type) {
104                 return true;
105             }
106         }
107         return false;
108     }
109
110     public void process() {
111         Timer.start("process");
112         process(getFactory().Package().getAllRoots());
113         Timer.stop("process");
114     }
115
116     public void process(Collection JavaDoc<? extends CtElement> elements) {
117         for (Processor p : getProcessors()) {
118             current = p;
119             process(elements, p);
120         }
121     }
122
123     /**
124      * Recursively processes elements and their children with a given processor.
125      */

126     public void process(Collection JavaDoc<? extends CtElement> elements,
127             Processor processor) {
128         getFactory().getEnvironment().debugMessage(
129                 "Processing with " + processor.getClass().getName());
130         current = processor;
131         Timer.start(processor.getClass().getName());
132         for (CtElement e : elements)
133             process(e, processor);
134         Timer.stop(processor.getClass().getName());
135     }
136
137     public void process(CtElement element) {
138         for (Processor p : getProcessors()) {
139             current = p;
140             process(element, p);
141         }
142     }
143
144     /**
145      * Recursively processes an element and its children with a given processor.
146      */

147     public void process(CtElement element, Processor<?> processor) {
148         getFactory()
149                 .getEnvironment()
150                 .debugMessage(
151                         "Processing "
152                                 + ((element instanceof CtNamedElement) ? ((CtNamedElement) element)
153                                         .getSimpleName()
154                                         : element.toString()) + " with "
155                                 + processor.getClass().getName());
156         processor.init();
157         getVisitor().setProcessor(processor);
158         getVisitor().scan(element);
159         processor.processingDone();
160     }
161
162     public void setFactory(Factory factory) {
163         this.factory = factory;
164         factory.getEnvironment().setManager(this);
165     }
166
167 }
168
Popular Tags