KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > asm > util > TraceCodeVisitor


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

34
35 package org.logicalcobwebs.asm.util;
36
37 import org.logicalcobwebs.asm.CodeVisitor;
38 import org.logicalcobwebs.asm.Label;
39 import org.logicalcobwebs.asm.Attribute;
40
41 import java.util.HashMap JavaDoc;
42
43 /**
44  * A {@link PrintCodeVisitor PrintCodeVisitor} that prints a disassembled view
45  * of the code it visits.
46  */

47
48 public class TraceCodeVisitor extends PrintCodeVisitor {
49
50   /**
51    * The {@link CodeVisitor CodeVisitor} to which this visitor delegates calls.
52    * May be <tt>null</tt>.
53    */

54
55   protected final CodeVisitor cv;
56
57   /**
58    * The label names. This map associate String values to Label keys.
59    */

60
61   private final HashMap JavaDoc labelNames;
62
63   /**
64    * Constructs a new {@link TraceCodeVisitor TraceCodeVisitor} object.
65    *
66    * @param cv the code visitor to which this adapter must delegate calls. May
67    * be <tt>null</tt>.
68    */

69
70   public TraceCodeVisitor (final CodeVisitor cv) {
71     this.cv = cv;
72     this.labelNames = new HashMap JavaDoc();
73   }
74
75   public void printInsn (final int opcode) {
76     buf.append(" ")
77       .append(OPCODES[opcode])
78       .append("\n");
79
80     if (cv != null) {
81       cv.visitInsn(opcode);
82     }
83   }
84
85   public void printIntInsn (final int opcode, final int operand) {
86     buf.append(" ")
87       .append(OPCODES[opcode])
88       .append(" ").append(operand)
89       .append("\n");
90
91     if (cv != null) {
92       cv.visitIntInsn(opcode, operand);
93     }
94   }
95
96   public void printVarInsn (final int opcode, final int var) {
97     buf.append(" ")
98       .append(OPCODES[opcode])
99       .append(" ")
100       .append(var)
101       .append("\n");
102
103     if (cv != null) {
104       cv.visitVarInsn(opcode, var);
105     }
106   }
107
108   public void printTypeInsn (final int opcode, final String JavaDoc desc) {
109     buf.append(" ")
110       .append(OPCODES[opcode])
111       .append(" ")
112       .append(desc)
113       .append("\n");
114
115     if (cv != null) {
116       cv.visitTypeInsn(opcode, desc);
117     }
118   }
119
120   public void printFieldInsn (
121     final int opcode,
122     final String JavaDoc owner,
123     final String JavaDoc name,
124     final String JavaDoc desc)
125   {
126     buf.append(" ")
127       .append(OPCODES[opcode])
128       .append(" ")
129       .append(owner)
130       .append(" ")
131       .append(name)
132       .append(" ")
133       .append(desc)
134       .append("\n");
135
136     if (cv != null) {
137       cv.visitFieldInsn(opcode, owner, name, desc);
138     }
139   }
140
141   public void printMethodInsn (
142     final int opcode,
143     final String JavaDoc owner,
144     final String JavaDoc name,
145     final String JavaDoc desc)
146   {
147     buf.append(" ")
148       .append(OPCODES[opcode])
149       .append(" ")
150       .append(owner)
151       .append(" ")
152       .append(name)
153       .append(" ")
154       .append(desc)
155       .append("\n");
156
157     if (cv != null) {
158       cv.visitMethodInsn(opcode, owner, name, desc);
159     }
160   }
161
162   public void printJumpInsn (final int opcode, final Label label) {
163     buf.append(" ")
164       .append(OPCODES[opcode]).
165       append(" ");
166     appendLabel(label);
167     buf.append("\n");
168
169     if (cv != null) {
170       cv.visitJumpInsn(opcode, label);
171     }
172   }
173
174   public void printLabel (final Label label) {
175     buf.append(" ");
176     appendLabel(label);
177     buf.append(":\n");
178
179     if (cv != null) {
180       cv.visitLabel(label);
181     }
182   }
183
184   public void printLdcInsn (final Object JavaDoc cst) {
185     buf.append(" LDC ");
186     if (cst instanceof String JavaDoc) {
187       buf.append("\"").append(cst).append("\"");
188     } else {
189       buf.append(cst);
190     }
191     buf.append("\n");
192
193     if (cv != null) {
194       cv.visitLdcInsn(cst);
195     }
196   }
197
198   public void printIincInsn (final int var, final int increment) {
199     buf.append(" IINC ")
200       .append(var)
201       .append(" ")
202       .append(increment)
203       .append("\n");
204
205     if (cv != null) {
206       cv.visitIincInsn(var, increment);
207     }
208   }
209
210   public void printTableSwitchInsn (
211     final int min,
212     final int max,
213     final Label dflt,
214     final Label labels[])
215   {
216     buf.append(" TABLESWITCH\n");
217     for (int i = 0; i < labels.length; ++i) {
218       buf.append(" ")
219         .append(min + i)
220         .append(": ");
221       appendLabel(labels[i]);
222       buf.append("\n");
223     }
224     buf.append(" default: ");
225     appendLabel(dflt);
226     buf.append("\n");
227
228     if (cv != null) {
229       cv.visitTableSwitchInsn(min, max, dflt, labels);
230     }
231   }
232
233   public void printLookupSwitchInsn (
234     final Label dflt,
235     final int keys[],
236     final Label labels[])
237   {
238     buf.append(" LOOKUPSWITCH\n");
239     for (int i = 0; i < labels.length; ++i) {
240       buf.append(" ")
241         .append(keys[i])
242         .append(": ");
243       appendLabel(labels[i]);
244       buf.append("\n");
245     }
246     buf.append(" default: ");
247     appendLabel(dflt);
248     buf.append("\n");
249
250     if (cv != null) {
251       cv.visitLookupSwitchInsn(dflt, keys, labels);
252     }
253   }
254
255   public void printMultiANewArrayInsn (final String JavaDoc desc, final int dims) {
256     buf.append(" MULTIANEWARRAY ")
257       .append(desc)
258       .append(" ")
259       .append(dims)
260       .append("\n");
261
262     if (cv != null) {
263       cv.visitMultiANewArrayInsn(desc, dims);
264     }
265   }
266
267   public void printTryCatchBlock (
268     final Label start,
269     final Label end,
270     final Label handler,
271     final String JavaDoc type)
272   {
273     buf.append(" TRYCATCHBLOCK ");
274     appendLabel(start);
275     buf.append(" ");
276     appendLabel(end);
277     buf.append(" ");
278     appendLabel(handler);
279     buf.append(" ")
280       .append(type)
281       .append("\n");
282
283     if (cv != null) {
284       cv.visitTryCatchBlock(start, end, handler, type);
285     }
286   }
287
288   public void printMaxs (final int maxStack, final int maxLocals) {
289     buf.append(" MAXSTACK = ")
290       .append(maxStack)
291       .append("\n MAXLOCALS = ")
292       .append(maxLocals)
293       .append("\n\n");
294
295     if (cv != null) {
296       cv.visitMaxs(maxStack, maxLocals);
297     }
298   }
299
300   public void printLocalVariable (
301     final String JavaDoc name,
302     final String JavaDoc desc,
303     final Label start,
304     final Label end,
305     final int index)
306   {
307     buf.append(" LOCALVARIABLE ")
308       .append(name)
309       .append(" ")
310       .append(desc)
311       .append(" ");
312     appendLabel(start);
313     buf.append(" ");
314     appendLabel(end);
315     buf.append(" ")
316       .append(index)
317       .append("\n");
318
319     if (cv != null) {
320       cv.visitLocalVariable(name, desc, start, end, index);
321     }
322   }
323
324   public void printLineNumber (final int line, final Label start) {
325     buf.append(" LINENUMBER ")
326       .append(line)
327       .append(" ");
328     appendLabel(start);
329     buf.append("\n");
330
331     if (cv != null) {
332       cv.visitLineNumber(line, start);
333     }
334   }
335
336   public void printAttribute (final Attribute attr) {
337     buf.append(" CODE ATTRIBUTE ")
338       .append(attr.type)
339       .append("\n");
340
341     if (cv != null) {
342       cv.visitAttribute(attr);
343     }
344   }
345
346   /**
347    * Appends the name of the given label to {@link #buf buf}. Creates a new
348    * label name if the given label does not yet have one.
349    *
350    * @param l a label.
351    */

352
353   private void appendLabel (final Label l) {
354     String JavaDoc name = (String JavaDoc)labelNames.get(l);
355     if (name == null) {
356       name = "L" + labelNames.size();
357       labelNames.put(l, name);
358     }
359     buf.append(name);
360   }
361 }
362
Popular Tags