KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > processing > AbstractManualProcessor


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.util.Set JavaDoc;
7
8 import spoon.reflect.Factory;
9 import spoon.reflect.declaration.CtElement;
10 import spoon.support.util.RtHelper;
11
12 /**
13  * This class defines an abstract processor to be subclassed by the user for
14  * defining new manual processors. A manual processor should overload the init
15  * method (called once) and scan the meta-model manually.
16  */

17 public abstract class AbstractManualProcessor implements Processor<CtElement> {
18
19     Factory factory;
20
21     private ProcessorProperties props;
22
23     /**
24      * Empty constructor only for all processors (invoked by Spoon).
25      */

26     @SuppressWarnings JavaDoc("unchecked")
27     public AbstractManualProcessor() {
28     }
29
30     /**
31      * Invalid method in this context.
32      */

33     protected void addProcessedElementType(
34             Class JavaDoc<? extends CtElement> elementType) {
35     }
36
37     public Environment getEnvironment() {
38         return getFactory().getEnvironment();
39     }
40
41     final public Factory getFactory() {
42         return this.factory;
43     }
44
45     /**
46      * Invalid method in this context.
47      */

48     public final Set JavaDoc<Class JavaDoc<? extends CtElement>> getProcessedElementTypes() {
49         return null;
50     }
51
52     private ProcessorProperties getProps() {
53         if (props == null) {
54             try {
55                 props = getFactory().getEnvironment().getProcessorProperties(
56                         getClass().getName());
57             } catch (FileNotFoundException JavaDoc e) {
58                 getFactory().getEnvironment().report(
59                         this,
60                         Severity.MESSAGE,
61                         "Property file not found for processor "
62                                 + getClass().getName());
63             } catch (IOException JavaDoc e) {
64                 getFactory().getEnvironment().report(
65                         this,
66                         Severity.ERROR,
67                         "Wrong properties file format for processor "
68                                 + getClass().getName());
69             }
70         }
71         return props;
72     }
73
74     /**
75      * Invalid method in this context.
76      */

77     public final TraversalStrategy getTraversalStrategy() {
78         return TraversalStrategy.PRE_ORDER;
79     }
80
81     public void init() {
82         loadProperties();
83     }
84
85     /**
86      * Invalid method in this context.
87      */

88     public final boolean isPrivileged() {
89         return false;
90     }
91
92     /**
93      * Always returns false in this context.
94      */

95     public final boolean isToBeProcessed(CtElement candidate) {
96         return false;
97     }
98
99     private void loadProperties() {
100         if (getProps() != null) {
101             for (Field JavaDoc f : RtHelper.getAllFields(getClass())) {
102                 if (f.isAnnotationPresent(Property.class)) {
103                     Object JavaDoc obj = getProps().get(f.getType(), f.getName());
104                     if (obj != null) {
105                         f.setAccessible(true);
106                         try {
107                             f.set(this, obj);
108                         } catch (Exception JavaDoc e) {
109                             e.printStackTrace();
110                         }
111                     } else {
112                         getFactory().getEnvironment().report(
113                                 this,
114                                 Severity.WARNING,
115                                 "No value found for property " + f.getName()
116                                         + " in processor "
117                                         + getClass().getName());
118                     }
119                 }
120             }
121         }
122     }
123
124     /**
125      * Does nothing in this context.
126      */

127     public final void process(CtElement element) {
128     }
129
130     public void processingDone() {
131         // do nothing by default
132
}
133
134     public final void setFactory(Factory factory) {
135         this.factory = factory;
136     }
137
138 }
Popular Tags