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