KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > processing > AbstractProcessor


1 package spoon.processing;
2
3 import java.io.FileNotFoundException JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.lang.reflect.Field JavaDoc;
6 import java.lang.reflect.Method JavaDoc;
7 import java.util.HashSet JavaDoc;
8 import java.util.Set JavaDoc;
9
10 import spoon.reflect.Factory;
11 import spoon.reflect.declaration.CtClass;
12 import spoon.reflect.declaration.CtElement;
13 import spoon.support.util.RtHelper;
14
15 /**
16  * This class defines an abstract processor to be subclassed by the user for
17  * defining new processors.
18  */

19 public abstract class AbstractProcessor<E extends CtElement> implements
20         Processor<E> {
21
22     Factory factory;
23
24     Set JavaDoc<Class JavaDoc<? extends CtElement>> processedElementTypes = new HashSet JavaDoc<Class JavaDoc<? extends CtElement>>();
25
26     private ProcessorProperties props;
27
28     /**
29      * Empty constructor only for all processors (invoked by Spoon).
30      */

31     @SuppressWarnings JavaDoc("unchecked")
32     public AbstractProcessor() {
33         for (Method JavaDoc m : getClass().getMethods()) {
34             if (m.getName().equals("process")
35                     && m.getParameterTypes().length == 1) {
36                 Class JavaDoc c = m.getParameterTypes()[0];
37                 if (CtElement.class != c) {
38                     addProcessedElementType((Class JavaDoc<? extends CtElement>) m
39                             .getParameterTypes()[0]);
40                 }
41             }
42         }
43         if (processedElementTypes.isEmpty()) {
44             addProcessedElementType(CtElement.class);
45         }
46     }
47
48     /**
49      * Adds a processed element type. This method is typically invoked in
50      * subclasses' constructors.
51      */

52     protected void addProcessedElementType(
53             Class JavaDoc<? extends CtElement> elementType) {
54         processedElementTypes.add(elementType);
55     }
56
57     /**
58      * Clears the processed element types.
59      */

60     protected void clearProcessedElementType() {
61         processedElementTypes.clear();
62     }
63
64     public Environment getEnvironment() {
65         return getFactory().getEnvironment();
66     }
67
68     public Factory getFactory() {
69         return this.factory;
70     }
71
72     public Set JavaDoc<Class JavaDoc<? extends CtElement>> getProcessedElementTypes() {
73         return processedElementTypes;
74     }
75
76     private ProcessorProperties getProps() {
77         if (props == null) {
78             try {
79                 props = getFactory().getEnvironment().getProcessorProperties(
80                         getClass().getName());
81             } catch (FileNotFoundException JavaDoc e) {
82                 getFactory().getEnvironment().report(
83                         this,
84                         Severity.MESSAGE,
85                         "Property file not found for processor "
86                                 + getClass().getName());
87             } catch (IOException JavaDoc e) {
88                 getFactory().getEnvironment().report(
89                         this,
90                         Severity.ERROR,
91                         "Wrong properties file format for processor "
92                                 + getClass().getName());
93             }
94         }
95         return props;
96     }
97
98     public TraversalStrategy getTraversalStrategy() {
99         return TraversalStrategy.PRE_ORDER;
100     }
101
102     public void init() {
103         loadProperties();
104     }
105
106     public boolean isToBeProcessed(E candidate) {
107         if (candidate instanceof CtClass) {
108             if (factory.Template().getAll().containsKey(
109                     ((CtClass) candidate).getQualifiedName())) {
110                 return false;
111             }
112         }
113         return true;
114     }
115
116     private void loadProperties() {
117         if (getProps() != null) {
118             for (Field JavaDoc f : RtHelper.getAllFields(getClass())) {
119                 if (f.isAnnotationPresent(Property.class)) {
120                     Object JavaDoc obj = getProps().get(f.getType(), f.getName());
121                     if (obj != null) {
122                         f.setAccessible(true);
123                         try {
124                             f.set(this, obj);
125                         } catch (Exception JavaDoc e) {
126                             e.printStackTrace();
127                         }
128                     } else {
129                         getFactory().getEnvironment().report(
130                                 this,
131                                 Severity.WARNING,
132                                 "No value found for property " + f.getName()
133                                         + " in processor "
134                                         + getClass().getName());
135                     }
136                 }
137             }
138         }
139     }
140
141     /**
142      * The manual meta-model processing cannot be overloaded (use
143      * {@link AbstractManualProcessor}) to do so.
144      */

145     public final void process() {
146     }
147
148     public void processingDone() {
149         // do nothing by default
150
}
151
152     /**
153      * Removes a processed element type.
154      */

155     protected void removeProcessedElementType(
156             Class JavaDoc<? extends CtElement> elementType) {
157         processedElementTypes.remove(elementType);
158     }
159
160     public final void setFactory(Factory factory) {
161         this.factory = factory;
162     }
163
164 }
165
Popular Tags