KickJava   Java API By Example, From Geeks To Geeks.

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


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.Label;
38 import org.logicalcobwebs.asm.Attribute;
39
40 import java.util.HashMap JavaDoc;
41
42 /**
43  * A {@link PrintCodeVisitor PrintCodeVisitor} that prints the ASM code that
44  * generates the code it visits.
45  */

46
47 public class DumpCodeVisitor extends PrintCodeVisitor {
48
49   /**
50    * The label names. This map associate String values to Label keys.
51    */

52
53   private final HashMap JavaDoc labelNames;
54
55   /**
56    * Constructs a new {@link DumpCodeVisitor DumpCodeVisitor} object.
57    */

58
59   public DumpCodeVisitor () {
60     this.labelNames = new HashMap JavaDoc();
61   }
62
63   public void printInsn (final int opcode) {
64     buf.append("cv.visitInsn(").
65       append(OPCODES[opcode]).
66       append(");\n");
67   }
68
69   public void printIntInsn (final int opcode, final int operand) {
70     buf.append("cv.visitIntInsn(").
71       append(OPCODES[opcode]).
72       append(", ").
73       append(operand).
74       append(");\n");
75   }
76
77   public void printVarInsn (final int opcode, final int var) {
78     buf.append("cv.visitVarInsn(").
79       append(OPCODES[opcode]).
80       append(", ").
81       append(var).
82       append(");\n");
83   }
84
85   public void printTypeInsn (final int opcode, final String JavaDoc desc) {
86     buf.append("cv.visitTypeInsn(").
87       append(OPCODES[opcode]).
88       append(", ");
89     DumpClassVisitor.appendConstant(buf, desc);
90     buf.append(");\n");
91   }
92
93   public void printFieldInsn (
94     final int opcode,
95     final String JavaDoc owner,
96     final String JavaDoc name,
97     final String JavaDoc desc)
98   {
99     buf.append("cv.visitFieldInsn(")
100       .append(OPCODES[opcode])
101       .append(", ");
102     DumpClassVisitor.appendConstant(buf, owner);
103     buf.append(", ");
104     DumpClassVisitor.appendConstant(buf, name);
105     buf.append(", ");
106     DumpClassVisitor.appendConstant(buf, desc);
107     buf.append(");\n");
108   }
109
110   public void printMethodInsn (
111     final int opcode,
112     final String JavaDoc owner,
113     final String JavaDoc name,
114     final String JavaDoc desc)
115   {
116     buf.append("cv.visitMethodInsn(")
117       .append(OPCODES[opcode])
118       .append(", ");
119     DumpClassVisitor.appendConstant(buf, owner);
120     buf.append(", ");
121     DumpClassVisitor.appendConstant(buf, name);
122     buf.append(", ");
123     DumpClassVisitor.appendConstant(buf, desc);
124     buf.append(");\n");
125   }
126
127   public void printJumpInsn (final int opcode, final Label label) {
128     declareLabel(label);
129     buf.append("cv.visitJumpInsn(")
130       .append(OPCODES[opcode])
131       .append(", ");
132     appendLabel(label);
133     buf.append(");\n");
134   }
135
136   public void printLabel (final Label label) {
137     declareLabel(label);
138     buf.append("cv.visitLabel(");
139     appendLabel(label);
140     buf.append(");\n");
141   }
142
143   public void printLdcInsn (final Object JavaDoc cst) {
144     buf.append("cv.visitLdcInsn(");
145     DumpClassVisitor.appendConstant(buf, cst);
146     buf.append(");\n");
147   }
148
149   public void printIincInsn (final int var, final int increment) {
150     buf.append("cv.visitIincInsn(")
151       .append(var)
152       .append(", ")
153       .append(increment)
154       .append(");\n");
155   }
156
157   public void printTableSwitchInsn (
158     final int min,
159     final int max,
160     final Label dflt,
161     final Label labels[])
162   {
163     for (int i = 0; i < labels.length; ++i) {
164       declareLabel(labels[i]);
165     }
166     declareLabel(dflt);
167
168     buf.append("cv.visitTableSwitchInsn(")
169       .append(min)
170       .append(", ")
171       .append(max)
172       .append(", ");
173     appendLabel(dflt);
174     buf.append(", new Label[] {");
175     for (int i = 0; i < labels.length; ++i) {
176       buf.append(i == 0 ? " " : ", ");
177       appendLabel(labels[i]);
178     }
179     buf.append(" });\n");
180   }
181
182   public void printLookupSwitchInsn (
183     final Label dflt,
184     final int keys[],
185     final Label labels[])
186   {
187     for (int i = 0; i < labels.length; ++i) {
188       declareLabel(labels[i]);
189     }
190     declareLabel(dflt);
191
192     buf.append("cv.visitLookupSwitchInsn(");
193     appendLabel(dflt);
194     buf.append(", new int[] {");
195     for (int i = 0; i < keys.length; ++i) {
196       buf.append(i == 0 ? " " : ", ").append(keys[i]);
197     }
198     buf.append(" }, new Label[] {");
199     for (int i = 0; i < labels.length; ++i) {
200       buf.append(i == 0 ? " " : ", ");
201       appendLabel(labels[i]);
202     }
203     buf.append(" });\n");
204   }
205
206   public void printMultiANewArrayInsn (final String JavaDoc desc, final int dims) {
207     buf.append("cv.visitMultiANewArrayInsn(");
208     DumpClassVisitor.appendConstant(buf, desc);
209     buf.append(", ")
210       .append(dims)
211       .append(");\n");
212   }
213
214   public void printTryCatchBlock (
215     final Label start,
216     final Label end,
217     final Label handler,
218     final String JavaDoc type)
219   {
220     buf.append("cv.visitTryCatchBlock(");
221     appendLabel(start);
222     buf.append(", ");
223     appendLabel(end);
224     buf.append(", ");
225     appendLabel(handler);
226     buf.append(", ");
227     DumpClassVisitor.appendConstant(buf, type);
228     buf.append(");\n");
229   }
230
231   public void printMaxs (final int maxStack, final int maxLocals) {
232     buf.append("cv.visitMaxs(")
233       .append(maxStack)
234       .append(", ")
235       .append(maxLocals)
236       .append(");\n");
237   }
238
239   public void printLocalVariable (
240     final String JavaDoc name,
241     final String JavaDoc desc,
242     final Label start,
243     final Label end,
244     final int index)
245   {
246     buf.append("cv.visitLocalVariable(");
247     DumpClassVisitor.appendConstant(buf, name);
248     buf.append(", ");
249     DumpClassVisitor.appendConstant(buf, desc);
250     buf.append(", ");
251     appendLabel(start);
252     buf.append(", ");
253     appendLabel(end);
254     buf.append(", ").append(index).append(");\n");
255   }
256
257   public void printLineNumber (final int line, final Label start) {
258     buf.append("cv.visitLineNumber(")
259       .append(line)
260       .append(", ");
261     appendLabel(start);
262     buf.append(");\n");
263   }
264
265   public void printAttribute (final Attribute attr) {
266     buf.append("// WARNING! skipped a non standard code attribute of type \"");
267     buf.append(attr.type);
268     buf.append("\"\n");
269   }
270
271   /**
272    * Appends a declaration of the given label to {@link #buf buf}. This
273    * declaration is of the form "Label lXXX = new Label();". Does nothing
274    * if the given label has already been declared.
275    *
276    * @param l a label.
277    */

278
279   private void declareLabel (final Label l) {
280     String JavaDoc name = (String JavaDoc)labelNames.get(l);
281     if (name == null) {
282       name = "l" + labelNames.size();
283       labelNames.put(l, name);
284       buf.append("Label ")
285         .append(name)
286         .append(" = new Label();\n");
287     }
288   }
289
290   /**
291    * Appends the name of the given label to {@link #buf buf}. The given label
292    * <i>must</i> already have a name. One way to ensure this is to always call
293    * {@link #declareLabel declared} before calling this method.
294    *
295    * @param l a label.
296    */

297
298   private void appendLabel (final Label l) {
299     buf.append((String JavaDoc)labelNames.get(l));
300   }
301 }
302
Popular Tags