KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > classreader > TextPrinter


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.classreader;
34
35 import java.io.*;
36 import java.util.*;
37
38 public class TextPrinter extends Printer {
39     private boolean top = true;
40
41     public TextPrinter(PrintWriter out) {
42         super(out);
43     }
44     
45     public void visitClassfile(Classfile classfile) {
46         classfile.getConstantPool().accept(this);
47
48         append(classfile.getDeclaration()).append(" {").eol();
49
50         Iterator i;
51
52         i = classfile.getAllFields().iterator();
53         while (i.hasNext()) {
54             ((Visitable) i.next()).accept(this);
55         }
56
57         i = classfile.getAllMethods().iterator();
58         while (i.hasNext()) {
59             ((Visitable) i.next()).accept(this);
60         }
61
62         append("}").eol();
63     }
64
65     public void visitClass_info(Class_info entry) {
66         if (top) {
67             top = false;
68             append(currentCount()).append(": ");
69             append("Class ");
70             entry.getRawName().accept(this);
71             eol();
72             top = true;
73         } else {
74             entry.getRawName().accept(this);
75         }
76     }
77
78     public void visitFieldRef_info(FieldRef_info entry) {
79         Class_info c = entry.getRawClass();
80         NameAndType_info nat = entry.getRawNameAndType();
81
82         if (top) {
83             top = false;
84             append(currentCount()).append(": ");
85             append("Field ");
86             nat.getRawType().accept(this);
87             append(" ");
88             c.accept(this);
89             append(".");
90             nat.getRawName().accept(this);
91             eol();
92             top = true;
93         } else {
94             nat.getRawType().accept(this);
95             append(" ");
96             c.accept(this);
97             append(".");
98             nat.getRawName().accept(this);
99         }
100     }
101
102     public void visitMethodRef_info(MethodRef_info entry) {
103         Class_info c = entry.getRawClass();
104         NameAndType_info nat = entry.getRawNameAndType();
105
106         if (top) {
107             top = false;
108             append(currentCount()).append(": ");
109             append("Method ");
110             c.accept(this);
111             append(".");
112             nat.getRawName().accept(this);
113             nat.getRawType().accept(this);
114             eol();
115             top = true;
116         } else {
117             c.accept(this);
118             append(".");
119             nat.getRawName().accept(this);
120             nat.getRawType().accept(this);
121         }
122     }
123
124     public void visitInterfaceMethodRef_info(InterfaceMethodRef_info entry) {
125         Class_info c = entry.getRawClass();
126         NameAndType_info nat = entry.getRawNameAndType();
127
128         if (top) {
129             top = false;
130             append(currentCount()).append(": ");
131             append("Interface Method ");
132             c.accept(this);
133             append(".");
134             nat.getRawName().accept(this);
135             nat.getRawType().accept(this);
136             eol();
137             top = true;
138         } else {
139             c.accept(this);
140             append(".");
141             nat.getRawName().accept(this);
142             nat.getRawType().accept(this);
143         }
144     }
145
146     public void visitString_info(String_info entry) {
147         if (top) {
148             top = false;
149             append(currentCount()).append(": String ");
150             entry.getRawValue().accept(this);
151             eol();
152             top = true;
153         } else {
154             entry.getRawValue().accept(this);
155         }
156     }
157
158     public void visitInteger_info(Integer_info entry) {
159         if (top) {
160             append(currentCount()).append(": Integer ").append(entry.getValue()).eol();
161         } else {
162             append(entry.getValue());
163         }
164     }
165
166     public void visitFloat_info(Float_info entry) {
167         if (top) {
168             append(currentCount()).append(": Float ").append(entry.getValue()).eol();
169         } else {
170             append(entry.getValue());
171         }
172     }
173
174     public void visitLong_info(Long_info entry) {
175         if (top) {
176             append(currentCount()).append(": Long ").append(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(entry.getValue()).eol();
185         } else {
186             append(entry.getValue());
187         }
188     }
189
190     public void visitNameAndType_info(NameAndType_info entry) {
191         if (top) {
192             top = false;
193             append(currentCount()).append(": Name and Type ");
194             entry.getRawName().accept(this);
195             append(" ");
196             entry.getRawType().accept(this);
197             eol();
198             top = true;
199         } else {
200             entry.getRawName().accept(this);
201             append(" ");
202             entry.getRawType().accept(this);
203         }
204     }
205
206     public void visitUTF8_info(UTF8_info entry) {
207         if (top) {
208             append(currentCount()).append(": \"").append(entry.getValue()).append("\"").eol();
209         } else {
210             append(entry.getValue());
211         }
212     }
213
214     public void visitField_info(Field_info entry) {
215         append(" ").append(entry.getDeclaration()).append(";").eol();
216     }
217
218     public void visitMethod_info(Method_info entry) {
219         append(" ").append(entry.getDeclaration()).append(";").eol();
220     }
221 }
222
Popular Tags