KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > util > ASMifierMethodVisitor


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 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 package org.objectweb.asm.util;
31
32 import org.objectweb.asm.AnnotationVisitor;
33 import org.objectweb.asm.MethodVisitor;
34 import org.objectweb.asm.Label;
35
36 import java.util.HashMap JavaDoc;
37
38 /**
39  * A {@link MethodVisitor} that prints the ASM code that generates the methods
40  * it visits.
41  *
42  * @author Eric Bruneton
43  * @author Eugene Kuleshov
44  */

45 public class ASMifierMethodVisitor extends ASMifierAbstractVisitor implements
46         MethodVisitor
47 {
48
49     /**
50      * Constructs a new {@link ASMifierMethodVisitor} object.
51      */

52     public ASMifierMethodVisitor() {
53         super("mv");
54         this.labelNames = new HashMap JavaDoc();
55     }
56
57     public AnnotationVisitor visitAnnotationDefault() {
58         buf.setLength(0);
59         buf.append("{\n").append("av0 = mv.visitAnnotationDefault();\n");
60         text.add(buf.toString());
61         ASMifierAnnotationVisitor av = new ASMifierAnnotationVisitor(0);
62         text.add(av.getText());
63         text.add("}\n");
64         return av;
65     }
66
67     public AnnotationVisitor visitParameterAnnotation(
68         final int parameter,
69         final String JavaDoc desc,
70         final boolean visible)
71     {
72         buf.setLength(0);
73         buf.append("{\n")
74                 .append("av0 = mv.visitParameterAnnotation(")
75                 .append(parameter)
76                 .append(", ");
77         appendConstant(desc);
78         buf.append(", ").append(visible).append(");\n");
79         text.add(buf.toString());
80         ASMifierAnnotationVisitor av = new ASMifierAnnotationVisitor(0);
81         text.add(av.getText());
82         text.add("}\n");
83         return av;
84     }
85
86     public void visitCode() {
87         text.add("mv.visitCode();\n");
88     }
89
90     public void visitInsn(final int opcode) {
91         buf.setLength(0);
92         buf.append("mv.visitInsn(").append(OPCODES[opcode]).append(");\n");
93         text.add(buf.toString());
94     }
95
96     public void visitIntInsn(final int opcode, final int operand) {
97         buf.setLength(0);
98         buf.append("mv.visitIntInsn(")
99                 .append(OPCODES[opcode])
100                 .append(", ")
101                 .append(operand)
102                 .append(");\n");
103         text.add(buf.toString());
104     }
105
106     public void visitVarInsn(final int opcode, final int var) {
107         buf.setLength(0);
108         buf.append("mv.visitVarInsn(")
109                 .append(OPCODES[opcode])
110                 .append(", ")
111                 .append(var)
112                 .append(");\n");
113         text.add(buf.toString());
114     }
115
116     public void visitTypeInsn(final int opcode, final String JavaDoc desc) {
117         buf.setLength(0);
118         buf.append("mv.visitTypeInsn(").append(OPCODES[opcode]).append(", ");
119         appendConstant(desc);
120         buf.append(");\n");
121         text.add(buf.toString());
122     }
123
124     public void visitFieldInsn(
125         final int opcode,
126         final String JavaDoc owner,
127         final String JavaDoc name,
128         final String JavaDoc desc)
129     {
130         buf.setLength(0);
131         buf.append("mv.visitFieldInsn(").append(OPCODES[opcode]).append(", ");
132         appendConstant(owner);
133         buf.append(", ");
134         appendConstant(name);
135         buf.append(", ");
136         appendConstant(desc);
137         buf.append(");\n");
138         text.add(buf.toString());
139     }
140
141     public void visitMethodInsn(
142         final int opcode,
143         final String JavaDoc owner,
144         final String JavaDoc name,
145         final String JavaDoc desc)
146     {
147         buf.setLength(0);
148         buf.append("mv.visitMethodInsn(").append(OPCODES[opcode]).append(", ");
149         appendConstant(owner);
150         buf.append(", ");
151         appendConstant(name);
152         buf.append(", ");
153         appendConstant(desc);
154         buf.append(");\n");
155         text.add(buf.toString());
156     }
157
158     public void visitJumpInsn(final int opcode, final Label label) {
159         buf.setLength(0);
160         declareLabel(label);
161         buf.append("mv.visitJumpInsn(").append(OPCODES[opcode]).append(", ");
162         appendLabel(label);
163         buf.append(");\n");
164         text.add(buf.toString());
165     }
166
167     public void visitLabel(final Label label) {
168         buf.setLength(0);
169         declareLabel(label);
170         buf.append("mv.visitLabel(");
171         appendLabel(label);
172         buf.append(");\n");
173         text.add(buf.toString());
174     }
175
176     public void visitLdcInsn(final Object JavaDoc cst) {
177         buf.setLength(0);
178         buf.append("mv.visitLdcInsn(");
179         appendConstant(cst);
180         buf.append(");\n");
181         text.add(buf.toString());
182     }
183
184     public void visitIincInsn(final int var, final int increment) {
185         buf.setLength(0);
186         buf.append("mv.visitIincInsn(")
187                 .append(var)
188                 .append(", ")
189                 .append(increment)
190                 .append(");\n");
191         text.add(buf.toString());
192     }
193
194     public void visitTableSwitchInsn(
195         final int min,
196         final int max,
197         final Label dflt,
198         final Label labels[])
199     {
200         buf.setLength(0);
201         for (int i = 0; i < labels.length; ++i) {
202             declareLabel(labels[i]);
203         }
204         declareLabel(dflt);
205
206         buf.append("mv.visitTableSwitchInsn(")
207                 .append(min)
208                 .append(", ")
209                 .append(max)
210                 .append(", ");
211         appendLabel(dflt);
212         buf.append(", new Label[] {");
213         for (int i = 0; i < labels.length; ++i) {
214             buf.append(i == 0 ? " " : ", ");
215             appendLabel(labels[i]);
216         }
217         buf.append(" });\n");
218         text.add(buf.toString());
219     }
220
221     public void visitLookupSwitchInsn(
222         final Label dflt,
223         final int keys[],
224         final Label labels[])
225     {
226         buf.setLength(0);
227         for (int i = 0; i < labels.length; ++i) {
228             declareLabel(labels[i]);
229         }
230         declareLabel(dflt);
231
232         buf.append("mv.visitLookupSwitchInsn(");
233         appendLabel(dflt);
234         buf.append(", new int[] {");
235         for (int i = 0; i < keys.length; ++i) {
236             buf.append(i == 0 ? " " : ", ").append(keys[i]);
237         }
238         buf.append(" }, new Label[] {");
239         for (int i = 0; i < labels.length; ++i) {
240             buf.append(i == 0 ? " " : ", ");
241             appendLabel(labels[i]);
242         }
243         buf.append(" });\n");
244         text.add(buf.toString());
245     }
246
247     public void visitMultiANewArrayInsn(final String JavaDoc desc, final int dims) {
248         buf.setLength(0);
249         buf.append("mv.visitMultiANewArrayInsn(");
250         appendConstant(desc);
251         buf.append(", ").append(dims).append(");\n");
252         text.add(buf.toString());
253     }
254
255     public void visitTryCatchBlock(
256         final Label start,
257         final Label end,
258         final Label handler,
259         final String JavaDoc type)
260     {
261         buf.setLength(0);
262         buf.append("mv.visitTryCatchBlock(");
263         appendLabel(start);
264         buf.append(", ");
265         appendLabel(end);
266         buf.append(", ");
267         appendLabel(handler);
268         buf.append(", ");
269         appendConstant(type);
270         buf.append(");\n");
271         text.add(buf.toString());
272     }
273
274     public void visitLocalVariable(
275         final String JavaDoc name,
276         final String JavaDoc desc,
277         final String JavaDoc signature,
278         final Label start,
279         final Label end,
280         final int index)
281     {
282         buf.setLength(0);
283         buf.append("mv.visitLocalVariable(");
284         appendConstant(name);
285         buf.append(", ");
286         appendConstant(desc);
287         buf.append(", ");
288         appendConstant(signature);
289         buf.append(", ");
290         appendLabel(start);
291         buf.append(", ");
292         appendLabel(end);
293         buf.append(", ").append(index).append(");\n");
294         text.add(buf.toString());
295     }
296
297     public void visitLineNumber(final int line, final Label start) {
298         buf.setLength(0);
299         buf.append("mv.visitLineNumber(").append(line).append(", ");
300         appendLabel(start);
301         buf.append(");\n");
302         text.add(buf.toString());
303     }
304
305     public void visitMaxs(final int maxStack, final int maxLocals) {
306         buf.setLength(0);
307         buf.append("mv.visitMaxs(")
308                 .append(maxStack)
309                 .append(", ")
310                 .append(maxLocals)
311                 .append(");\n");
312         text.add(buf.toString());
313     }
314
315     /**
316      * Appends a declaration of the given label to {@link #buf buf}. This
317      * declaration is of the form "Label lXXX = new Label();". Does nothing if
318      * the given label has already been declared.
319      *
320      * @param l a label.
321      */

322     private void declareLabel(final Label l) {
323         String JavaDoc name = (String JavaDoc) labelNames.get(l);
324         if (name == null) {
325             name = "l" + labelNames.size();
326             labelNames.put(l, name);
327             buf.append("Label ").append(name).append(" = new Label();\n");
328         }
329     }
330
331     /**
332      * Appends the name of the given label to {@link #buf buf}. The given label
333      * <i>must</i> already have a name. One way to ensure this is to always
334      * call {@link #declareLabel declared} before calling this method.
335      *
336      * @param l a label.
337      */

338     private void appendLabel(final Label l) {
339         buf.append((String JavaDoc) labelNames.get(l));
340     }
341 }
342
Popular Tags