KickJava   Java API By Example, From Geeks To Geeks.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

689
690   public abstract void printAttribute (final Attribute attr);
691 }
Popular Tags