KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Tools > Resolver > ClassInfo


1 /* $Id: ClassInfo.java,v 1.2 2004/05/20 14:23:53 bures Exp $ */
2 package SOFA.Tools.Resolver;
3
4 import org.objectweb.asm.ClassReader;
5 import org.objectweb.asm.ClassVisitor;
6 import org.objectweb.asm.CodeVisitor;
7
8 /** Provides information about class (read from byte code).
9   *
10   * @author Petr Hnetynka
11   */

12 public class ClassInfo {
13   
14   private String JavaDoc name;
15
16   public ClassInfo(String JavaDoc fname) throws java.io.IOException JavaDoc {
17     name = null;
18     java.io.FileInputStream JavaDoc is = new java.io.FileInputStream JavaDoc(fname);
19     ClassReader cr = new ClassReader(is);
20     ClassVisitor cv = new ClassInfoVisitor(this);
21     cr.accept(cv, false);
22   }
23
24   public String JavaDoc className() {
25     return name;
26   }
27
28   void className(String JavaDoc name) {
29     this.name = name;
30   }
31
32   public static void main(String JavaDoc [] argv) throws Exception JavaDoc {
33     ClassInfo info = new ClassInfo(argv[0]);
34     System.out.println("Class name: "+info.className());
35   }
36 }
37
38 class ClassInfoVisitor implements ClassVisitor {
39   private ClassInfo info;
40   public ClassInfoVisitor(ClassInfo info) {
41     this.info = info;
42   }
43   
44   public void visit(final int access, final String JavaDoc name, final String JavaDoc superName, final String JavaDoc[] interfaces, final String JavaDoc sourceFile) {
45     info.className(name);
46   }
47   public void visitField(final int access, final String JavaDoc name, final String JavaDoc desc, final Object JavaDoc value) {}
48   public CodeVisitor visitMethod(final int access, final String JavaDoc name, final String JavaDoc desc, final String JavaDoc[] exceptions) {
49     return null;
50   }
51   public void visitEnd() {}
52   public void visitInnerClass(String JavaDoc name, String JavaDoc outerName, String JavaDoc innerName, int access) {}
53 }
54
Popular Tags