KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > common > annotation > AnnotationProcessor


1 package org.objectweb.celtix.common.annotation;
2
3 import java.lang.annotation.Annotation JavaDoc;
4 import java.lang.reflect.AnnotatedElement JavaDoc;
5 import java.lang.reflect.Field JavaDoc;
6 import java.lang.reflect.InvocationTargetException JavaDoc;
7 import java.lang.reflect.Method JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.logging.Level JavaDoc;
10 import java.util.logging.Logger JavaDoc;
11
12 import org.objectweb.celtix.common.i18n.Message;
13 import org.objectweb.celtix.common.logging.LogUtils;
14
15
16 /** Process instance of an annotated class. This is a visitable
17  * object that allows an caller to visit that annotated elements in
18  * this class definition. If a class level annotation is overridden
19  * by a member level annotation, only the visit method for the member
20  * level annotation
21  */

22 public class AnnotationProcessor {
23     
24     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(AnnotationProcessor.class);
25     
26     private static Method JavaDoc visitClassMethod;
27     private static Method JavaDoc visitFieldMethod;
28     private static Method JavaDoc visitMethodMethod;
29     
30     static {
31         try {
32             visitClassMethod = AnnotationVisitor.class.getMethod("visitClass", Class JavaDoc.class, Annotation JavaDoc.class);
33             visitFieldMethod = AnnotationVisitor.class.getMethod("visitField", Field JavaDoc.class, Annotation JavaDoc.class);
34             visitMethodMethod = AnnotationVisitor.class.getMethod("visitMethod",
35                                                                   Method JavaDoc.class, Annotation JavaDoc.class);
36             
37         } catch (NoSuchMethodException JavaDoc e) {
38             // ignore
39
}
40         
41     }
42     
43     private final Object JavaDoc target;
44     private List JavaDoc<Class JavaDoc<? extends Annotation JavaDoc>> annotationTypes;
45     
46     
47     public AnnotationProcessor(Object JavaDoc o) {
48         if (o == null) {
49             throw new IllegalArgumentException JavaDoc(new Message("INVALID_CTOR_ARGS", LOG).toString());
50         }
51         target = o;
52     }
53     
54     /**
55      * Visits each of the annotated elements of the object.
56      *
57      * @param visitor a visitor
58      *
59      */

60     public void accept(AnnotationVisitor visitor) {
61         
62         if (visitor == null) {
63             throw new IllegalArgumentException JavaDoc();
64         }
65         
66         annotationTypes = visitor.getTargetAnnotations();
67         visitor.setTarget(target);
68         processClass(visitor);
69         processFields(visitor);
70         processMethods(visitor);
71     }
72     
73     
74     private void processMethods(AnnotationVisitor visitor) {
75         
76         visitAnnotatedElement(target.getClass().getDeclaredMethods(), visitor, visitMethodMethod);
77     }
78     
79     private void processFields(AnnotationVisitor visitor) {
80         
81         visitAnnotatedElement(target.getClass().getDeclaredFields(), visitor, visitFieldMethod);
82     }
83     
84     
85     private void processClass(AnnotationVisitor visitor) {
86         Class JavaDoc<?>[] classes = {target.getClass()};
87         visitAnnotatedElement(classes, visitor, visitClassMethod);
88     }
89     
90     private <U extends AnnotatedElement JavaDoc> void visitAnnotatedElement(U[] elements,
91                                                                     AnnotationVisitor visitor,
92                                                                     Method JavaDoc visitorMethod) {
93         
94         for (U element : elements) {
95             for (Class JavaDoc<? extends Annotation JavaDoc> clz : annotationTypes) {
96                 Annotation JavaDoc ann = element.getAnnotation(clz);
97                 if (ann != null) {
98                     try {
99                         visitorMethod.invoke(visitor, element, ann);
100                     } catch (IllegalAccessException JavaDoc e) {
101                         // ignore, we're invoking methods of a public interface
102
} catch (InvocationTargetException JavaDoc e) {
103                         Throwable JavaDoc cause = e.getCause() == null ? e : e.getCause();
104                         LogUtils.log(LOG, Level.SEVERE, "VISITOR_RAISED_EXCEPTION", cause, visitor);
105                     }
106                 }
107             }
108         }
109     }
110 }
111
Popular Tags