KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > aval > processing > AnnotationProcessingMapProcessor


1 package spoon.aval.processing;
2
3 import java.awt.Color JavaDoc;
4 import java.awt.Component JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.swing.JFrame JavaDoc;
11 import javax.swing.JLabel JavaDoc;
12 import javax.swing.JScrollPane JavaDoc;
13 import javax.swing.JTree JavaDoc;
14 import javax.swing.event.TreeModelListener JavaDoc;
15 import javax.swing.tree.TreeCellRenderer JavaDoc;
16 import javax.swing.tree.TreeModel JavaDoc;
17 import javax.swing.tree.TreePath JavaDoc;
18
19 import spoon.processing.AbstractProcessor;
20 import spoon.processing.Severity;
21 import spoon.reflect.declaration.CtAnnotationType;
22 import spoon.reflect.declaration.CtElement;
23 import spoon.reflect.declaration.CtExecutable;
24 import spoon.reflect.declaration.CtField;
25 import spoon.reflect.declaration.CtPackage;
26 import spoon.reflect.declaration.CtSimpleType;
27 import spoon.reflect.declaration.SourcePosition;
28 import spoon.reflect.reference.CtExecutableReference;
29 import spoon.reflect.reference.CtReference;
30 import spoon.reflect.reference.CtTypeReference;
31 import spoon.reflect.visitor.CtStackScanner;
32
33 /**
34  * This processor maps annotations with their corresponding uses in a given
35  * package
36  * <p>
37  * ********************* UNSTABLE ***************************
38  * <p>
39  */

40 public class AnnotationProcessingMapProcessor extends
41         AbstractProcessor<CtAnnotationType<?>> {
42
43     private List JavaDoc<CtPackage> framework;
44
45     private List JavaDoc<APPoint> report;
46
47     @Override JavaDoc
48     public void init() {
49         super.init();
50         framework = new ArrayList JavaDoc<CtPackage>();
51         framework.addAll(getFactory().Package().getAll());
52         framework.remove(getFactory().Package()
53                 .get("org.hibernate.annotations"));
54         framework.remove(getFactory().Package().get("org.hibernate.validator"));
55         // System.out.println(framework);
56
// framework.add(getFactory().Package().getCreatedPackage("spoon.aval.processing"));
57
// framework.add(getFactory().Package().getCreatedPackage("org.objectweb.fractal.fraclet.annotation.processor"));
58
report = new ArrayList JavaDoc<APPoint>();
59     }
60
61     public void process(CtAnnotationType<?> element) {
62         APPoint point = new APPoint();
63         point.ap = element;
64         point.apPosition = checkTypeUse(element);
65         point.attribsPosition = new HashMap JavaDoc<CtField<?>, List JavaDoc<CtElement>>();
66         List JavaDoc<CtField<?>> fields = element.getFields();
67
68         for (CtField<?> field : fields) {
69             point.attribsPosition.put(field, checkAccess(field));
70         }
71         report.add(point);
72     }
73
74     private List JavaDoc<CtElement> checkAccess(final CtField<?> field) {
75         final boolean[] found = new boolean[1];
76         final List JavaDoc<CtElement> users = new ArrayList JavaDoc<CtElement>();
77         found[0] = false;
78         for (CtPackage pack : framework) {
79             new CtStackScanner() {
80                 @Override JavaDoc
81                 public void scan(CtReference reference) {
82                     if (reference instanceof CtExecutableReference) {
83                         CtExecutableReference ref = (CtExecutableReference) reference;
84                         if (ref.getSimpleName().equals(field.getSimpleName())
85                                 && ref.getDeclaringType()
86                                         .equals(
87                                                 field.getDeclaringType()
88                                                         .getReference())
89                                 && this.elementStack.peek().getParent(
90                                         CtExecutable.class) != null) {
91                             users.add(this.elementStack.peek());
92                         }
93                     }
94                     super.scan(reference);
95                 }
96             }.scan(pack);
97         }
98
99         return users;
100     }
101
102     private List JavaDoc<CtElement> checkTypeUse(final CtAnnotationType<?> element) {
103         final List JavaDoc<CtElement> users = new ArrayList JavaDoc<CtElement>();
104         for (CtPackage pack : framework) {
105             new CtStackScanner() {
106                 @Override JavaDoc
107                 public void scan(CtReference reference) {
108                     if (reference instanceof CtTypeReference<?>) {
109                         CtTypeReference<?> ref = (CtTypeReference<?>) reference;
110                         if (ref.equals(element.getReference())
111                                 && this.elementStack.peek().getParent(
112                                         CtExecutable.class) != null) {
113                             if (!users.contains(this.elementStack.peek()))
114                                 users.add(this.elementStack.peek());
115                         }
116                     }
117                     super.scan(reference);
118                 }
119
120             }.scan(pack);
121         }
122         return users;
123     }
124
125     @Override JavaDoc
126     public void processingDone() {
127         super.processingDone();
128         // new ReportFrame(report).setVisible(true);
129
for (APPoint point : report) {
130             point.ap.getFactory().getEnvironment().report(this,
131                     Severity.MESSAGE, point.ap,
132                     "Annotation " + point.ap.getSimpleName());
133             for (CtElement p : point.apPosition) {
134                 p.getFactory().getEnvironment().report(this, Severity.MESSAGE,
135                         p, "is processed ");
136             }
137         }
138     }
139 }
140
141 class APPoint {
142     CtAnnotationType ap;
143
144     List JavaDoc<CtElement> apPosition;
145
146     Map JavaDoc<CtField<?>, List JavaDoc<CtElement>> attribsPosition;
147 }
148
149 class ReportFrame extends JFrame JavaDoc {
150     /**
151      * generated ID
152      */

153     private static final long serialVersionUID = -5118768893183502357L;
154
155     class Renderer implements TreeCellRenderer JavaDoc {
156
157         JLabel JavaDoc lbl = new JLabel JavaDoc();
158
159         public Component JavaDoc getTreeCellRendererComponent(JTree JavaDoc tree, Object JavaDoc value,
160                 boolean selected, boolean expanded, boolean leaf, int row,
161                 boolean hasFocus) {
162
163             if (selected) {
164                 lbl.setBackground(Color.green);
165             } else {
166                 lbl.setBackground(Color.white);
167             }
168
169             if (value instanceof CtSimpleType) {
170                 CtSimpleType stV = (CtSimpleType) value;
171                 lbl.setText(stV.getSimpleName());
172             } else if (value instanceof CtElement) {
173                 SourcePosition sp = (SourcePosition) ((CtElement) value)
174                         .getPosition();
175                 lbl.setText(sp.getFile() + " : " + sp.getLine());
176             } else if (value instanceof List JavaDoc) {
177                 lbl.setText("Report");
178             } else
179                 lbl.setText(value.toString());
180             return lbl;
181         }
182
183     }
184
185     JTree JavaDoc tree;
186
187     public ReportFrame(List JavaDoc<APPoint> r) {
188         tree = new JTree JavaDoc();
189         tree.setModel(new ReportModel(r));
190         tree.setCellRenderer(new Renderer JavaDoc());
191         JScrollPane JavaDoc jsp = new JScrollPane JavaDoc(tree);
192         tree.setScrollsOnExpand(true);
193         // DefaultTreeSelectionModel tsm = new DefaultTreeSelectionModel();
194
this.getContentPane().add(jsp);
195         this.setSize(640, 480);
196     }
197
198     class ReportModel implements TreeModel JavaDoc {
199         List JavaDoc<APPoint> rep;
200
201         List JavaDoc<TreeModelListener JavaDoc> list;
202
203         public ReportModel(List JavaDoc<APPoint> r) {
204             super();
205             rep = r;
206             list = new ArrayList JavaDoc<TreeModelListener JavaDoc>();
207
208         }
209
210         public void addTreeModelListener(TreeModelListener JavaDoc l) {
211             list.add(l);
212         }
213
214         public Object JavaDoc getChild(Object JavaDoc parent, int index) {
215             if (parent instanceof List JavaDoc) {
216                 return rep.get(index).ap;
217             }
218             for (APPoint point : rep) {
219                 if (point.ap.equals(parent)) {
220                     return point.apPosition.get(index);
221                 }
222                 // for (iterable_type iterable_element : iterable) {
223
//
224
// }
225
}
226             return null;
227         }
228
229         public int getChildCount(Object JavaDoc parent) {
230             if (parent instanceof List JavaDoc) {
231                 return rep.size();
232             }
233             for (APPoint point : rep) {
234                 if (point.ap.equals(parent)) {
235                     return point.apPosition.size();
236                 }
237                 // for (iterable_type iterable_element : iterable) {
238
//
239
// }
240
}
241             return 0;
242         }
243
244         public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
245             for (APPoint point : rep) {
246                 if (point.ap.equals(parent)) {
247                 }
248                 // for (iterable_type iterable_element : iterable) {
249
//
250
// }
251
}
252             return 0;
253         }
254
255         public Object JavaDoc getRoot() {
256             // TODO Auto-generated method stub
257
return rep;
258         }
259
260         public boolean isLeaf(Object JavaDoc node) {
261             return node instanceof SourcePosition;
262         }
263
264         public void removeTreeModelListener(TreeModelListener JavaDoc l) {
265             list.remove(l);
266         }
267
268         public void valueForPathChanged(TreePath JavaDoc path, Object JavaDoc newValue) {
269             // TODO Auto-generated method stub
270

271         }
272
273     }
274 }
275
Popular Tags