KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
42 import java.util.List JavaDoc;
43
44 /**
45  * An abstract code visitor that prints the code it visits. Each
46  * <tt>visit</tt><i>XXX</i> method clears the {@link #buf buf} buffer, calls the
47  * corresponding <tt>print</tt><i>XXX</i> method, and then adds the buffer's
48  * content to the {@link #text text} list. In order to provide a concrete
49  * print code visitor, one must implement the <tt>print</tt><i>XXX</i> methods
50  * in a sub class of this class. Each method should print the instructions it
51  * visits in {@link #buf buf}.
52  */

53
54 public abstract class PrintCodeVisitor implements CodeVisitor {
55
56   /**
57    * The text to be printed. See {@link PrintClassVisitor#text text}.
58    */

59
60   protected final List JavaDoc text;
61
62   /**
63    * A buffer used to convert instructions to strings.
64    */

65
66   protected final StringBuffer JavaDoc buf;
67
68   /**
69    * The names of the Java Virtual Machine opcodes.
70    */

71
72   public final static String JavaDoc[] OPCODES = {
73     "NOP",
74     "ACONST_NULL",
75     "ICONST_M1",
76     "ICONST_0",
77     "ICONST_1",
78     "ICONST_2",
79     "ICONST_3",
80     "ICONST_4",
81     "ICONST_5",
82     "LCONST_0",
83     "LCONST_1",
84     "FCONST_0",
85     "FCONST_1",
86     "FCONST_2",
87     "DCONST_0",
88     "DCONST_1",
89     "BIPUSH",
90     "SIPUSH",
91     "LDC",
92     null,
93     null,
94     "ILOAD",
95     "LLOAD",
96     "FLOAD",
97     "DLOAD",
98     "ALOAD",
99     null,
100     null,
101     null,
102     null,
103     null,
104     null,
105     null,
106     null,
107     null,
108     null,
109     null,
110     null,
111     null,
112     null,
113     null,
114     null,
115     null,
116     null,
117     null,
118     null,
119     "IALOAD",
120     "LALOAD",
121     "FALOAD",
122     "DALOAD",
123     "AALOAD",
124     "BALOAD",
125     "CALOAD",
126     "SALOAD",
127     "ISTORE",
128     "LSTORE",
129     "FSTORE",
130     "DSTORE",
131     "ASTORE",
132     null,
133     null,
134     null,
135     null,
136     null,
137     null,
138     null,
139     null,
140     null,
141     null,
142     null,
143     null,
144     null,
145     null,
146     null,
147     null,
148     null,
149     null,
150     null,
151     null,
152     "IASTORE",
153     "LASTORE",
154     "FASTORE",
155     "DASTORE",
156     "AASTORE",
157     "BASTORE",
158     "CASTORE",
159     "SASTORE",
160     "POP",
161     "POP2",
162     "DUP",
163     "DUP_X1",
164     "DUP_X2",
165     "DUP2",
166     "DUP2_X1",
167     "DUP2_X2",
168     "SWAP",
169     "IADD",
170     "LADD",
171     "FADD",
172     "DADD",
173     "ISUB",
174     "LSUB",
175     "FSUB",
176     "DSUB",
177     "IMUL",
178     "LMUL",
179     "FMUL",
180     "DMUL",
181     "IDIV",
182     "LDIV",
183     "FDIV",
184     "DDIV",
185     "IREM",
186     "LREM",
187     "FREM",
188     "DREM",
189     "INEG",
190     "LNEG",
191     "FNEG",
192     "DNEG",
193     "ISHL",
194     "LSHL",
195     "ISHR",
196     "LSHR",
197     "IUSHR",
198     "LUSHR",
199     "IAND",
200     "LAND",
201     "IOR",
202     "LOR",
203     "IXOR",
204     "LXOR",
205     "IINC",
206     "I2L",
207     "I2F",
208     "I2D",
209     "L2I",
210     "L2F",
211     "L2D",
212     "F2I",
213     "F2L",
214     "F2D",
215     "D2I",
216     "D2L",
217     "D2F",
218     "I2B",
219     "I2C",
220     "I2S",
221     "LCMP",
222     "FCMPL",
223     "FCMPG",
224     "DCMPL",
225     "DCMPG",
226     "IFEQ",
227     "IFNE",
228     "IFLT",
229     "IFGE",
230     "IFGT",
231     "IFLE",
232     "IF_ICMPEQ",
233     "IF_ICMPNE",
234     "IF_ICMPLT",
235     "IF_ICMPGE",
236     "IF_ICMPGT",
237     "IF_ICMPLE",
238     "IF_ACMPEQ",
239     "IF_ACMPNE",
240     "GOTO",
241     "JSR",
242     "RET",
243     "TABLESWITCH",
244     "LOOKUPSWITCH",
245     "IRETURN",
246     "LRETURN",
247     "FRETURN",
248     "DRETURN",
249     "ARETURN",
250     "RETURN",
251     "GETSTATIC",
252     "PUTSTATIC",
253     "GETFIELD",
254     "PUTFIELD",
255     "INVOKEVIRTUAL",
256     "INVOKESPECIAL",
257     "INVOKESTATIC",
258     "INVOKEINTERFACE",
259     null,
260     "NEW",
261     "NEWARRAY",
262     "ANEWARRAY",
263     "ARRAYLENGTH",
264     "ATHROW",
265     "CHECKCAST",
266     "INSTANCEOF",
267     "MONITORENTER",
268     "MONITOREXIT",
269     null,
270     "MULTIANEWARRAY",
271     "IFNULL",
272     "IFNONNULL",
273     null,
274     null
275   };
276
277   /**
278    * Constructs a new {@link PrintCodeVisitor PrintCodeVisitor} object.
279    */

280
281   public PrintCodeVisitor () {
282     this.buf = new StringBuffer JavaDoc();
283     this.text = new ArrayList JavaDoc();
284   }
285
286   public void visitInsn (final int opcode) {
287     buf.setLength(0);
288     printInsn(opcode);
289     text.add(buf.toString());
290   }
291
292   public void visitIntInsn (final int opcode, final int operand) {
293     buf.setLength(0);
294     printIntInsn(opcode, operand);
295     text.add(buf.toString());
296   }
297
298   public void visitVarInsn (final int opcode, final int var) {
299     buf.setLength(0);
300     printVarInsn(opcode, var);
301     text.add(buf.toString());
302   }
303
304   public void visitTypeInsn (final int opcode, final String JavaDoc desc) {
305     buf.setLength(0);
306     printTypeInsn(opcode, desc);
307     text.add(buf.toString());
308   }
309
310   public void visitFieldInsn (
311     final int opcode,
312     final String JavaDoc owner,
313     final String JavaDoc name,
314     final String JavaDoc desc)
315   {
316     buf.setLength(0);
317     printFieldInsn(opcode, owner, name, desc);
318     text.add(buf.toString());
319   }
320
321   public void visitMethodInsn (
322     final int opcode,
323     final String JavaDoc owner,
324     final String JavaDoc name,
325     final String JavaDoc desc)
326   {
327     buf.setLength(0);
328     printMethodInsn(opcode, owner, name, desc);
329     text.add(buf.toString());
330   }
331
332   public void visitJumpInsn (final int opcode, final Label label) {
333     buf.setLength(0);
334     printJumpInsn(opcode, label);
335     text.add(buf.toString());
336   }
337
338   public void visitLabel (final Label label) {
339     buf.setLength(0);
340     printLabel(label);
341     text.add(buf.toString());
342   }
343
344   public void visitLdcInsn (final Object JavaDoc cst) {
345     buf.setLength(0);
346     printLdcInsn(cst);
347     text.add(buf.toString());
348   }
349
350   public void visitIincInsn (final int var, final int increment) {
351     buf.setLength(0);
352     printIincInsn(var, increment);
353     text.add(buf.toString());
354   }
355
356   public void visitTableSwitchInsn (
357     final int min,
358     final int max,
359     final Label dflt,
360     final Label labels[])
361   {
362     buf.setLength(0);
363     printTableSwitchInsn(min, max, dflt, labels);
364     text.add(buf.toString());
365   }
366
367   public void visitLookupSwitchInsn (
368     final Label dflt,
369     final int keys[],
370     final Label labels[])
371   {
372     buf.setLength(0);
373     printLookupSwitchInsn(dflt, keys, labels);
374     text.add(buf.toString());
375   }
376
377   public void visitMultiANewArrayInsn (final String JavaDoc desc, final int dims) {
378     buf.setLength(0);
379     printMultiANewArrayInsn(desc, dims);
380     text.add(buf.toString());
381   }
382
383   public void visitTryCatchBlock (
384     final Label start,
385     final Label end,
386     final Label handler,
387     final String JavaDoc type)
388   {
389     buf.setLength(0);
390     printTryCatchBlock(start, end, handler, type);
391     text.add(buf.toString());
392   }
393
394   public void visitMaxs (final int maxStack, final int maxLocals) {
395     buf.setLength(0);
396     printMaxs(maxStack, maxLocals);
397     text.add(buf.toString());
398   }
399
400   public void visitLocalVariable (
401     final String JavaDoc name,
402     final String JavaDoc desc,
403     final Label start,
404     final Label end,
405     final int index)
406   {
407     buf.setLength(0);
408     printLocalVariable(name, desc, start, end, index);
409     text.add(buf.toString());
410   }
411
412   public void visitLineNumber (final int line, final Label start) {
413     buf.setLength(0);
414     printLineNumber(line, start);
415     text.add(buf.toString());
416   }
417
418   public void visitAttribute (final Attribute attr) {
419     buf.setLength(0);
420     printAttribute(attr);
421     text.add(buf.toString());
422   }
423
424   /**
425    * Returns the code printed by this code visitor.
426    *
427    * @return the code printed by this code visitor. See {@link
428    * PrintClassVisitor#text text}.
429    */

430
431   public List JavaDoc getText () {
432     return text;
433   }
434
435   /**
436    * Prints a zero operand instruction.
437    *
438    * @param opcode the opcode of the instruction to be printed. This opcode is
439    * either NOP, ACONST_NULL, ICONST_M1, ICONST_0, ICONST_1, ICONST_2,
440    * ICONST_3, ICONST_4, ICONST_5, LCONST_0, LCONST_1, FCONST_0, FCONST_1,
441    * FCONST_2, DCONST_0, DCONST_1,
442    *
443    * IALOAD, LALOAD, FALOAD, DALOAD, AALOAD, BALOAD, CALOAD, SALOAD,
444    * IASTORE, LASTORE, FASTORE, DASTORE, AASTORE, BASTORE, CASTORE,
445    * SASTORE,
446    *
447    * POP, POP2, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, SWAP,
448    *
449    * IADD, LADD, FADD, DADD, ISUB, LSUB, FSUB, DSUB, IMUL, LMUL, FMUL,
450    * DMUL, IDIV, LDIV, FDIV, DDIV, IREM, LREM, FREM, DREM, INEG, LNEG,
451    * FNEG, DNEG, ISHL, LSHL, ISHR, LSHR, IUSHR, LUSHR, IAND, LAND, IOR,
452    * LOR, IXOR, LXOR,
453    *
454    * I2L, I2F, I2D, L2I, L2F, L2D, F2I, F2L, F2D, D2I, D2L, D2F, I2B, I2C,
455    * I2S,
456    *
457    * LCMP, FCMPL, FCMPG, DCMPL, DCMPG,
458    *
459    * IRETURN, LRETURN, FRETURN, DRETURN, ARETURN, RETURN,
460    *
461    * ARRAYLENGTH,
462    *
463    * ATHROW,
464    *
465    * MONITORENTER, or MONITOREXIT.
466    */

467
468   public abstract void printInsn (final int opcode);
469
470   /**
471    * Prints an instruction with a single int operand.
472    *
473    * @param opcode the opcode of the instruction to be printed. This opcode is
474    * either BIPUSH, SIPUSH or NEWARRAY.
475    * @param operand the operand of the instruction to be printed.
476    */

477
478   public abstract void printIntInsn (final int opcode, final int operand);
479
480   /**
481    * Prints a local variable instruction. A local variable instruction is an
482    * instruction that loads or stores the value of a local variable.
483    *
484    * @param opcode the opcode of the local variable instruction to be printed.
485    * This opcode is either ILOAD, LLOAD, FLOAD, DLOAD, ALOAD, ISTORE,
486    * LSTORE, FSTORE, DSTORE, ASTORE or RET.
487    * @param var the operand of the instruction to be printed. This operand is
488    * the index of a local variable.
489    */

490
491   public abstract void printVarInsn (final int opcode, final int var);
492
493   /**
494    * Prints a type instruction. A type instruction is an instruction that
495    * takes a type descriptor as parameter.
496    *
497    * @param opcode the opcode of the type instruction to be printed. This opcode
498    * is either NEW, ANEWARRAY, CHECKCAST or INSTANCEOF.
499    * @param desc the operand of the instruction to be printed. This operand is
500    * must be a fully qualified class name in internal form, or a the type
501    * descriptor of an array type (see {@link org.logicalcobwebs.asm.Type Type}).
502    */

503
504   public abstract void printTypeInsn (final int opcode, final String JavaDoc desc);
505
506   /**
507    * Prints a field instruction. A field instruction is an instruction that
508    * loads or stores the value of a field of an object.
509    *
510    * @param opcode the opcode of the type instruction to be printed. This opcode
511    * is either GETSTATIC, PUTSTATIC, GETFIELD or PUTFIELD.
512    * @param owner the internal name of the field's owner class (see {@link
513    * org.logicalcobwebs.asm.Type#getInternalName getInternalName}).
514    * @param name the field's name.
515    * @param desc the field's descriptor (see {@link org.logicalcobwebs.asm.Type
516    * Type}).
517    */

518
519   public abstract void printFieldInsn (
520     final int opcode,
521     final String JavaDoc owner,
522     final String JavaDoc name,
523     final String JavaDoc desc);
524
525   /**
526    * Prints a method instruction. A method instruction is an instruction that
527    * invokes a method.
528    *
529    * @param opcode the opcode of the type instruction to be printed. This opcode
530    * is either INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC or
531    * INVOKEINTERFACE.
532    * @param owner the internal name of the method's owner class (see {@link
533    * org.logicalcobwebs.asm.Type#getInternalName getInternalName}).
534    * @param name the method's name.
535    * @param desc the method's descriptor (see {@link org.logicalcobwebs.asm.Type
536    * Type}).
537    */

538
539   public abstract void printMethodInsn (
540     final int opcode,
541     final String JavaDoc owner,
542     final String JavaDoc name,
543     final String JavaDoc desc);
544
545   /**
546    * Prints a jump instruction. A jump instruction is an instruction that may
547    * jump to another instruction.
548    *
549    * @param opcode the opcode of the type instruction to be printed. This opcode
550    * is either IFEQ, IFNE, IFLT, IFGE, IFGT, IFLE, IF_ICMPEQ, IF_ICMPNE,
551    * IF_ICMPLT, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ACMPEQ, IF_ACMPNE,
552    * GOTO, JSR, IFNULL or IFNONNULL.
553    * @param label the operand of the instruction to be printed. This operand is
554    * a label that designates the instruction to which the jump instruction
555    * may jump.
556    */

557
558   public abstract void printJumpInsn (final int opcode, final Label label);
559
560   /**
561    * Prints a label. A label designates the instruction that will be visited
562    * just after it.
563    *
564    * @param label a {@link Label Label} object.
565    */

566
567   public abstract void printLabel (final Label label);
568
569   /**
570    * Prints a LDC instruction.
571    *
572    * @param cst the constant to be loaded on the stack. This parameter must be
573    * a non null {@link java.lang.Integer Integer}, a {@link java.lang.Float
574    * Float}, a {@link java.lang.Long Long}, a {@link java.lang.Double
575    * Double} or a {@link String String}.
576    */

577
578   public abstract void printLdcInsn (final Object JavaDoc cst);
579
580   /**
581    * Prints an IINC instruction.
582    *
583    * @param var index of the local variable to be incremented.
584    * @param increment amount to increment the local variable by.
585    */

586
587   public abstract void printIincInsn (final int var, final int increment);
588
589   /**
590    * Prints a TABLESWITCH instruction.
591    *
592    * @param min the minimum key value.
593    * @param max the maximum key value.
594    * @param dflt beginning of the default handler block.
595    * @param labels beginnings of the handler blocks. <tt>labels[i]</tt> is the
596    * beginning of the handler block for the <tt>min + i</tt> key.
597    */

598
599   public abstract void printTableSwitchInsn (
600     final int min,
601     final int max,
602     final Label dflt,
603     final Label labels[]);
604
605   /**
606    * Prints a LOOKUPSWITCH instruction.
607    *
608    * @param dflt beginning of the default handler block.
609    * @param keys the values of the keys.
610    * @param labels beginnings of the handler blocks. <tt>labels[i]</tt> is the
611    * beginning of the handler block for the <tt>keys[i]</tt> key.
612    */

613
614   public abstract void printLookupSwitchInsn (
615     final Label dflt,
616     final int keys[],
617     final Label labels[]);
618
619   /**
620    * Prints a MULTIANEWARRAY instruction.
621    *
622    * @param desc an array type descriptor (see {@link org.logicalcobwebs.asm.Type
623    * Type}).
624    * @param dims number of dimensions of the array to allocate.
625    */

626
627   public abstract void printMultiANewArrayInsn (
628     final String JavaDoc desc,
629     final int dims);
630
631   /**
632    * Prints a try catch block.
633    *
634    * @param start beginning of the exception handler's scope (inclusive).
635    * @param end end of the exception handler's scope (exclusive).
636    * @param handler beginning of the exception handler's code.
637    * @param type internal name of the type of exceptions handled by the handler,
638    * or <tt>null</tt> to catch any exceptions (for "finally" blocks).
639    */

640
641   public abstract void printTryCatchBlock (
642     final Label start,
643     final Label end,
644     final Label handler,
645     final String JavaDoc type);
646
647   /**
648    * Prints the maximum stack size and the maximum number of local variables of
649    * the method.
650    *
651    * @param maxStack maximum stack size of the method.
652    * @param maxLocals maximum number of local variables for the method.
653    */

654
655   public abstract void printMaxs (final int maxStack, final int maxLocals);
656
657   /**
658    * Prints a local variable declaration.
659    *
660    * @param name the name of a local variable.
661    * @param desc the type descriptor of this local variable.
662    * @param start the first instruction corresponding to the scope of this
663    * local variable (inclusive).
664    * @param end the last instruction corresponding to the scope of this
665    * local variable (exclusive).
666    * @param index the local variable's index.
667    */

668
669   public abstract void printLocalVariable (
670     final String JavaDoc name,
671     final String JavaDoc desc,
672     final Label start,
673     final Label end,
674     final int index);
675
676   /**
677    * Prints a line number declaration.
678    *
679    * @param line a line number. This number refers to the source file
680    * from which the class was compiled.
681    * @param start the first instruction corresponding to this line number.
682    */

683
684   public abstract void printLineNumber (final int line, final Label start);
685
686   /**
687    * Prints a non standard code attribute.
688    *
689    * @param attr a non standard code attribute.
690    */

691
692   public abstract void printAttribute (final Attribute attr);
693 }
Popular Tags