View Javadoc

1   /***************************************************************************************
2    * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package org.codehaus.aspectwerkz.annotation.instrumentation.asm;
9   
10  import org.codehaus.aspectwerkz.annotation.Annotation;
11  import org.codehaus.aspectwerkz.annotation.AnnotationInfo;
12  import org.codehaus.aspectwerkz.reflect.ClassInfo;
13  import org.codehaus.aspectwerkz.reflect.MethodInfo;
14  import org.codehaus.aspectwerkz.reflect.ConstructorInfo;
15  import org.codehaus.aspectwerkz.reflect.FieldInfo;
16  
17  import java.util.List;
18  import java.util.Iterator;
19  import java.util.ArrayList;
20  
21  /***
22   * Helper class to extract annotations by their name from a ClassInfo structure.
23   *
24   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
25   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
26   */
27  public class AsmAnnotations {
28      /***
29       * Return the annotation with a specific name for a specific class.
30       *
31       * @param annotationName the annotation name
32       * @param classInfo          the ClassInfo object to find the annotation on.
33       * @return the annotation or null
34       */
35      public static Annotation getAnnotation(final String annotationName, final ClassInfo classInfo) {
36          List annotations = classInfo.getAnnotations();
37          for (Iterator it = annotations.iterator(); it.hasNext();) {
38              AnnotationInfo annotationInfo = (AnnotationInfo)it.next();
39              if (annotationInfo.getName().equals(annotationName)) {
40                  return annotationInfo.getAnnotation();
41              }
42          }
43          return null;
44      }
45  
46      /***
47       * Return the annotation with a specific name for a specific method.
48       *
49       * @param annotationName the annotation name
50       * @param methodInfo         the MethodInfo object to find the annotation on.
51       * @return the annotation or null
52       */
53      public static Annotation getAnnotation(final String annotationName, final MethodInfo methodInfo) {
54          List annotations = methodInfo.getAnnotations();
55          for (Iterator it = annotations.iterator(); it.hasNext();) {
56              AnnotationInfo annotationInfo = (AnnotationInfo)it.next();
57              if (annotationInfo.getName().equals(annotationName)) {
58                  return annotationInfo.getAnnotation();
59              }
60          }
61          return null;
62      }
63  
64      /***
65       * Return the annotation with a specific name for a specific constructor.
66       *
67       * @param annotationName the annotation name
68       * @param constructorInfo    the ConstructorInfo object to find the annotation on.
69       * @return the annotation or null
70       */
71      public static Annotation getAnnotation(final String annotationName, final ConstructorInfo constructorInfo) {
72          List annotations = constructorInfo.getAnnotations();
73          for (Iterator it = annotations.iterator(); it.hasNext();) {
74              AnnotationInfo annotationInfo = (AnnotationInfo)it.next();
75              if (annotationInfo.getName().equals(annotationName)) {
76                  return annotationInfo.getAnnotation();
77              }
78          }
79          return null;
80      }
81  
82      /***
83       * Return the annotation with a specific name for a specific field.
84       *
85       * @param annotationName the annotation name
86       * @param fieldInfo          the FieldInfo object to find the annotation on.
87       * @return the annotation or null
88       */
89      public static Annotation getAnnotation(final String annotationName, final FieldInfo fieldInfo) {
90          List annotations = fieldInfo.getAnnotations();
91          for (Iterator it = annotations.iterator(); it.hasNext();) {
92              AnnotationInfo annotationInfo = (AnnotationInfo)it.next();
93              if (annotationInfo.getName().equals(annotationName)) {
94                  return annotationInfo.getAnnotation();
95              }
96          }
97          return null;
98      }
99  
100     /***
101      * Return a list with the annotations with a specific name for a specific class.
102      *
103      * @param annotationName the annotation name
104      * @param classInfo          ClassInfo object to find the annotation on.
105      * @return the annotations in a list (can be empty)
106      */
107     public static List getAnnotations(final String annotationName, final ClassInfo classInfo) {
108         List annotations = new ArrayList();
109         for (Iterator it = classInfo.getAnnotations().iterator(); it.hasNext();) {
110             AnnotationInfo annotationInfo = (AnnotationInfo)it.next();
111             if (annotationInfo.getName().equals(annotationName)) {
112                 annotations.add(annotationInfo.getAnnotation());
113             }
114         }
115         return annotations;
116     }
117 
118     /***
119      * Return a list with the annotations with a specific name for a specific method.
120      *
121      * @param annotationName the annotation name
122      * @param methodInfo         the MethodInfo object to find the annotation on.
123      * @return the annotations in a list (can be empty)
124      */
125     public static List getAnnotations(final String annotationName, final MethodInfo methodInfo) {
126         List annotations = new ArrayList();
127         for (Iterator it = methodInfo.getAnnotations().iterator(); it.hasNext();) {
128             AnnotationInfo annotationInfo = (AnnotationInfo)it.next();
129             if (annotationInfo.getName().equals(annotationName)) {
130                 annotations.add(annotationInfo.getAnnotation());
131             }
132         }
133         return annotations;
134     }
135 
136     /***
137      * Return a list with the annotations with a specific name for a specific constructor.
138      *
139      * @param annotationName the annotation name
140      * @param constructorInfo    the ConstructorInfo object to find the annotation on.
141      * @return the annotations in a list (can be empty)
142      */
143     public static List getAnnotations(final String annotationName, final ConstructorInfo constructorInfo) {
144         List annotations = new ArrayList();
145         for (Iterator it = constructorInfo.getAnnotations().iterator(); it.hasNext();) {
146             AnnotationInfo annotationInfo = (AnnotationInfo)it.next();
147             if (annotationInfo.getName().equals(annotationName)) {
148                 annotations.add(annotationInfo.getAnnotation());
149             }
150         }
151         return annotations;
152     }
153 
154     /***
155      * Return a list with the annotations with a specific name for a specific field.
156      *
157      * @param annotationName the annotation name
158      * @param fieldInfo          the FieldInfo object to find the annotation on.
159      * @return the annotations in a list (can be empty)
160      */
161     public static List getAnnotations(final String annotationName, final FieldInfo fieldInfo) {
162         List annotations = new ArrayList();
163         for (Iterator it = fieldInfo.getAnnotations().iterator(); it.hasNext();) {
164             AnnotationInfo annotationInfo = (AnnotationInfo)it.next();
165             if (annotationInfo.getName().equals(annotationName)) {
166                 annotations.add(annotationInfo.getAnnotation());
167             }
168         }
169         return annotations;
170     }
171 
172 }