001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.classreader;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: public class TextPrinter extends Printer {
039: private boolean top = true;
040:
041: public TextPrinter(PrintWriter out) {
042: super (out);
043: }
044:
045: public void visitClassfile(Classfile classfile) {
046: classfile.getConstantPool().accept(this );
047:
048: append(classfile.getDeclaration()).append(" {").eol();
049:
050: for (Field_info field : classfile.getAllFields()) {
051: field.accept(this );
052: }
053:
054: for (Method_info method : classfile.getAllMethods()) {
055: method.accept(this );
056: }
057:
058: append("}").eol();
059: }
060:
061: public void visitClass_info(Class_info entry) {
062: if (top) {
063: top = false;
064: append(currentCount()).append(": ");
065: append("Class ");
066: entry.getRawName().accept(this );
067: eol();
068: top = true;
069: } else {
070: entry.getRawName().accept(this );
071: }
072: }
073:
074: public void visitFieldRef_info(FieldRef_info entry) {
075: Class_info c = entry.getRawClass();
076: NameAndType_info nat = entry.getRawNameAndType();
077:
078: if (top) {
079: top = false;
080: append(currentCount()).append(": ");
081: append("Field ");
082: nat.getRawType().accept(this );
083: append(" ");
084: c.accept(this );
085: append(".");
086: nat.getRawName().accept(this );
087: eol();
088: top = true;
089: } else {
090: nat.getRawType().accept(this );
091: append(" ");
092: c.accept(this );
093: append(".");
094: nat.getRawName().accept(this );
095: }
096: }
097:
098: public void visitMethodRef_info(MethodRef_info entry) {
099: Class_info c = entry.getRawClass();
100: NameAndType_info nat = entry.getRawNameAndType();
101:
102: if (top) {
103: top = false;
104: append(currentCount()).append(": ");
105: append("Method ");
106: c.accept(this );
107: append(".");
108: nat.getRawName().accept(this );
109: nat.getRawType().accept(this );
110: eol();
111: top = true;
112: } else {
113: c.accept(this );
114: append(".");
115: nat.getRawName().accept(this );
116: nat.getRawType().accept(this );
117: }
118: }
119:
120: public void visitInterfaceMethodRef_info(
121: InterfaceMethodRef_info entry) {
122: Class_info c = entry.getRawClass();
123: NameAndType_info nat = entry.getRawNameAndType();
124:
125: if (top) {
126: top = false;
127: append(currentCount()).append(": ");
128: append("Interface Method ");
129: c.accept(this );
130: append(".");
131: nat.getRawName().accept(this );
132: nat.getRawType().accept(this );
133: eol();
134: top = true;
135: } else {
136: c.accept(this );
137: append(".");
138: nat.getRawName().accept(this );
139: nat.getRawType().accept(this );
140: }
141: }
142:
143: public void visitString_info(String_info entry) {
144: if (top) {
145: top = false;
146: append(currentCount()).append(": String \"");
147: entry.getRawValue().accept(this );
148: append("\"").eol();
149: top = true;
150: } else {
151: entry.getRawValue().accept(this );
152: }
153: }
154:
155: public void visitInteger_info(Integer_info entry) {
156: if (top) {
157: append(currentCount()).append(": Integer ").append(
158: entry.getValue()).eol();
159: } else {
160: append(entry.getValue());
161: }
162: }
163:
164: public void visitFloat_info(Float_info entry) {
165: if (top) {
166: append(currentCount()).append(": Float ").append(
167: entry.getValue()).eol();
168: } else {
169: append(entry.getValue());
170: }
171: }
172:
173: public void visitLong_info(Long_info entry) {
174: if (top) {
175: append(currentCount()).append(": Long ").append(
176: entry.getValue()).eol();
177: } else {
178: append(entry.getValue());
179: }
180: }
181:
182: public void visitDouble_info(Double_info entry) {
183: if (top) {
184: append(currentCount()).append(": Double ").append(
185: entry.getValue()).eol();
186: } else {
187: append(entry.getValue());
188: }
189: }
190:
191: public void visitNameAndType_info(NameAndType_info entry) {
192: if (top) {
193: top = false;
194: append(currentCount()).append(": Name and Type ");
195: entry.getRawName().accept(this );
196: append(" ");
197: entry.getRawType().accept(this );
198: eol();
199: top = true;
200: } else {
201: entry.getRawName().accept(this );
202: append(" ");
203: entry.getRawType().accept(this );
204: }
205: }
206:
207: public void visitUTF8_info(UTF8_info entry) {
208: if (top) {
209: append(currentCount()).append(": \"").append(
210: entry.getValue()).append("\"").eol();
211: } else {
212: append(entry.getValue());
213: }
214: }
215:
216: public void visitField_info(Field_info entry) {
217: append(" ").append(entry.getDeclaration()).append(";").eol();
218: }
219:
220: public void visitMethod_info(Method_info entry) {
221: append(" ").append(entry.getDeclaration()).append(";").eol();
222: }
223: }
|