KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > annotation > PortableAnnotationElement


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.annotation;
23
24 import javassist.ClassPool;
25 import javassist.CtClass;
26 import javassist.CtConstructor;
27 import javassist.CtField;
28 import javassist.CtMethod;
29 import javassist.NotFoundException;
30 import javassist.bytecode.AnnotationsAttribute;
31 import javassist.bytecode.ClassFile;
32 import javassist.bytecode.FieldInfo;
33 import javassist.bytecode.MethodInfo;
34
35 import org.jboss.aop.AspectManager;
36 import org.jboss.aop.annotation.factory.duplicate.javassist.AnnotationProxy;
37 import org.jboss.aop.util.ReflectToJavassist;
38
39 import java.lang.reflect.Constructor JavaDoc;
40 import java.lang.reflect.Field JavaDoc;
41 import java.lang.reflect.Method JavaDoc;
42 import java.lang.reflect.Proxy JavaDoc;
43
44 /**
45  * This base class is portable between JDK 1.4 and JDK 1.5
46  * AnnotationElement will be different for JDK 1.4 and JDK 1.5
47  *
48  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
49  * @version $Revision: 57019 $
50  */

51 public class PortableAnnotationElement
52 {
53    public static boolean isInvisibleAnnotationPresent(Field field, String JavaDoc annotation) throws Exception JavaDoc
54    {
55       CtField ctMethod = ReflectToJavassist.fieldToJavassist(field);
56       return AnnotationElement.isInvisibleAnnotationPresent(ctMethod, annotation);
57
58    }
59
60    public static boolean isInvisibleAnnotationPresent(CtField field, String JavaDoc annotation)
61    {
62       FieldInfo mi = field.getFieldInfo2();
63
64       AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
65       if (invisible == null) return false;
66
67       return invisible.getAnnotation(annotation) != null;
68    }
69
70    public static boolean isVisibleAnnotationPresent(CtField field, String JavaDoc annotation)
71    {
72       FieldInfo mi = field.getFieldInfo2();
73
74       AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag);
75       if (visible == null) return false;
76
77       return visible.getAnnotation(annotation) != null;
78    }
79
80    public static boolean isAnyAnnotationPresent(CtField ctField, String JavaDoc annotation)
81    {
82       FieldInfo mi = ctField.getFieldInfo2();
83
84       AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag);
85       if (visible != null)
86       {
87          if (visible.getAnnotation(annotation) != null) return true;
88       }
89
90       AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
91       if (invisible != null)
92       {
93          if (invisible.getAnnotation(annotation) != null) return true;
94       }
95
96       return false;
97    }
98
99    public static boolean isInvisibleAnnotationPresent(Method method, String JavaDoc annotation) throws Exception JavaDoc
100    {
101       CtMethod ctMethod = ReflectToJavassist.methodToJavassist(method);
102       if (ctMethod == null) return false;
103       MethodInfo mi = ctMethod.getMethodInfo2();
104
105
106       AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
107       if (invisible == null) return false;
108
109
110       return invisible.getAnnotation(annotation) != null;
111    }
112
113    public static boolean isAnyAnnotationPresent(Field field, String JavaDoc annotation) throws Exception JavaDoc
114    {
115       CtField ctField = ReflectToJavassist.fieldToJavassist(field);
116       return AnnotationElement.isAnyAnnotationPresent(ctField, annotation);
117
118    }
119
120    public static boolean isAnyAnnotationPresent(Method method, String JavaDoc annotation) throws Exception JavaDoc
121    {
122       CtMethod ctMethod = ReflectToJavassist.methodToJavassist(method);
123       if (ctMethod == null) return false;
124       boolean present = AnnotationElement.isAnyAnnotationPresent(ctMethod, annotation);
125       return present;
126
127    }
128
129    public static boolean isAnyAnnotationPresent(CtMethod ctMethod, String JavaDoc annotation)
130    {
131       MethodInfo mi = ctMethod.getMethodInfo2();
132
133       AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag);
134       if (visible != null)
135       {
136          if (visible.getAnnotation(annotation) != null) return true;
137       }
138
139       AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
140       if (invisible != null)
141       {
142          if (invisible.getAnnotation(annotation) != null) return true;
143       }
144
145       return false;
146    }
147
148    public static boolean isInvisibleAnnotationPresent(Constructor con, String JavaDoc annotation) throws Exception JavaDoc
149    {
150       CtConstructor ctMethod = ReflectToJavassist.constructorToJavassist(con);
151       return AnnotationElement.isInvisibleAnnotationPresent(ctMethod, annotation);
152
153
154    }
155
156    public static boolean isInvisibleAnnotationPresent(CtConstructor ctMethod, String JavaDoc annotation)
157    {
158       MethodInfo mi = ctMethod.getMethodInfo2();
159
160
161       AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
162       if (invisible == null) return false;
163
164       return invisible.getAnnotation(annotation) != null;
165    }
166
167    public static boolean isVisibleAnnotationPresent(CtConstructor ctMethod, String JavaDoc annotation)
168    {
169       MethodInfo mi = ctMethod.getMethodInfo2();
170
171
172       AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag);
173       if (visible == null) return false;
174
175       return visible.getAnnotation(annotation) != null;
176    }
177
178    public static boolean isAnyAnnotationPresent(Constructor con, String JavaDoc annotation) throws Exception JavaDoc
179    {
180       CtConstructor ctMethod = ReflectToJavassist.constructorToJavassist(con);
181       return AnnotationElement.isAnyAnnotationPresent(ctMethod, annotation);
182
183    }
184
185    public static boolean isAnyAnnotationPresent(CtConstructor ctMethod, String JavaDoc annotation)
186    {
187       MethodInfo mi = ctMethod.getMethodInfo2();
188
189       AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag);
190       if (visible != null)
191       {
192          if (visible.getAnnotation(annotation) != null) return true;
193       }
194
195       AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
196       if (invisible != null)
197       {
198          if (invisible.getAnnotation(annotation) != null) return true;
199       }
200
201       return false;
202    }
203
204    public static boolean isInvisibleAnnotationPresent(Class JavaDoc clazz, String JavaDoc annotation) throws Exception JavaDoc
205    {
206       if (clazz == Void.TYPE) return false;
207       ClassFile cf = AnnotationElement.getClassFile(clazz);
208
209       AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag);
210       if (invisible == null) return false;
211
212       return invisible.getAnnotation(annotation) != null;
213    }
214
215    public static boolean isAnyAnnotationPresent(CtClass clazz, String JavaDoc annotation) throws Exception JavaDoc
216    {
217       if (clazz == CtClass.voidType) return false;
218       ClassFile cf = clazz.getClassFile2();
219       AnnotationsAttribute visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag);
220       if (visible != null)
221       {
222          if (visible.getAnnotation(annotation) != null) return true;
223       }
224
225       AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag);
226       if (invisible != null)
227       {
228          if (invisible.getAnnotation(annotation) != null) return true;
229       }
230
231       return false;
232    }
233
234    public static boolean isAnyAnnotationPresent(Class JavaDoc clazz, String JavaDoc annotation) throws Exception JavaDoc
235    {
236       if (clazz == Void.TYPE) return false;
237       ClassFile cf = AnnotationElement.getClassFile(clazz);
238       AnnotationsAttribute visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag);
239       if (visible != null)
240       {
241          if (visible.getAnnotation(annotation) != null) return true;
242       }
243
244       AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag);
245       if (invisible != null)
246       {
247          if (invisible.getAnnotation(annotation) != null) return true;
248       }
249
250       return false;
251    }
252
253    protected static ClassFile getClassFile(Class JavaDoc clazz) throws NotFoundException
254    {
255       ClassPool pool = AspectManager.instance().findClassPool(clazz.getClassLoader());
256       CtClass ct = pool.get(clazz.getName());
257       ClassFile cf = ct.getClassFile2();
258       return cf;
259    }
260
261    protected static Object JavaDoc create(AnnotationsAttribute group, Class JavaDoc annotation) throws Exception JavaDoc
262    {
263       if (group == null) return null;
264       javassist.bytecode.annotation.Annotation info = group.getAnnotation(annotation.getName());
265       if (info == null) return null;
266       return AnnotationProxy.createProxy(info, annotation);
267    }
268
269    public static Object JavaDoc getInvisibleAnnotation(Method method, Class JavaDoc annotation)
270    {
271       try
272       {
273          CtMethod ctMethod = ReflectToJavassist.methodToJavassist(method);
274          if (ctMethod == null)
275          {
276             return null;
277          }
278          MethodInfo mi = ctMethod.getMethodInfo2();
279
280          AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
281          if (invisible == null) return null;
282
283          return create(invisible, annotation);
284       }
285       catch (Exception JavaDoc e)
286       {
287          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
288
}
289    }
290
291    public static Object JavaDoc getInvisibleAnnotation(Constructor con, Class JavaDoc annotation)
292    {
293       try
294       {
295          CtConstructor ctMethod = ReflectToJavassist.constructorToJavassist(con);
296          MethodInfo mi = ctMethod.getMethodInfo2();
297
298
299          AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
300          if (invisible == null) return null;
301
302          return create(invisible, annotation);
303       }
304       catch (Exception JavaDoc e)
305       {
306          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
307
}
308    }
309
310    public static Object JavaDoc getInvisibleAnnotation(Field field, Class JavaDoc annotation)
311    {
312       try
313       {
314          CtField ctField = ReflectToJavassist.fieldToJavassist(field);
315          FieldInfo mi = ctField.getFieldInfo2();
316
317
318          AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
319          if (invisible == null) return null;
320
321          return create(invisible, annotation);
322       }
323       catch (Exception JavaDoc e)
324       {
325          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
326
}
327    }
328
329    public static Object JavaDoc getInvisibleAnnotation(Class JavaDoc clazz, Class JavaDoc annotation)
330    {
331       try
332       {
333          if (clazz == Void.TYPE) return null;
334          ClassFile cf = getClassFile(clazz);
335
336          AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag);
337          if (invisible == null) return null;
338
339          return create(invisible, annotation);
340       }
341       catch (Exception JavaDoc e)
342       {
343          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
344
}
345    }
346
347    /**
348     * If invisble or visible annotation is present for method, then return it
349     *
350     * @param method
351     * @param annotation
352     * @return
353     */

354    public static Object JavaDoc getAnyAnnotation(Method method, Class JavaDoc annotation)
355    {
356       Object JavaDoc rtn = AnnotationElement.getVisibleAnnotation(method, annotation);
357       if (rtn != null) return rtn;
358       return getInvisibleAnnotation(method, annotation);
359    }
360
361    /**
362     * If con has a invisible or visible annotation return it
363     *
364     * @param con
365     * @param annotation
366     * @return
367     */

368    public static Object JavaDoc getAnyAnnotation(Constructor con, Class JavaDoc annotation)
369    {
370       Object JavaDoc rtn = AnnotationElement.getVisibleAnnotation(con, annotation);
371       if (rtn != null) return rtn;
372       return getInvisibleAnnotation(con, annotation);
373    }
374
375    public static Object JavaDoc getAnyAnnotation(Field field, Class JavaDoc annotation)
376    {
377       Object JavaDoc rtn = AnnotationElement.getVisibleAnnotation(field, annotation);
378       if (rtn != null) return rtn;
379       return getInvisibleAnnotation(field, annotation);
380    }
381
382    public static Object JavaDoc getAnyAnnotation(Class JavaDoc clazz, Class JavaDoc annotation)
383    {
384       if (clazz == Void.TYPE) return null;
385       Object JavaDoc rtn = AnnotationElement.getVisibleAnnotation(clazz, annotation);
386       if (rtn != null) return rtn;
387       return getInvisibleAnnotation(clazz, annotation);
388    }
389
390    public static boolean isAnyAnnotationPresent(Field field, Class JavaDoc annotation) throws Exception JavaDoc
391    {
392       if (AnnotationElement.isVisibleAnnotationPresent(field, annotation)) return true;
393       CtField ctMethod = ReflectToJavassist.fieldToJavassist(field);
394       return isInvisibleAnnotationPresent(ctMethod, annotation.getName());
395    }
396
397    public static boolean isAnyAnnotationPresent(Class JavaDoc clazz, Class JavaDoc annotation) throws Exception JavaDoc
398    {
399       if (clazz == Void.TYPE) return false;
400       if (AnnotationElement.isVisibleAnnotationPresent(clazz, annotation)) return true;
401       ClassFile cf = getClassFile(clazz);
402
403       AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag);
404       if (invisible == null) return false;
405
406       return invisible.getAnnotation(annotation.getName()) != null;
407    }
408
409    public static boolean isAnyAnnotationPresent(Constructor con, Class JavaDoc annotation) throws Exception JavaDoc
410    {
411       if (AnnotationElement.isVisibleAnnotationPresent(con, annotation)) return true;
412       CtConstructor ctMethod = ReflectToJavassist.constructorToJavassist(con);
413       return isVisibleAnnotationPresent(ctMethod, annotation.getName());
414
415
416    }
417
418    public static boolean isAnyAnnotationPresent(Method method, Class JavaDoc annotation) throws Exception JavaDoc
419    {
420       if (AnnotationElement.isVisibleAnnotationPresent(method, annotation)) return true;
421       CtMethod ctMethod = ReflectToJavassist.methodToJavassist(method);
422       if (ctMethod == null) return false;
423       MethodInfo mi = ctMethod.getMethodInfo2();
424
425
426       AnnotationsAttribute invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
427       if (invisible == null) return false;
428
429       return invisible.getAnnotation(annotation.getName()) != null;
430    }
431
432    public static boolean isVisibleAnnotationPresent(Field field, String JavaDoc annotation) throws Exception JavaDoc
433    {
434       CtField ctMethod = ReflectToJavassist.fieldToJavassist(field);
435       return isVisibleAnnotationPresent(ctMethod, annotation);
436    }
437
438    public static boolean isVisibleAnnotationPresent(Class JavaDoc clazz, String JavaDoc annotation) throws Exception JavaDoc
439    {
440       if (clazz == Void.TYPE) return false;
441
442       ClassFile cf = getClassFile(clazz);
443
444       AnnotationsAttribute visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag);
445       if (visible == null) return false;
446
447       return visible.getAnnotation(annotation) != null;
448    }
449
450    public static boolean isVisibleAnnotationPresent(Constructor con, String JavaDoc annotation) throws Exception JavaDoc
451    {
452       CtConstructor ctMethod = ReflectToJavassist.constructorToJavassist(con);
453       return isVisibleAnnotationPresent(ctMethod, annotation);
454
455
456    }
457
458    public static boolean isVisibleAnnotationPresent(Method method, String JavaDoc annotation) throws Exception JavaDoc
459    {
460       CtMethod ctMethod = ReflectToJavassist.methodToJavassist(method);
461       if (ctMethod == null) return false;
462       MethodInfo mi = ctMethod.getMethodInfo2();
463
464
465       AnnotationsAttribute visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag);
466       if (visible == null) return false;
467
468       return visible.getAnnotation(annotation) != null;
469    }
470
471    public static Object JavaDoc[] getVisibleAnnotations(Class JavaDoc clazz) throws Exception JavaDoc
472    {
473       return AnnotationElement.getVisibleAnnotations(clazz);
474    }
475
476    public static Object JavaDoc[] getVisibleAnnotations(Method m) throws Exception JavaDoc
477    {
478       return AnnotationElement.getVisibleAnnotations(m);
479    }
480       
481    public static Object JavaDoc[] getVisibleAnnotations(Field f) throws Exception JavaDoc
482    {
483       return AnnotationElement.getVisibleAnnotations(f);
484    }
485    
486    public static Object JavaDoc[] getVisibleAnnotations(Constructor c) throws Exception JavaDoc
487    {
488       return AnnotationElement.getVisibleAnnotations(c);
489    }
490       
491    public static Class JavaDoc getAnnotationType(Object JavaDoc o)
492    {
493       Class JavaDoc proxy = o.getClass();
494       if (Proxy.isProxyClass(proxy))
495       {
496          Class JavaDoc[] interfaces = proxy.getInterfaces();
497          if (interfaces.length == 1)
498          {
499             return interfaces[0];
500          }
501       }
502      return null;
503    }
504 }
505
Popular Tags