001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.util;
008:
009: import java.lang.annotation.Annotation;
010: import java.lang.reflect.Method;
011: import java.lang.reflect.Field;
012: import java.lang.reflect.Constructor;
013: import java.util.ArrayList;
014: import java.util.List;
015:
016: /**
017: * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
018: */
019:
020: public class AnnotationUtils {
021:
022: /**
023: * method 是�被类 annotation 注解
024: *
025: * @param method method
026: * @param annotation annotation class
027: */
028: public static boolean isAnnotated(Method method,
029: Class<? extends Annotation> annotation) {
030: return method.isAnnotationPresent(annotation);
031: }
032:
033: public static Constructor[] getAnnotatedConstructors(Class clazz,
034: Class<? extends Annotation> annotation) {
035: List<Constructor> constructorList = new ArrayList<Constructor>();
036: Constructor[] constructors = clazz.getConstructors();
037: for (Constructor constructor : constructors) {
038: if (constructor.isAnnotationPresent(annotation)) {
039: constructorList.add(constructor);
040: }
041: }
042: return constructorList.toArray(new Constructor[constructorList
043: .size()]);
044: }
045:
046: public static Field[] getAnnotatedFields(Class clazz,
047: Class<? extends Annotation> annotation) {
048: List<Field> fieldList = new ArrayList<Field>();
049: Field[] fields = ClassUtils.getAllDecaredFields(clazz);
050: for (Field field : fields) {
051: if (field.isAnnotationPresent(annotation)) {
052: fieldList.add(field);
053: }
054: }
055: return fieldList.toArray(new Field[fieldList.size()]);
056: }
057:
058: public static Method[] getAnnotatedMethods(Class clz,
059: Class<? extends Annotation> annotation) {
060: List<Method> methods = new ArrayList<Method>();
061: Method[] allMethods = clz.getMethods();
062: for (int i = 0; i < allMethods.length; i++) {
063: if (isAnnotated(allMethods[i], annotation)) {
064: methods.add(allMethods[i]);
065: }
066: }
067: return methods.toArray(new Method[methods.size()]);
068: }
069:
070: public static Method[] getAnnotatedDeclaredMethods(Class clz,
071: Class<? extends Annotation> annotation) {
072: List<Method> methods = new ArrayList<Method>();
073: Method[] allMethods = clz.getDeclaredMethods();
074: for (int i = 0; i < allMethods.length; i++) {
075: if (isAnnotated(allMethods[i], annotation)) {
076: methods.add(allMethods[i]);
077: }
078: }
079: return methods.toArray(new Method[methods.size()]);
080: }
081:
082: public static Method[] getAnnotatedSetMethods(Class clz,
083: Class<? extends Annotation> annotation) {
084: List<Method> methods = new ArrayList<Method>();
085: Method[] allMethods = clz.getMethods();
086: for (int i = 0; i < allMethods.length; i++) {
087: if (MethodUtils.isSetMethod(allMethods[i])
088: && isAnnotated(allMethods[i], annotation)) {
089: methods.add(allMethods[i]);
090: }
091: }
092: return methods.toArray(new Method[methods.size()]);
093: }
094:
095: public static Method[] getAnnotatedGetMethods(Class clz,
096: Class<? extends Annotation> annotation) {
097: List<Method> methods = new ArrayList<Method>();
098: Method[] allMethods = clz.getMethods();
099: for (int i = 0; i < allMethods.length; i++) {
100: if (MethodUtils.isGetMethod(allMethods[i])
101: && isAnnotated(allMethods[i], annotation)) {
102: methods.add(allMethods[i]);
103: }
104: }
105: return methods.toArray(new Method[methods.size()]);
106: }
107:
108: public static boolean hasAnnotatedMethod(Class clz,
109: Class<? extends Annotation> annotation) {
110: Method[] allMethods = clz.getMethods();
111: for (int i = 0; i < allMethods.length; i++) {
112: if (isAnnotated(allMethods[i], annotation)) {
113: return true;
114: }
115: }
116: return false;
117: }
118:
119: public static boolean isAnnotated(Field field,
120: Class<? extends Annotation> annotation) {
121: return field.isAnnotationPresent(annotation);
122: }
123:
124: public static void main(String[] args) {
125:
126: }
127: }
|