1
2
3
4
5
6
7
8
9
10 package org.mule.util.annotation;
11
12 import java.io.IOException;
13 import java.lang.annotation.Annotation;
14 import java.lang.annotation.ElementType;
15 import java.lang.reflect.Field;
16 import java.lang.reflect.Method;
17 import java.util.ArrayList;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21
22
23
24
25 public class AnnotationUtils
26 {
27 public static boolean methodHasParamAnnotations(Method method)
28 {
29 for (int i = 0; i < method.getParameterAnnotations().length; i++)
30 {
31 if (method.getParameterAnnotations()[i].length > 0)
32 {
33 return true;
34 }
35 }
36 return false;
37 }
38
39 public static List<AnnotationMetaData> getParamAnnotations(Method method)
40 {
41 return getParamAnnotationsWithMeta(method, null);
42 }
43
44 public static List<AnnotationMetaData> getParamAnnotationsWithMeta(Method method, Class<? extends Annotation> metaAnnotation)
45 {
46 List<AnnotationMetaData> annos = new ArrayList<AnnotationMetaData>();
47
48 for (int i = 0; i < method.getParameterAnnotations().length; i++)
49 {
50 for (int j = 0; j < method.getParameterAnnotations()[i].length; j++)
51 {
52 Annotation annotation = method.getParameterAnnotations()[i][j];
53 if(metaAnnotation==null || annotation.annotationType().isAnnotationPresent(metaAnnotation))
54 {
55 annos.add(new AnnotationMetaData(method.getDeclaringClass(), method, ElementType.PARAMETER, annotation));
56 }
57 }
58 }
59 return annos;
60 }
61
62
63
64 public static List<AnnotationMetaData> getClassAndMethodAnnotations(Class c)
65 {
66 List<AnnotationMetaData> annotations = new ArrayList<AnnotationMetaData>();
67
68 for (int i = 0; i < c.getAnnotations().length; i++)
69 {
70 annotations.add(new AnnotationMetaData(c, null, ElementType.TYPE, c.getAnnotations()[i]));
71 }
72
73 annotations.addAll(getAllMethodAnnotations(c));
74 return annotations;
75 }
76
77 public static List<AnnotationMetaData> getAllMethodAnnotations(Class c)
78 {
79 List<AnnotationMetaData> annotations = new ArrayList<AnnotationMetaData>();
80
81 for (int i = 0; i < c.getMethods().length; i++)
82 {
83 Method method = c.getMethods()[i];
84 for (int j = 0; j < method.getDeclaredAnnotations().length; j++)
85 {
86 annotations.add(new AnnotationMetaData(c, method, ElementType.METHOD, method.getDeclaredAnnotations()[j]));
87 }
88 }
89 return annotations;
90 }
91
92 public static List<AnnotationMetaData> getMethodAnnotations(Class c, Class<? extends Annotation> ann)
93 {
94 List<AnnotationMetaData> annotations = new ArrayList<AnnotationMetaData>();
95
96 for (int i = 0; i < c.getMethods().length; i++)
97 {
98 Method method = c.getMethods()[i];
99 for (int j = 0; j < method.getDeclaredAnnotations().length; j++)
100 {
101 if (ann.isInstance(method.getDeclaredAnnotations()[j]))
102 {
103 annotations.add(new AnnotationMetaData(c, method, ElementType.METHOD, method.getDeclaredAnnotations()[j]));
104 }
105 }
106 }
107 return annotations;
108 }
109
110 public static List<AnnotationMetaData> getMethodMetaAnnotations(Class c, Class<? extends Annotation> metaAnn)
111 {
112 List<AnnotationMetaData> annotations = new ArrayList<AnnotationMetaData>();
113
114 for (int i = 0; i < c.getMethods().length; i++)
115 {
116 Method method = c.getMethods()[i];
117 for (int j = 0; j < method.getDeclaredAnnotations().length; j++)
118 {
119 if (method.getDeclaredAnnotations()[j].annotationType().isAnnotationPresent(metaAnn))
120 {
121 annotations.add(new AnnotationMetaData(c, method, ElementType.METHOD, method.getDeclaredAnnotations()[j]));
122 }
123 }
124 }
125 return annotations;
126 }
127
128 public static List<AnnotationMetaData> getAllFieldAnnotations(Class c)
129 {
130 List<AnnotationMetaData> annotations = new ArrayList<AnnotationMetaData>();
131
132 for (int i = 0; i < c.getDeclaredFields().length; i++)
133 {
134 Field field = c.getDeclaredFields()[i];
135 for (int j = 0; j < field.getDeclaredAnnotations().length; j++)
136 {
137 annotations.add(new AnnotationMetaData(c, field, ElementType.FIELD, field.getDeclaredAnnotations()[j]));
138 }
139 }
140 return annotations;
141 }
142
143 public static List<AnnotationMetaData> getFieldAnnotations(Class c, Class<? extends Annotation> annotation)
144 {
145 List<AnnotationMetaData> annotations = new ArrayList<AnnotationMetaData>();
146
147 for (int i = 0; i < c.getDeclaredFields().length; i++)
148 {
149 Field field = c.getDeclaredFields()[i];
150 for (int j = 0; j < field.getDeclaredAnnotations().length; j++)
151 {
152 if (annotation.equals(field.getDeclaredAnnotations()[j].annotationType()))
153 {
154 annotations.add(new AnnotationMetaData(c, field, ElementType.FIELD, field.getDeclaredAnnotations()[j]));
155 }
156 }
157 }
158 return annotations;
159 }
160
161 public static AnnotationMetaData getClassAnnotationInHeirarchy(Class<? extends Annotation> annotation, Class bottom)
162 {
163 AnnotationMetaData anno = getClassAnnotationForSuperClasses(annotation, bottom);
164 if (anno == null)
165 {
166 for (int i = 0; i < bottom.getInterfaces().length; i++)
167 {
168 Class aClass = bottom.getInterfaces()[i];
169 if (aClass.isAnnotationPresent(annotation))
170 {
171 anno = new AnnotationMetaData(aClass, null, ElementType.TYPE, aClass.getAnnotation(annotation));
172 break;
173 }
174 }
175 }
176 return anno;
177 }
178
179 public static List<AnnotationMetaData> getClassAnnotationInHeirarchy(Class bottom)
180 {
181 List<AnnotationMetaData> annos = new ArrayList<AnnotationMetaData>();
182
183 getClassAnnotationForSuperClasses(bottom, annos);
184 getClassAnnotationForInterfaces(bottom, annos);
185
186 return annos;
187 }
188
189 protected static AnnotationMetaData getClassAnnotationForSuperClasses(Class<? extends Annotation> annotation, Class bottom)
190 {
191 if (bottom.isAnnotationPresent(annotation))
192 {
193 return new AnnotationMetaData(bottom, null, ElementType.TYPE, bottom.getAnnotation(annotation));
194 }
195 if (bottom.getSuperclass() != null && !bottom.getSuperclass().equals(Object.class))
196 {
197 return getClassAnnotationForSuperClasses(annotation, bottom.getSuperclass());
198 }
199 return null;
200 }
201
202 protected static void getClassAnnotationForSuperClasses(Class bottom, List<AnnotationMetaData> annos)
203 {
204 for (Annotation annotation : bottom.getAnnotations())
205 {
206 annos.add(new AnnotationMetaData(bottom, null, ElementType.TYPE, annotation));
207 }
208
209 if (bottom.getSuperclass() != null && !bottom.getSuperclass().equals(Object.class))
210 {
211 getClassAnnotationForSuperClasses(bottom.getSuperclass(), annos);
212 }
213 }
214
215 protected static void getClassAnnotationForInterfaces(Class bottom, List<AnnotationMetaData> annos)
216 {
217 for (Class aClass : bottom.getInterfaces())
218 {
219 for (Annotation annotation : aClass.getAnnotations())
220 {
221 annos.add(new AnnotationMetaData(bottom, null, ElementType.TYPE, annotation));
222 }
223 }
224 }
225
226 public static Set<AnnotationMetaData> getFieldAnnotationsForHeirarchy(Class bottom)
227 {
228 Set<AnnotationMetaData> annos = new HashSet<AnnotationMetaData>();
229 getFieldAnnotationsForSuperClasses(bottom, annos);
230 getFieldAnnotationsForInterfaces(bottom, annos);
231 return annos;
232 }
233
234 public static void getFieldAnnotationsForInterfaces(Class clazz, Set<AnnotationMetaData> annos)
235 {
236 for (int i = 0; i < clazz.getInterfaces().length; i++)
237 {
238 Class aClass = clazz.getInterfaces()[i];
239 annos.addAll(getAllFieldAnnotations(aClass));
240 getFieldAnnotationsForInterfaces(aClass, annos);
241 }
242 }
243
244 protected static void getFieldAnnotationsForSuperClasses(Class bottom, Set<AnnotationMetaData> annos)
245 {
246 annos.addAll(getAllFieldAnnotations(bottom));
247
248 if (bottom.getSuperclass() != null && !bottom.getSuperclass().equals(Object.class))
249 {
250 getFieldAnnotationsForSuperClasses(bottom.getSuperclass(), annos);
251 }
252 }
253
254
255 public static Set<AnnotationMetaData> getFieldAnnotationsForHeirarchy(Class bottom, Class<? extends Annotation> annotation)
256 {
257 Set<AnnotationMetaData> annos = new HashSet<AnnotationMetaData>();
258 getFieldAnnotationsForSuperClasses(bottom, annos, annotation);
259 getFieldAnnotationsForInterfaces(bottom, annos, annotation);
260 return annos;
261 }
262
263 public static void getFieldAnnotationsForInterfaces(Class clazz, Set<AnnotationMetaData> annos, Class<? extends Annotation> annotation)
264 {
265 for (int i = 0; i < clazz.getInterfaces().length; i++)
266 {
267 Class aClass = clazz.getInterfaces()[i];
268 annos.addAll(getFieldAnnotations(aClass, annotation));
269 getFieldAnnotationsForInterfaces(aClass, annos, annotation);
270 }
271 }
272
273 protected static void getFieldAnnotationsForSuperClasses(Class bottom, Set<AnnotationMetaData> annos, Class<? extends Annotation> annotation)
274 {
275 annos.addAll(getFieldAnnotations(bottom, annotation));
276
277 if (bottom.getSuperclass() != null && !bottom.getSuperclass().equals(Object.class))
278 {
279 getFieldAnnotationsForSuperClasses(bottom.getSuperclass(), annos, annotation);
280 }
281 }
282
283
284 public static boolean hasAnnotation(Class<? super Annotation> annotation, Class clazz) throws IOException
285 {
286 for (Annotation anno : clazz.getDeclaredAnnotations())
287 {
288 if (anno.annotationType().getName().equals(clazz.getName()))
289 {
290 return true;
291 }
292 }
293
294 for (Field field : clazz.getDeclaredFields())
295 {
296 for (Annotation anno : field.getDeclaredAnnotations())
297 {
298 if (anno.annotationType().getName().equals(clazz.getName()))
299 {
300 return true;
301 }
302 }
303 }
304
305 for (Method method : clazz.getDeclaredMethods())
306 {
307 for (Annotation anno : method.getDeclaredAnnotations())
308 {
309 if (anno.annotationType().getName().equals(clazz.getName()))
310 {
311 return true;
312 }
313 }
314 }
315
316 return false;
317 }
318
319 public static boolean hasAnnotationWithPackage(String packageName, Class clazz) throws IOException
320 {
321 for (Annotation anno : clazz.getDeclaredAnnotations())
322 {
323 if (anno.annotationType().getPackage().getName().startsWith(packageName))
324 {
325 return true;
326 }
327 }
328
329 for (Field field : clazz.getDeclaredFields())
330 {
331 for (Annotation anno : field.getDeclaredAnnotations())
332 {
333 if (anno.annotationType().getPackage().getName().startsWith(packageName))
334 {
335 return true;
336 }
337 }
338 }
339
340 for (Method method : clazz.getDeclaredMethods())
341 {
342 for (Annotation anno : method.getDeclaredAnnotations())
343 {
344 if (anno.annotationType().getPackage().getName().startsWith(packageName))
345 {
346 return true;
347 }
348 }
349 }
350
351 return false;
352 }
353
354 }