001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package EDU.purdue.cs.bloat.dump;
022:
023: import java.util.*;
024:
025: import EDU.purdue.cs.bloat.context.*;
026: import EDU.purdue.cs.bloat.editor.*;
027: import EDU.purdue.cs.bloat.file.*;
028: import EDU.purdue.cs.bloat.reflect.*;
029:
030: /**
031: * Prints the contents of a Java classfile to the console.
032: */
033: public class Main implements Opcode {
034: public static void main(final String[] args) {
035: final ClassFileLoader loader = new ClassFileLoader();
036: String className = null;
037:
038: for (int i = 0; i < args.length; i++) {
039: if (args[i].equals("-help")) {
040: Main.usage();
041: } else if (args[i].equals("-classpath")) {
042: if (++i >= args.length) {
043: Main.usage();
044: }
045:
046: final String classpath = args[i];
047: loader.setClassPath(classpath);
048: } else if (args[i].startsWith("-")) {
049: Main.usage();
050: } else {
051: if (className != null) {
052: Main.usage();
053: }
054: className = args[i];
055: }
056: }
057:
058: if (className == null) {
059: Main.usage();
060: }
061:
062: ClassInfo info = null;
063:
064: try {
065: info = loader.loadClass(className);
066: } catch (final ClassNotFoundException ex) {
067: System.err.println("Couldn't find class: "
068: + ex.getMessage());
069: System.exit(1);
070: }
071:
072: final Collection classes = new ArrayList(1);
073: classes.add(className);
074:
075: final BloatContext context = new CachingBloatContext(loader,
076: classes, true);
077:
078: if (info != null) {
079: Main.printClass(context, info);
080: }
081: }
082:
083: private static void usage() {
084: System.err
085: .println("Usage: java EDU.purdue.cs.bloat.dump.Main "
086: + "\n [-options] class"
087: + "\n"
088: + "\nwhere options include:"
089: + "\n -help print out this message"
090: + "\n -classpath <directories separated by colons>"
091: + "\n list directories in which to look for classes");
092: System.exit(0);
093: }
094:
095: private static void printClass(final EditorContext context,
096: final ClassInfo info) {
097: final ClassEditor c = context.editClass(info);
098:
099: if (c.isPublic()) {
100: System.out.print("public ");
101: } else if (c.isPrivate()) {
102: System.out.print("private ");
103: } else if (c.isProtected()) {
104: System.out.print("protected ");
105: }
106:
107: if (c.isStatic()) {
108: System.out.print("static ");
109: }
110:
111: if (c.isFinal()) {
112: System.out.print("final ");
113: }
114:
115: if (c.isInterface()) {
116: System.out.print("interface ");
117: } else if (c.isAbstract()) {
118: System.out.print("abstract class ");
119: } else {
120: System.out.print("class ");
121: }
122:
123: System.out.print(c.type().className());
124:
125: if (c.super class() != null) {
126: System.out.print(" extends " + c.super class().className());
127: }
128:
129: final Type[] interfaces = c.interfaces();
130:
131: for (int i = 0; i < interfaces.length; i++) {
132: if (i == 0) {
133: System.out.print(" implements");
134: } else {
135: System.out.print(",");
136: }
137:
138: System.out.print(" " + interfaces[i].className());
139: }
140:
141: System.out.println();
142: System.out.println("{");
143:
144: final FieldInfo[] fields = c.fields();
145:
146: for (int i = 0; i < fields.length; i++) {
147: FieldEditor f = null;
148:
149: try {
150: f = context.editField(fields[i]);
151: } catch (final ClassFormatException ex) {
152: System.err.println(ex.getMessage());
153: System.exit(1);
154: }
155:
156: System.out.print(" ");
157:
158: if (f.isPublic()) {
159: System.out.print("public ");
160: } else if (f.isPrivate()) {
161: System.out.print("private ");
162: } else if (f.isProtected()) {
163: System.out.print("protected ");
164: }
165:
166: if (f.isTransient()) {
167: System.out.print("transient ");
168: }
169:
170: if (f.isVolatile()) {
171: System.out.print("volatile ");
172: }
173:
174: if (f.isStatic()) {
175: System.out.print("static ");
176: }
177:
178: if (f.isFinal()) {
179: System.out.print("final ");
180: }
181:
182: System.out.println(f.type() + " " + f.name());
183:
184: context.release(fields[i]);
185: }
186:
187: if (fields.length != 0) {
188: System.out.println();
189: }
190:
191: final MethodInfo[] methods = c.methods();
192:
193: for (int i = 0; i < methods.length; i++) {
194: MethodEditor m = null;
195:
196: try {
197: m = context.editMethod(methods[i]);
198: } catch (final ClassFormatException ex) {
199: System.err.println(ex.getMessage());
200: System.exit(1);
201: }
202:
203: if (i != 0) {
204: System.out.println();
205: }
206:
207: System.out.print(" ");
208:
209: if (m.isPublic()) {
210: System.out.print("public ");
211: } else if (m.isPrivate()) {
212: System.out.print("private ");
213: } else if (m.isProtected()) {
214: System.out.print("protected ");
215: }
216:
217: if (m.isNative()) {
218: System.out.print("native ");
219: }
220:
221: if (m.isSynchronized()) {
222: System.out.print("synchronized ");
223: }
224:
225: if (m.isAbstract()) {
226: System.out.print("abstract ");
227: }
228:
229: if (m.isStatic()) {
230: System.out.print("static ");
231: }
232:
233: if (m.isFinal()) {
234: System.out.print("final ");
235: }
236:
237: System.out.println(m.type() + " " + m.name());
238:
239: final Iterator iter = m.code().iterator();
240:
241: while (iter.hasNext()) {
242: final Object obj = iter.next();
243: System.out.println(" " + obj);
244: }
245:
246: context.release(methods[i]);
247: }
248:
249: System.out.println("}");
250:
251: context.release(info);
252: }
253: }
|