KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > libraries > asm > util > ASMifierCodeVisitor


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.Label;
34 import oracle.toplink.libraries.asm.Attribute;
35 import oracle.toplink.libraries.asm.util.attrs.ASMifiable;
36
37 import java.util.HashMap JavaDoc;
38
39 /**
40  * A {@link PrintCodeVisitor} that prints the ASM code that
41  * generates the code it visits.
42  *
43  * @author Eric Bruneton, Eugene Kuleshov
44  */

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

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

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

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

300
301   private void appendLabel (final Label l) {
302     buf.append((String JavaDoc)labelNames.get(l));
303   }
304 }
305
Popular Tags