001: /**************************************************************************************
002: * Copyright (c) Jonas Bon?r, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package org.codehaus.aspectwerkz.annotation.instrumentation.asm;
008:
009: import org.codehaus.aspectwerkz.annotation.Annotation;
010: import org.codehaus.aspectwerkz.annotation.AnnotationInfo;
011: import org.codehaus.aspectwerkz.reflect.ClassInfo;
012: import org.codehaus.aspectwerkz.reflect.MethodInfo;
013: import org.codehaus.aspectwerkz.reflect.ConstructorInfo;
014: import org.codehaus.aspectwerkz.reflect.FieldInfo;
015:
016: import java.util.List;
017: import java.util.Iterator;
018: import java.util.ArrayList;
019:
020: /**
021: * Helper class to extract annotations by their name from a ClassInfo structure.
022: *
023: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
024: * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur</a>
025: */
026: public class AsmAnnotations {
027: /**
028: * Return the annotation with a specific name for a specific class.
029: *
030: * @param annotationName the annotation name
031: * @param classInfo the ClassInfo object to find the annotation on.
032: * @return the annotation or null
033: */
034: public static Annotation getAnnotation(final String annotationName,
035: final ClassInfo classInfo) {
036: List annotations = classInfo.getAnnotations();
037: for (Iterator it = annotations.iterator(); it.hasNext();) {
038: AnnotationInfo annotationInfo = (AnnotationInfo) it.next();
039: if (annotationInfo.getName().equals(annotationName)) {
040: return annotationInfo.getAnnotation();
041: }
042: }
043: return null;
044: }
045:
046: /**
047: * Return the annotation with a specific name for a specific method.
048: *
049: * @param annotationName the annotation name
050: * @param methodInfo the MethodInfo object to find the annotation on.
051: * @return the annotation or null
052: */
053: public static Annotation getAnnotation(final String annotationName,
054: final MethodInfo methodInfo) {
055: List annotations = methodInfo.getAnnotations();
056: for (Iterator it = annotations.iterator(); it.hasNext();) {
057: AnnotationInfo annotationInfo = (AnnotationInfo) it.next();
058: if (annotationInfo.getName().equals(annotationName)) {
059: return annotationInfo.getAnnotation();
060: }
061: }
062: return null;
063: }
064:
065: /**
066: * Return the annotation with a specific name for a specific constructor.
067: *
068: * @param annotationName the annotation name
069: * @param constructorInfo the ConstructorInfo object to find the annotation on.
070: * @return the annotation or null
071: */
072: public static Annotation getAnnotation(final String annotationName,
073: final ConstructorInfo constructorInfo) {
074: List annotations = constructorInfo.getAnnotations();
075: for (Iterator it = annotations.iterator(); it.hasNext();) {
076: AnnotationInfo annotationInfo = (AnnotationInfo) it.next();
077: if (annotationInfo.getName().equals(annotationName)) {
078: return annotationInfo.getAnnotation();
079: }
080: }
081: return null;
082: }
083:
084: /**
085: * Return the annotation with a specific name for a specific field.
086: *
087: * @param annotationName the annotation name
088: * @param fieldInfo the FieldInfo object to find the annotation on.
089: * @return the annotation or null
090: */
091: public static Annotation getAnnotation(final String annotationName,
092: final FieldInfo fieldInfo) {
093: List annotations = fieldInfo.getAnnotations();
094: for (Iterator it = annotations.iterator(); it.hasNext();) {
095: AnnotationInfo annotationInfo = (AnnotationInfo) it.next();
096: if (annotationInfo.getName().equals(annotationName)) {
097: return annotationInfo.getAnnotation();
098: }
099: }
100: return null;
101: }
102:
103: /**
104: * Return a list with the annotations with a specific name for a specific class.
105: *
106: * @param annotationName the annotation name
107: * @param classInfo ClassInfo object to find the annotation on.
108: * @return the annotations in a list (can be empty)
109: */
110: public static List getAnnotations(final String annotationName,
111: final ClassInfo classInfo) {
112: List annotations = new ArrayList();
113: for (Iterator it = classInfo.getAnnotations().iterator(); it
114: .hasNext();) {
115: AnnotationInfo annotationInfo = (AnnotationInfo) it.next();
116: if (annotationInfo.getName().equals(annotationName)) {
117: annotations.add(annotationInfo.getAnnotation());
118: }
119: }
120: return annotations;
121: }
122:
123: /**
124: * Return a list with the annotations with a specific name for a specific method.
125: *
126: * @param annotationName the annotation name
127: * @param methodInfo the MethodInfo object to find the annotation on.
128: * @return the annotations in a list (can be empty)
129: */
130: public static List getAnnotations(final String annotationName,
131: final MethodInfo methodInfo) {
132: List annotations = new ArrayList();
133: for (Iterator it = methodInfo.getAnnotations().iterator(); it
134: .hasNext();) {
135: AnnotationInfo annotationInfo = (AnnotationInfo) it.next();
136: if (annotationInfo.getName().equals(annotationName)) {
137: annotations.add(annotationInfo.getAnnotation());
138: }
139: }
140: return annotations;
141: }
142:
143: /**
144: * Return a list with the annotations with a specific name for a specific constructor.
145: *
146: * @param annotationName the annotation name
147: * @param constructorInfo the ConstructorInfo object to find the annotation on.
148: * @return the annotations in a list (can be empty)
149: */
150: public static List getAnnotations(final String annotationName,
151: final ConstructorInfo constructorInfo) {
152: List annotations = new ArrayList();
153: for (Iterator it = constructorInfo.getAnnotations().iterator(); it
154: .hasNext();) {
155: AnnotationInfo annotationInfo = (AnnotationInfo) it.next();
156: if (annotationInfo.getName().equals(annotationName)) {
157: annotations.add(annotationInfo.getAnnotation());
158: }
159: }
160: return annotations;
161: }
162:
163: /**
164: * Return a list with the annotations with a specific name for a specific field.
165: *
166: * @param annotationName the annotation name
167: * @param fieldInfo the FieldInfo object to find the annotation on.
168: * @return the annotations in a list (can be empty)
169: */
170: public static List getAnnotations(final String annotationName,
171: final FieldInfo fieldInfo) {
172: List annotations = new ArrayList();
173: for (Iterator it = fieldInfo.getAnnotations().iterator(); it
174: .hasNext();) {
175: AnnotationInfo annotationInfo = (AnnotationInfo) it.next();
176: if (annotationInfo.getName().equals(annotationName)) {
177: annotations.add(annotationInfo.getAnnotation());
178: }
179: }
180: return annotations;
181: }
182:
183: }
|