public void processAnnotations(Object instance) throws AnnotationException {
// process class annotations
Class clazz = instance.getClass();
Iterator iter = this.handlers.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Class annotationType = (Class) entry.getKey();
AnnotationHandler handler = (AnnotationHandler) entry.getValue();
if (clazz.isAnnotationPresent(annotationType)) {
Annotation annotation = clazz.getAnnotation(annotationType);
handler.processClassAnnotation(instance, clazz, annotation);
}
}
// process fields annotations
Field[] fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
iter = this.handlers.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Class annotationType = (Class) entry.getKey();
AnnotationHandler handler = (AnnotationHandler) entry
.getValue();
if (fields[i].isAnnotationPresent(annotationType)) {
Annotation annotation = fields[i]
.getAnnotation(annotationType);
handler.processFieldAnnotation(instance, fields[i],
annotation);
}
}
}
// process method annotations
Method[] methods = clazz.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
iter = this.handlers.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Class annotationType = (Class) entry.getKey();
AnnotationHandler handler = (AnnotationHandler) entry
.getValue();
if (methods[i].isAnnotationPresent(annotationType)) {
Annotation annotation = methods[i]
.getAnnotation(annotationType);
handler.processMethodAnnotation(instance, methods[i],
annotation);
}
}
}
}
|