1 package org.jfox.ioc.util; 2 3 import java.lang.annotation.Annotation ; 4 import java.lang.reflect.Method ; 5 import java.lang.reflect.Field ; 6 import java.util.ArrayList ; 7 import java.util.List ; 8 9 12 13 public class Annotations { 14 15 18 public static boolean isAnnotated(Method method, Class <? extends Annotation > annotation) { 19 return method.isAnnotationPresent(annotation); 20 } 21 22 public static Method [] getAnnotatedMethods(Class clz, Class <? extends Annotation > annotation){ 23 List <Method > methods = new ArrayList <Method >(); 24 Method [] 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 [methods.size()]); 31 } 32 33 public static Method [] getAnnotatedSetMethods(Class clz, Class <? extends Annotation > annotation){ 34 List <Method > methods = new ArrayList <Method >(); 35 Method [] 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 [methods.size()]); 42 } 43 44 public static Method [] getAnnotatedGetMethods(Class clz, Class <? extends Annotation > annotation){ 45 List <Method > methods = new ArrayList <Method >(); 46 Method [] 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 [methods.size()]); 53 } 54 55 public static boolean hasAnnotatedMethod(Class clz, Class <? extends Annotation > annotation){ 56 Method [] 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 field, 66 Class <? extends Annotation > annotation) { 67 return field.isAnnotationPresent(annotation); 68 } 69 70 public static void main(String [] args) { 71 72 } 73 } 74 75 | Popular Tags |