KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > processing > AbstractAnnotationProcessor


1 package spoon.processing;
2
3 import java.lang.annotation.Annotation JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.Set JavaDoc;
8 import java.util.TreeMap JavaDoc;
9 import java.util.TreeSet JavaDoc;
10
11 import spoon.reflect.declaration.CtAnnotation;
12 import spoon.reflect.declaration.CtElement;
13
14 /**
15  * This class defines an abstract annotation processor to be subclassed by the
16  * user for defining new annotation processors.
17  */

18 public abstract class AbstractAnnotationProcessor<A extends Annotation JavaDoc, E extends CtElement>
19         extends AbstractProcessor<E> implements AnnotationProcessor<A, E> {
20
21     Map JavaDoc<String JavaDoc, Class JavaDoc<? extends A>> consumedAnnotationTypes = new TreeMap JavaDoc<String JavaDoc, Class JavaDoc<? extends A>>();
22
23     Map JavaDoc<String JavaDoc, Class JavaDoc<? extends A>> processedAnnotationTypes = new TreeMap JavaDoc<String JavaDoc, Class JavaDoc<? extends A>>();
24
25     /**
26      * Empty constructor only for all processors (invoked by Spoon).
27      */

28     @SuppressWarnings JavaDoc("unchecked")
29     public AbstractAnnotationProcessor() {
30         if (inferConsumedAnnotationType()) {
31             for (Method JavaDoc m : getClass().getMethods()) {
32                 if (m.getName().equals("process")
33                         && m.getParameterTypes().length == 2) {
34                     Class JavaDoc c = m.getParameterTypes()[0];
35                     if (Annotation JavaDoc.class != c) {
36                         addConsumedAnnotationType((Class JavaDoc<? extends A>) m
37                                 .getParameterTypes()[0]);
38                     }
39                 }
40             }
41             if (processedAnnotationTypes.isEmpty()) {
42                 addProcessedAnnotationType((Class JavaDoc<? extends A>) Annotation JavaDoc.class);
43             }
44         }
45     }
46
47     /**
48      * Adds a consumed annotation type (to be used in subclasses constructors).
49      * A consumed annotation type is also part of the processed annotation
50      * types.
51      */

52     final protected void addConsumedAnnotationType(
53             Class JavaDoc<? extends A> annotationType) {
54         addProcessedAnnotationType(annotationType);
55         consumedAnnotationTypes.put(annotationType.getName(), annotationType);
56     }
57
58     /**
59      * Adds a processed annotation type (to be used in subclasses constructors).
60      */

61     final protected void addProcessedAnnotationType(
62             Class JavaDoc<? extends A> annotationType) {
63         processedAnnotationTypes.put(annotationType.getName(), annotationType);
64     }
65
66     /**
67      * Removes a processed annotation type.
68      */

69     final protected void removeProcessedAnnotationType(Class JavaDoc<? extends A> annotationType) {
70         processedAnnotationTypes.remove(annotationType.getName());
71     }
72
73     /**
74      * Clears the processed annotation types.
75      */

76     final protected void clearProcessedAnnotationTypes() {
77         processedAnnotationTypes.clear();
78     }
79
80     /**
81      * Clears the consumed annotation types.
82      */

83     final protected void clearConsumedAnnotationTypes() {
84         consumedAnnotationTypes.clear();
85     }
86
87     /**
88      * Removes a processed annotation type.
89      */

90     final protected void removeConsumedAnnotationType(Class JavaDoc<? extends A> annotationType) {
91         consumedAnnotationTypes.remove(annotationType.getName());
92     }
93     
94     final public Set JavaDoc<Class JavaDoc<? extends A>> getConsumedAnnotationTypes() {
95         return new TreeSet JavaDoc<Class JavaDoc<? extends A>>(consumedAnnotationTypes.values());
96     }
97
98     final public Set JavaDoc<Class JavaDoc<? extends A>> getProcessedAnnotationTypes() {
99         return new TreeSet JavaDoc<Class JavaDoc<? extends A>>(processedAnnotationTypes
100                 .values());
101     }
102
103     public boolean inferConsumedAnnotationType() {
104         return true;
105     }
106
107     /**
108      * Returns true if the element is annotated with an annotation whose type is
109      * processed.
110      */

111     final public boolean isToBeProcessed(E element) {
112         if (element != null && element.getAnnotations() != null) {
113             for (CtAnnotation<? extends Annotation JavaDoc> a : element
114                     .getAnnotations()) {
115                 if (shoudBeProcessed(a))
116                     return true;
117             }
118         }
119         return false;
120     }
121
122     @SuppressWarnings JavaDoc("unchecked")
123     final public void process(E element) {
124         for (CtAnnotation<? extends Annotation JavaDoc> annotation : new ArrayList JavaDoc<CtAnnotation>(
125                 element.getAnnotations())) {
126             if (shoudBeProcessed(annotation)) {
127                 try {
128                     process((A) annotation.getActualAnnotation(), element);
129                 } catch (Exception JavaDoc e) {
130                     e.printStackTrace();
131                 }
132                 if (shoudBeConsumed(annotation))
133                     element.getAnnotations().remove(annotation);
134             }
135         }
136     }
137
138     private boolean shoudBeConsumed(
139             CtAnnotation<? extends Annotation JavaDoc> annotation) {
140         if (consumedAnnotationTypes.containsKey(annotation.getAnnotationType()
141                 .getQualifiedName())) {
142             return true;
143         }
144         return false;
145     }
146
147     private boolean shoudBeProcessed(
148             CtAnnotation<? extends Annotation JavaDoc> annotation) {
149         if (processedAnnotationTypes.containsKey(annotation.getAnnotationType()
150                 .getQualifiedName())) {
151             return true;
152         }
153         return false;
154     }
155
156 }
157
Popular Tags