View Javadoc

1   /*
2    * $Id: AnnotationUtils.java 22263 2011-06-27 08:31:06Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * A helper class for reading annotations.
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     /**
162      * @deprecated use getClassAnnotationInHierarchy(Class, Class)
163      */
164     @Deprecated
165     public static AnnotationMetaData getClassAnnotationInHeirarchy(Class<? extends Annotation> annotation, Class<?> bottom)
166     {
167         return getClassAnnotationInHierarchy(annotation, bottom);
168     }
169 
170     public static AnnotationMetaData getClassAnnotationInHierarchy(Class<? extends Annotation> annotation, Class<?> bottom)
171     {
172         AnnotationMetaData anno = getClassAnnotationForSuperClasses(annotation, bottom);
173         if (anno == null)
174         {
175             for (int i = 0; i < bottom.getInterfaces().length; i++)
176             {
177                 Class<?> aClass = bottom.getInterfaces()[i];
178                 if (aClass.isAnnotationPresent(annotation))
179                 {
180                     anno = new AnnotationMetaData(aClass, null, ElementType.TYPE, aClass.getAnnotation(annotation));
181                     break;
182                 }
183             }
184         }
185         return anno;
186     }
187 
188     /**
189      * @deprecated use getClassAnnotationInHierarchy(Class)
190      */
191     @Deprecated
192     public static List<AnnotationMetaData> getClassAnnotationInHeirarchy(Class<?> bottom)
193     {
194         return getClassAnnotationInHierarchy(bottom);
195     }
196 
197     public static List<AnnotationMetaData> getClassAnnotationInHierarchy(Class<?> bottom)
198     {
199         List<AnnotationMetaData> annos = new ArrayList<AnnotationMetaData>();
200 
201         getClassAnnotationForSuperClasses(bottom, annos);
202         getClassAnnotationForInterfaces(bottom, annos);
203 
204         return annos;
205     }
206 
207     protected static AnnotationMetaData getClassAnnotationForSuperClasses(Class<? extends Annotation> annotation, Class<?> bottom)
208     {
209         if (bottom.isAnnotationPresent(annotation))
210         {
211             return new AnnotationMetaData(bottom, null, ElementType.TYPE, bottom.getAnnotation(annotation));
212         }
213         if (bottom.getSuperclass() != null && !bottom.getSuperclass().equals(Object.class))
214         {
215             return getClassAnnotationForSuperClasses(annotation, bottom.getSuperclass());
216         }
217         return null;
218     }
219 
220     protected static void getClassAnnotationForSuperClasses(Class<?> bottom, List<AnnotationMetaData> annos)
221     {
222         for (Annotation annotation : bottom.getAnnotations())
223         {
224             annos.add(new AnnotationMetaData(bottom, null, ElementType.TYPE, annotation));
225         }
226 
227         if (bottom.getSuperclass() != null && !bottom.getSuperclass().equals(Object.class))
228         {
229             getClassAnnotationForSuperClasses(bottom.getSuperclass(), annos);
230         }
231     }
232 
233     protected static void getClassAnnotationForInterfaces(Class<?> bottom, List<AnnotationMetaData> annos)
234     {
235         for (Class<?> aClass : bottom.getInterfaces())
236         {
237             for (Annotation annotation : aClass.getAnnotations())
238             {
239                 annos.add(new AnnotationMetaData(bottom, null, ElementType.TYPE, annotation));
240             }
241         }
242     }
243 
244     /**
245      * @deprecated use getFieldAnnotationsForHierarchy(Class)
246      */
247     @Deprecated
248     public static Set<AnnotationMetaData> getFieldAnnotationsForHeirarchy(Class<?> bottom)
249     {
250         return getFieldAnnotationsForHierarchy(bottom);
251     }
252 
253     public static Set<AnnotationMetaData> getFieldAnnotationsForHierarchy(Class<?> bottom)
254     {
255         Set<AnnotationMetaData> annos = new HashSet<AnnotationMetaData>();
256         getFieldAnnotationsForSuperClasses(bottom, annos);
257         getFieldAnnotationsForInterfaces(bottom, annos);
258         return annos;
259     }
260 
261     public static void getFieldAnnotationsForInterfaces(Class<?> clazz, Set<AnnotationMetaData> annos)
262     {
263         for (int i = 0; i < clazz.getInterfaces().length; i++)
264         {
265             Class<?> aClass = clazz.getInterfaces()[i];
266             annos.addAll(getAllFieldAnnotations(aClass));
267             getFieldAnnotationsForInterfaces(aClass, annos);
268         }
269     }
270 
271     protected static void getFieldAnnotationsForSuperClasses(Class<?> bottom, Set<AnnotationMetaData> annos)
272     {
273         annos.addAll(getAllFieldAnnotations(bottom));
274 
275         if (bottom.getSuperclass() != null && !bottom.getSuperclass().equals(Object.class))
276         {
277             getFieldAnnotationsForSuperClasses(bottom.getSuperclass(), annos);
278         }
279     }
280 
281     /**
282      * @deprecated use getFieldAnnotationsForHierarchy(Class, Class)
283      */
284     @Deprecated
285     public static Set<AnnotationMetaData> getFieldAnnotationsForHeirarchy(Class<?> bottom, Class<? extends Annotation> annotation)
286     {
287         return getFieldAnnotationsForHierarchy(bottom, annotation);
288     }
289 
290     public static Set<AnnotationMetaData> getFieldAnnotationsForHierarchy(Class<?> bottom, Class<? extends Annotation> annotation)
291     {
292         Set<AnnotationMetaData> annos = new HashSet<AnnotationMetaData>();
293         getFieldAnnotationsForSuperClasses(bottom, annos, annotation);
294         getFieldAnnotationsForInterfaces(bottom, annos, annotation);
295         return annos;
296     }
297 
298     public static void getFieldAnnotationsForInterfaces(Class<?> clazz, Set<AnnotationMetaData> annos, Class<? extends Annotation> annotation)
299     {
300         for (int i = 0; i < clazz.getInterfaces().length; i++)
301         {
302             Class<?> aClass = clazz.getInterfaces()[i];
303             annos.addAll(getFieldAnnotations(aClass, annotation));
304             getFieldAnnotationsForInterfaces(aClass, annos, annotation);
305         }
306     }
307 
308     protected static void getFieldAnnotationsForSuperClasses(Class<?> bottom, Set<AnnotationMetaData> annos, Class<? extends Annotation> annotation)
309     {
310         annos.addAll(getFieldAnnotations(bottom, annotation));
311 
312         if (bottom.getSuperclass() != null && !bottom.getSuperclass().equals(Object.class))
313         {
314             getFieldAnnotationsForSuperClasses(bottom.getSuperclass(), annos, annotation);
315         }
316     }
317 
318 
319     public static boolean hasAnnotation(Class<? super Annotation> annotation, Class<?> clazz) throws IOException
320     {
321         for (Annotation anno : clazz.getDeclaredAnnotations())
322         {
323             if (anno.annotationType().getName().equals(clazz.getName()))
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().getName().equals(clazz.getName()))
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().getName().equals(clazz.getName()))
345                 {
346                     return true;
347                 }
348             }
349         }
350 
351         return false;
352     }
353 
354     public static boolean hasAnnotationWithPackage(String packageName, Class<?> clazz) throws IOException
355     {
356         for (Annotation anno : clazz.getDeclaredAnnotations())
357         {
358             if (anno.annotationType().getPackage().getName().startsWith(packageName))
359             {
360                 return true;
361             }
362         }
363 
364         for (Field field : clazz.getDeclaredFields())
365         {
366             for (Annotation anno : field.getDeclaredAnnotations())
367             {
368                 if (anno.annotationType().getPackage().getName().startsWith(packageName))
369                 {
370                     return true;
371                 }
372             }
373         }
374 
375         for (Method method : clazz.getDeclaredMethods())
376         {
377             for (Annotation anno : method.getDeclaredAnnotations())
378             {
379                 if (anno.annotationType().getPackage().getName().startsWith(packageName))
380                 {
381                     return true;
382                 }
383             }
384         }
385 
386         return false;
387     }
388 
389 }