KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > util > Annotations


1 package org.jfox.ioc.util;
2
3 import java.lang.annotation.Annotation JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.lang.reflect.Field JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.List JavaDoc;
8
9 /**
10  * @author <a HREF="mailto:yy.young@gmail.com">Young Yang</a>
11  */

12
13 public class Annotations {
14
15     /**
16      * method Ƿ annotation ע
17      */

18     public static boolean isAnnotated(Method JavaDoc method, Class JavaDoc<? extends Annotation JavaDoc> annotation) {
19         return method.isAnnotationPresent(annotation);
20     }
21
22     public static Method JavaDoc[] getAnnotatedMethods(Class JavaDoc clz, Class JavaDoc<? extends Annotation JavaDoc> annotation){
23         List JavaDoc<Method JavaDoc> methods = new ArrayList JavaDoc<Method JavaDoc>();
24         Method JavaDoc[] allMethods = clz.getMethods();
25         for(int i=0; i<allMethods.length; i++) {
26             if(isAnnotated(allMethods[i], annotation)) {
27                 methods.add(allMethods[i]);
28             }
29         }
30         return methods.toArray(new Method JavaDoc[methods.size()]);
31     }
32
33     public static Method JavaDoc[] getAnnotatedSetMethods(Class JavaDoc clz, Class JavaDoc<? extends Annotation JavaDoc> annotation){
34         List JavaDoc<Method JavaDoc> methods = new ArrayList JavaDoc<Method JavaDoc>();
35         Method JavaDoc[] allMethods = clz.getMethods();
36         for(int i=0; i<allMethods.length; i++) {
37             if(Methods.isSetMethod(allMethods[i]) && isAnnotated(allMethods[i], annotation)) {
38                 methods.add(allMethods[i]);
39             }
40         }
41         return methods.toArray(new Method JavaDoc[methods.size()]);
42     }
43
44     public static Method JavaDoc[] getAnnotatedGetMethods(Class JavaDoc clz, Class JavaDoc<? extends Annotation JavaDoc> annotation){
45         List JavaDoc<Method JavaDoc> methods = new ArrayList JavaDoc<Method JavaDoc>();
46         Method JavaDoc[] allMethods = clz.getMethods();
47         for(int i=0; i<allMethods.length; i++) {
48             if(Methods.isGetMethod(allMethods[i]) && isAnnotated(allMethods[i], annotation)) {
49                 methods.add(allMethods[i]);
50             }
51         }
52         return methods.toArray(new Method JavaDoc[methods.size()]);
53     }
54
55     public static boolean hasAnnotatedMethod(Class JavaDoc clz, Class JavaDoc<? extends Annotation JavaDoc> annotation){
56         Method JavaDoc[] allMethods = clz.getMethods();
57         for(int i=0; i<allMethods.length; i++) {
58             if(isAnnotated(allMethods[i], annotation)) {
59                return true;
60             }
61         }
62         return false;
63     }
64
65     public static boolean isAnnotated(Field JavaDoc field,
66                                       Class JavaDoc<? extends Annotation JavaDoc> annotation) {
67         return field.isAnnotationPresent(annotation);
68     }
69
70     public static void main(String JavaDoc[] args) {
71
72     }
73 }
74
75
Popular Tags