KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > visitclass > PrintClass


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.visitclass;
21
22 import java.io.IOException JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.TreeSet JavaDoc;
28 import java.util.zip.ZipEntry JavaDoc;
29 import java.util.zip.ZipFile JavaDoc;
30
31 import org.apache.bcel.classfile.ClassParser;
32 import org.apache.bcel.classfile.Code;
33 import org.apache.bcel.classfile.JavaClass;
34 import org.apache.bcel.classfile.Method;
35
36
37 /**
38  * @author pugh
39  */

40 public class PrintClass {
41
42     /**
43      * @author pugh
44      */

45     static final class ZipEntryComparator implements Comparator JavaDoc<ZipEntry JavaDoc>, Serializable JavaDoc {
46         private static final long serialVersionUID = 1L;
47         public int compare(ZipEntry JavaDoc o1, ZipEntry JavaDoc o2) {
48             ZipEntry JavaDoc e1 = (ZipEntry JavaDoc) o1;
49             String JavaDoc s1 = e1.getName();
50             int pos1 = s1.lastIndexOf('/');
51             String JavaDoc p1 = "-";
52             if (pos1 >= 0)
53                 p1 = s1.substring(0, pos1);
54
55             ZipEntry JavaDoc e2 = (ZipEntry JavaDoc) o2;
56             String JavaDoc s2 = e2.getName();
57             int pos2 = s2.lastIndexOf('/');
58             String JavaDoc p2 = "-";
59             if (pos2 >= 0)
60                 p2 = s2.substring(0, pos2);
61             int r = p1.compareTo(p2);
62             if (r != 0)
63                 return r;
64             return s1.compareTo(s2);
65         }
66     }
67
68     static boolean code = false, constants = false;
69
70     public static void main(String JavaDoc argv[]) throws IOException JavaDoc {
71         String JavaDoc[] file_name = new String JavaDoc[argv.length];
72         int files = 0;
73         ClassParser parser = null;
74         String JavaDoc zip_file = null;
75
76         /*
77          * Parse command line arguments.
78          */

79         for (int i = 0; i < argv.length; i++) {
80             if (argv[i].charAt(0) == '-') { // command line switch
81
if (argv[i].equals("-constants"))
82                     constants = true;
83                 else if (argv[i].equals("-code"))
84                     code = true;
85                 else if (argv[i].equals("-zip"))
86                     zip_file = argv[++i];
87             } else if (argv[i].endsWith(".zip") || argv[i].endsWith(".jar"))
88                 zip_file = argv[i];
89             else { // add file name to list
90
file_name[files++] = argv[i];
91             }
92         }
93
94         if (!constants) code = true;
95         if (files == 0 && zip_file == null) {
96             System.err.println("list: No input files specified");
97         } else {
98
99             if (files == 0 && zip_file != null) {
100                 ZipFile JavaDoc z = new ZipFile JavaDoc(zip_file);
101                 TreeSet JavaDoc<ZipEntry JavaDoc> zipEntries = new TreeSet JavaDoc<ZipEntry JavaDoc>(
102                         new ZipEntryComparator());
103                 for (Enumeration JavaDoc<? extends ZipEntry JavaDoc> e = z
104                         .entries(); e.hasMoreElements();)
105                     zipEntries.add(e.nextElement());
106
107                 for (ZipEntry JavaDoc ze : zipEntries) {
108                     String JavaDoc name = ze.getName();
109                     if (name.endsWith(".class"))
110                         printClass(new ClassParser(z.getInputStream(ze), name));
111
112
113                     
114                 }
115             } else
116                 for (int i = 0; i < files; i++)
117                     if (file_name[i].endsWith(".class")) {
118                         if (zip_file == null)
119                             printClass(new ClassParser(file_name[i]));
120                         else
121                             printClass(new ClassParser(zip_file, file_name[i]));
122
123                     }
124         }
125
126     }
127
128     private static void printClass(ClassParser parser) throws IOException JavaDoc {
129         JavaClass java_class;
130         java_class = parser.parse();
131
132         if (constants || code)
133             System.out.println(java_class); // Dump the contents
134
if (constants) // Dump the constant pool ?
135
System.out.println(java_class.getConstantPool());
136
137         if (code) // Dump the method code ?
138
printCode(java_class.getMethods());
139     }
140
141     /**
142      * Dump the disassembled code of all methods in the class.
143      */

144     public static void printCode(Method[] methods) {
145         for (Method m : methods) {
146             System.out.println(m);
147             Code code = m.getCode();
148             if (code != null)
149                 System.out.println(code);
150
151         }
152     }
153 }
Popular Tags