comparison src/com/go/trove/classfile/TestClassFileRead.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 /* ====================================================================
2 * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3 * ====================================================================
4 * The Tea Software License, Version 1.1
5 *
6 * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The end-user documentation included with the redistribution,
21 * if any, must include the following acknowledgment:
22 * "This product includes software developed by the
23 * Walt Disney Internet Group (http://opensource.go.com/)."
24 * Alternately, this acknowledgment may appear in the software itself,
25 * if and wherever such third-party acknowledgments normally appear.
26 *
27 * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28 * not be used to endorse or promote products derived from this
29 * software without prior written permission. For written
30 * permission, please contact opensource@dig.com.
31 *
32 * 5. Products derived from this software may not be called "Tea",
33 * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34 * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35 * written permission of the Walt Disney Internet Group.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * For more information about Tea, please see http://opensource.go.com/.
51 */
52
53 package com.go.trove.classfile;
54
55 import java.io.*;
56
57 /******************************************************************************
58 * Reads a class file and prints out its contents.
59 *
60 * @author Brian S O'Neill
61 * @version
62 * <!--$$Revision: 1.1 $-->, <!--$$JustDate:--> 00/12/27 <!-- $-->
63 */
64 public class TestClassFileRead {
65 /**
66 * @param args first argument is path to class file.
67 */
68 public static void main(String[] args) throws Exception {
69 InputStream in = new FileInputStream(args[0]);
70 in = new BufferedInputStream(in);
71
72 ClassFileDataLoader loader = new ResourceClassFileDataLoader();
73
74 ClassFile cf = ClassFile.readFrom(in, loader, null);
75 in.close();
76
77 while (cf.getOuterClass() != null) {
78 cf = cf.getOuterClass();
79 }
80
81 dump(cf);
82 }
83
84 private static void dump(ClassFile cf) {
85 dump(cf, "");
86 }
87
88 private static void dump(ClassFile cf, String indent) {
89 println(cf, indent);
90 println("className: " + cf.getClassName(), indent);
91 println("superClassName: " + cf.getSuperClassName(), indent);
92 println("innerClass: " + cf.isInnerClass(), indent);
93 println("innerClassName: " + cf.getInnerClassName(), indent);
94 println("type: " + cf.getType(), indent);
95 println("accessFlags: " + cf.getAccessFlags(), indent);
96
97 String[] interfaces = cf.getInterfaces();
98 print("interfaces: ", indent);
99 for (int i=0; i<interfaces.length; i++) {
100 if (i > 0) {
101 System.out.print(", ");
102 }
103 System.out.print(interfaces[i]);
104 }
105 println();
106
107 FieldInfo[] fields = cf.getFields();
108 println("fields: ", indent);
109 for (int i=0; i<fields.length; i++) {
110 dump(fields[i], indent + " ");
111 }
112
113 MethodInfo[] methods = cf.getMethods();
114 println("methods: ", indent);
115 for (int i=0; i<methods.length; i++) {
116 dump(methods[i], indent + " ");
117 }
118
119 methods = cf.getConstructors();
120 println("constructors: ", indent);
121 for (int i=0; i<methods.length; i++) {
122 dump(methods[i], indent + " ");
123 }
124
125 MethodInfo init = cf.getInitializer();
126 println("initializer: ", indent);
127 if (init != null) {
128 dump(init, indent + " ");
129 }
130
131 ClassFile[] innerClasses = cf.getInnerClasses();
132 println("innerClasses: ", indent);
133 for (int i=0; i<innerClasses.length; i++) {
134 dump(innerClasses[i], indent + " ");
135 }
136
137 println("sourceFile: " + cf.getSourceFile(), indent);
138 println("synthetic: " + cf.isSynthetic(), indent);
139 println("deprecated: " + cf.isDeprecated(), indent);
140 println("attributes: ", indent);
141 dump(cf.getAttributes(), indent + " ");
142
143 println();
144 }
145
146 private static void dump(FieldInfo field, String indent) {
147 println(field, indent);
148 println("name: " + field.getName(), indent);
149 println("type: " + field.getType(), indent);
150 println("accessFlags: " + field.getAccessFlags(), indent);
151 println("constantValue: " + field.getConstantValue(), indent);
152 println("synthetic: " + field.isSynthetic(), indent);
153 println("deprecated: " + field.isDeprecated(), indent);
154 println("attributes: ", indent);
155 dump(field.getAttributes(), indent + " ");
156
157 println();
158 }
159
160 private static void dump(MethodInfo method, String indent) {
161 println(method, indent);
162 println("name: " + method.getName(), indent);
163 println("methodDescriptor: " + method.getMethodDescriptor(), indent);
164 println("accessFlags: " + method.getAccessFlags(), indent);
165
166 String[] exceptions = method.getExceptions();
167 print("exceptions: ", indent);
168 for (int i=0; i<exceptions.length; i++) {
169 if (i > 0) {
170 System.out.print(", ");
171 }
172 System.out.print(exceptions[i]);
173 }
174 println();
175
176 if (method.getCodeAttr() != null) {
177 println("code:", indent);
178
179 PrintWriter writer = new PrintWriter(System.out);
180
181 TypeDescriptor[] paramTypes =
182 method.getMethodDescriptor().getParameterTypes();
183 boolean isStatic = method.getAccessFlags().isStatic();
184
185 new CodeDisassembler(method).disassemble
186 (new CodeAssemblerPrinter(paramTypes, isStatic,
187 writer, indent + " ", null));
188
189 writer.flush();
190 }
191
192 println("synthetic: " + method.isSynthetic(), indent);
193 println("deprecated: " + method.isDeprecated(), indent);
194 println("attributes: ", indent);
195 dump(method.getAttributes(), indent + " ");
196
197 println();
198 }
199
200 private static void dump(Attribute[] attributes, String indent) {
201 if (attributes == null) {
202 return;
203 }
204 for (int i=0; i<attributes.length; i++) {
205 Attribute attribute = attributes[i];
206 println(attribute, indent);
207 Attribute[] subAttributes = attribute.getAttributes();
208 if (subAttributes != null && subAttributes.length > 0) {
209 println("attributes: ", indent);
210 dump(subAttributes, indent + " ");
211 }
212 }
213 }
214
215 private static void print(Object obj, String indent) {
216 System.out.print(indent);
217 System.out.print(obj);
218 }
219
220 private static void println(Object obj, String indent) {
221 print(obj, indent);
222 println();
223 }
224
225 private static void println() {
226 System.out.println();
227 }
228 }