KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cojen > classfile > DisassemblyTool


1 /*
2  * Copyright 2004 Brian S O'Neill
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.cojen.classfile;
18
19 import java.io.BufferedInputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Comparator JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.cojen.classfile.attribute.CodeAttr;
35 import org.cojen.classfile.attribute.SignatureAttr;
36
37 /**
38  * Disassembles a class file, sending the results to standard out. The class
39  * can be specified by name or by a file name. If the class is specified by
40  * name, it must be available in the classpath.
41  * <p>
42  * Two output formats are supported: assembly and builder. The assembly format
43  * is the default, and it produces a pseudo Java source file, where the method
44  * bodies contain JVM assembly code.
45  * <p>
46  * The builder format produces a valid Java file, which uses the Cojen
47  * classfile API. When compiled and run, it rebuilds the original class and
48  * inner classes. This format makes it easier to understand how to use the
49  * classfile API to generate new classes.
50  *
51  * @author Brian S O'Neill
52  */

53 public class DisassemblyTool {
54     /**
55      * Disassembles a class file, sending the results to standard out.
56      *
57      * <pre>
58      * DisassemblyTool [-f &lt;format style&gt;] &lt;file or class name&gt;
59      * </pre>
60      *
61      * The format style may be "assembly" (the default) or "builder".
62      */

63     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
64         if (args.length == 0) {
65             System.out.println("DisassemblyTool [-f <format style>] <file or class name>");
66             System.out.println();
67             System.out.println("The format style may be \"assembly\" (the default) or \"builder\"");
68             return;
69         }
70
71         String JavaDoc style;
72         String JavaDoc name;
73
74         if ("-f".equals(args[0])) {
75             style = args[1];
76             name = args[2];
77         } else {
78             style = "assembly";
79             name = args[0];
80         }
81
82         ClassFileDataLoader loader;
83         InputStream JavaDoc in;
84
85         try {
86             final File JavaDoc file = new File JavaDoc(name);
87             in = new FileInputStream JavaDoc(file);
88             loader = new ClassFileDataLoader() {
89                 public InputStream JavaDoc getClassData(String JavaDoc name)
90                     throws IOException JavaDoc
91                 {
92                     name = name.substring(name.lastIndexOf('.') + 1);
93                     File JavaDoc f = new File JavaDoc(file.getParentFile(), name + ".class");
94
95                     if (f.exists()) {
96                         return new FileInputStream JavaDoc(f);
97                     }
98
99                     return null;
100                 }
101             };
102         } catch (FileNotFoundException JavaDoc e) {
103             if (name.endsWith(".class")) {
104                 System.err.println(e);
105                 return;
106             }
107
108             loader = new ResourceClassFileDataLoader();
109             in = loader.getClassData(name);
110
111             if (in == null) {
112                 System.err.println(e);
113                 return;
114             }
115         }
116
117         in = new BufferedInputStream JavaDoc(in);
118         ClassFile cf = ClassFile.readFrom(in, loader, null);
119
120         PrintWriter JavaDoc out = new PrintWriter JavaDoc(System.out);
121
122         Printer p;
123         if (style == null || style.equals("assembly")) {
124             p = new AssemblyStylePrinter();
125         } else if (style.equals("builder")) {
126             p = new BuilderStylePrinter();
127         } else {
128             System.err.println("Unknown format style: " + style);
129             return;
130         }
131
132         p.disassemble(cf, out);
133
134         out.flush();
135     }
136
137     public static interface Printer {
138         void disassemble(ClassFile cf, PrintWriter JavaDoc out);
139     }
140 }
141
Popular Tags