KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > libraries > 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
31 package oracle.toplink.libraries.asm.util;
32
33 import oracle.toplink.libraries.asm.CodeVisitor;
34 import oracle.toplink.libraries.asm.Label;
35 import oracle.toplink.libraries.asm.Attribute;
36 import oracle.toplink.libraries.asm.Type;
37
38 import java.util.HashMap JavaDoc;
39
40 /**
41  * A {@link PrintCodeVisitor PrintCodeVisitor} that prints a disassembled view
42  * of the code it visits.
43  *
44  * @author Eric Bruneton
45  */

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

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

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

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

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