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