KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > annotation > util > FindAnnotations


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.util;
23
24 import javassist.ClassPool;
25 import javassist.CtClass;
26 import javassist.bytecode.AnnotationsAttribute;
27 import javassist.bytecode.ClassFile;
28 import javassist.bytecode.FieldInfo;
29 import javassist.bytecode.MethodInfo;
30 import javassist.bytecode.annotation.Annotation;
31
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Set JavaDoc;
35
36 public class FindAnnotations
37 {
38    public static ClassPool pool;
39
40    public static void main(String JavaDoc[] args) throws Exception JavaDoc
41    {
42       pool = ClassPool.getDefault();
43       pool.appendSystemPath();
44       pool.insertClassPath(".");
45       CtClass clazz = pool.get(args[0]);
46       clazz.getClassFile2().getConstPool().print();
47       printClassAttributes(clazz);
48    }
49
50    public static void printAnnotations(Annotation[] annotations)
51    {
52       for (int i = 0; i < annotations.length; i++)
53       {
54          printAnnotation(annotations[i]);
55       }
56    }
57
58    public static void printClassAttributes(CtClass clazz) throws Exception JavaDoc
59    {
60       ClassFile cf = clazz.getClassFile();
61       AnnotationsAttribute invisible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.invisibleTag);
62       if (invisible != null)
63       {
64          System.out.println("** invisible class annotations **");
65          Annotation[] annotations = invisible.getAnnotations();
66          printAnnotations(annotations);
67       }
68
69       AnnotationsAttribute visible = (AnnotationsAttribute) cf.getAttribute(AnnotationsAttribute.visibleTag);
70       if (visible != null)
71       {
72          System.out.println("** visible class annotations **");
73          Annotation[] annotations = visible.getAnnotations();
74          printAnnotations(annotations);
75       }
76
77       System.out.println("**** method annotations ****");
78       List JavaDoc methods = cf.getMethods();
79       for (int i = 0; i < methods.size(); i++)
80       {
81          MethodInfo mi = (MethodInfo) methods.get(i);
82          System.out.println("method: " + mi.getName());
83          System.out.println("CONST POOL: ");
84          mi.getConstPool().print();
85          System.out.println("-------------");
86          invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
87          if (invisible != null)
88          {
89             System.out.println("** invisible method annotations **");
90             Annotation[] annotations = invisible.getAnnotations();
91             printAnnotations(annotations);
92          }
93
94          visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag);
95          if (visible != null)
96          {
97             System.out.println("** visible method annotations **");
98             Annotation[] annotations = visible.getAnnotations();
99             printAnnotations(annotations);
100          }
101
102          System.out.println("----");
103       }
104
105       System.out.println("**** field annotations ****");
106       List JavaDoc fields = cf.getFields();
107       for (int i = 0; i < fields.size(); i++)
108       {
109          FieldInfo mi = (FieldInfo) fields.get(i);
110          System.out.println("field: " + mi.getName());
111          System.out.println("CONST POOL: ");
112          mi.getConstPool().print();
113          System.out.println("field: " + mi.getName());
114          invisible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.invisibleTag);
115          if (invisible != null)
116          {
117             System.out.println("** invisible method annotations **");
118             Annotation[] annotations = invisible.getAnnotations();
119             printAnnotations(annotations);
120          }
121
122          visible = (AnnotationsAttribute) mi.getAttribute(AnnotationsAttribute.visibleTag);
123          if (visible != null)
124          {
125             System.out.println("** visible method annotations **");
126             Annotation[] annotations = visible.getAnnotations();
127             printAnnotations(annotations);
128          }
129
130          System.out.println("----");
131       }
132    }
133
134    public static void printAnnotation(Annotation info)
135    {
136       System.out.print("@" + info.getTypeName());
137       Set JavaDoc members = info.getMemberNames();
138       if (members != null)
139       {
140          System.out.print("(");
141          Iterator JavaDoc mit = members.iterator();
142          while (mit.hasNext())
143          {
144             String JavaDoc name = (String JavaDoc) mit.next();
145             System.out.print(name + "=" + info.getMemberValue(name).toString());
146             if (mit.hasNext()) System.out.print(", ");
147          }
148          System.out.print(")");
149       }
150       System.out.println("");
151    }
152 }
153
154
Popular Tags