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