• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2007 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.mockito.asm;
31 
32 /**
33  * A visitor to visit a Java method. The methods of this interface must be
34  * called in the following order: [ <tt>visitAnnotationDefault</tt> ] (
35  * <tt>visitAnnotation</tt> | <tt>visitParameterAnnotation</tt> |
36  * <tt>visitAttribute</tt> )* [ <tt>visitCode</tt> ( <tt>visitFrame</tt> |
37  * <tt>visit</tt><i>X</i>Insn</tt> | <tt>visitLabel</tt> | <tt>visitTryCatchBlock</tt> |
38  * <tt>visitLocalVariable</tt> | <tt>visitLineNumber</tt>)* <tt>visitMaxs</tt> ]
39  * <tt>visitEnd</tt>. In addition, the <tt>visit</tt><i>X</i>Insn</tt>
40  * and <tt>visitLabel</tt> methods must be called in the sequential order of
41  * the bytecode instructions of the visited code, <tt>visitTryCatchBlock</tt>
42  * must be called <i>before</i> the labels passed as arguments have been
43  * visited, and the <tt>visitLocalVariable</tt> and <tt>visitLineNumber</tt>
44  * methods must be called <i>after</i> the labels passed as arguments have been
45  * visited.
46  *
47  * @author Eric Bruneton
48  */
49 public interface MethodVisitor {
50 
51     // -------------------------------------------------------------------------
52     // Annotations and non standard attributes
53     // -------------------------------------------------------------------------
54 
55     /**
56      * Visits the default value of this annotation interface method.
57      *
58      * @return a visitor to the visit the actual default value of this
59      *         annotation interface method, or <tt>null</tt> if this visitor
60      *         is not interested in visiting this default value. The 'name'
61      *         parameters passed to the methods of this annotation visitor are
62      *         ignored. Moreover, exacly one visit method must be called on this
63      *         annotation visitor, followed by visitEnd.
64      */
visitAnnotationDefault()65     AnnotationVisitor visitAnnotationDefault();
66 
67     /**
68      * Visits an annotation of this method.
69      *
70      * @param desc the class descriptor of the annotation class.
71      * @param visible <tt>true</tt> if the annotation is visible at runtime.
72      * @return a visitor to visit the annotation values, or <tt>null</tt> if
73      *         this visitor is not interested in visiting this annotation.
74      */
visitAnnotation(String desc, boolean visible)75     AnnotationVisitor visitAnnotation(String desc, boolean visible);
76 
77     /**
78      * Visits an annotation of a parameter this method.
79      *
80      * @param parameter the parameter index.
81      * @param desc the class descriptor of the annotation class.
82      * @param visible <tt>true</tt> if the annotation is visible at runtime.
83      * @return a visitor to visit the annotation values, or <tt>null</tt> if
84      *         this visitor is not interested in visiting this annotation.
85      */
visitParameterAnnotation( int parameter, String desc, boolean visible)86     AnnotationVisitor visitParameterAnnotation(
87         int parameter,
88         String desc,
89         boolean visible);
90 
91     /**
92      * Visits a non standard attribute of this method.
93      *
94      * @param attr an attribute.
95      */
visitAttribute(Attribute attr)96     void visitAttribute(Attribute attr);
97 
98     /**
99      * Starts the visit of the method's code, if any (i.e. non abstract method).
100      */
visitCode()101     void visitCode();
102 
103     /**
104      * Visits the current state of the local variables and operand stack
105      * elements. This method must(*) be called <i>just before</i> any
106      * instruction <b>i</b> that follows an unconditionnal branch instruction
107      * such as GOTO or THROW, that is the target of a jump instruction, or that
108      * starts an exception handler block. The visited types must describe the
109      * values of the local variables and of the operand stack elements <i>just
110      * before</i> <b>i</b> is executed. <br> <br> (*) this is mandatory only
111      * for classes whose version is greater than or equal to
112      * {@link Opcodes#V1_6 V1_6}. <br> <br> Packed frames are basically
113      * "deltas" from the state of the previous frame (very first frame is
114      * implicitly defined by the method's parameters and access flags): <ul>
115      * <li>{@link Opcodes#F_SAME} representing frame with exactly the same
116      * locals as the previous frame and with the empty stack.</li> <li>{@link Opcodes#F_SAME1}
117      * representing frame with exactly the same locals as the previous frame and
118      * with single value on the stack (<code>nStack</code> is 1 and
119      * <code>stack[0]</code> contains value for the type of the stack item).</li>
120      * <li>{@link Opcodes#F_APPEND} representing frame with current locals are
121      * the same as the locals in the previous frame, except that additional
122      * locals are defined (<code>nLocal</code> is 1, 2 or 3 and
123      * <code>local</code> elements contains values representing added types).</li>
124      * <li>{@link Opcodes#F_CHOP} representing frame with current locals are
125      * the same as the locals in the previous frame, except that the last 1-3
126      * locals are absent and with the empty stack (<code>nLocals</code> is 1,
127      * 2 or 3). </li> <li>{@link Opcodes#F_FULL} representing complete frame
128      * data.</li> </li> </ul>
129      *
130      * @param type the type of this stack map frame. Must be
131      *        {@link Opcodes#F_NEW} for expanded frames, or
132      *        {@link Opcodes#F_FULL}, {@link Opcodes#F_APPEND},
133      *        {@link Opcodes#F_CHOP}, {@link Opcodes#F_SAME} or
134      *        {@link Opcodes#F_APPEND}, {@link Opcodes#F_SAME1} for compressed
135      *        frames.
136      * @param nLocal the number of local variables in the visited frame.
137      * @param local the local variable types in this frame. This array must not
138      *        be modified. Primitive types are represented by
139      *        {@link Opcodes#TOP}, {@link Opcodes#INTEGER},
140      *        {@link Opcodes#FLOAT}, {@link Opcodes#LONG},
141      *        {@link Opcodes#DOUBLE},{@link Opcodes#NULL} or
142      *        {@link Opcodes#UNINITIALIZED_THIS} (long and double are
143      *        represented by a single element). Reference types are represented
144      *        by String objects (representing internal names), and uninitialized
145      *        types by Label objects (this label designates the NEW instruction
146      *        that created this uninitialized value).
147      * @param nStack the number of operand stack elements in the visited frame.
148      * @param stack the operand stack types in this frame. This array must not
149      *        be modified. Its content has the same format as the "local" array.
150      */
visitFrame( int type, int nLocal, Object[] local, int nStack, Object[] stack)151     void visitFrame(
152         int type,
153         int nLocal,
154         Object[] local,
155         int nStack,
156         Object[] stack);
157 
158     // -------------------------------------------------------------------------
159     // Normal instructions
160     // -------------------------------------------------------------------------
161 
162     /**
163      * Visits a zero operand instruction.
164      *
165      * @param opcode the opcode of the instruction to be visited. This opcode is
166      *        either NOP, ACONST_NULL, ICONST_M1, ICONST_0, ICONST_1, ICONST_2,
167      *        ICONST_3, ICONST_4, ICONST_5, LCONST_0, LCONST_1, FCONST_0,
168      *        FCONST_1, FCONST_2, DCONST_0, DCONST_1, IALOAD, LALOAD, FALOAD,
169      *        DALOAD, AALOAD, BALOAD, CALOAD, SALOAD, IASTORE, LASTORE, FASTORE,
170      *        DASTORE, AASTORE, BASTORE, CASTORE, SASTORE, POP, POP2, DUP,
171      *        DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, SWAP, IADD, LADD, FADD,
172      *        DADD, ISUB, LSUB, FSUB, DSUB, IMUL, LMUL, FMUL, DMUL, IDIV, LDIV,
173      *        FDIV, DDIV, IREM, LREM, FREM, DREM, INEG, LNEG, FNEG, DNEG, ISHL,
174      *        LSHL, ISHR, LSHR, IUSHR, LUSHR, IAND, LAND, IOR, LOR, IXOR, LXOR,
175      *        I2L, I2F, I2D, L2I, L2F, L2D, F2I, F2L, F2D, D2I, D2L, D2F, I2B,
176      *        I2C, I2S, LCMP, FCMPL, FCMPG, DCMPL, DCMPG, IRETURN, LRETURN,
177      *        FRETURN, DRETURN, ARETURN, RETURN, ARRAYLENGTH, ATHROW,
178      *        MONITORENTER, or MONITOREXIT.
179      */
visitInsn(int opcode)180     void visitInsn(int opcode);
181 
182     /**
183      * Visits an instruction with a single int operand.
184      *
185      * @param opcode the opcode of the instruction to be visited. This opcode is
186      *        either BIPUSH, SIPUSH or NEWARRAY.
187      * @param operand the operand of the instruction to be visited.<br> When
188      *        opcode is BIPUSH, operand value should be between Byte.MIN_VALUE
189      *        and Byte.MAX_VALUE.<br> When opcode is SIPUSH, operand value
190      *        should be between Short.MIN_VALUE and Short.MAX_VALUE.<br> When
191      *        opcode is NEWARRAY, operand value should be one of
192      *        {@link Opcodes#T_BOOLEAN}, {@link Opcodes#T_CHAR},
193      *        {@link Opcodes#T_FLOAT}, {@link Opcodes#T_DOUBLE},
194      *        {@link Opcodes#T_BYTE}, {@link Opcodes#T_SHORT},
195      *        {@link Opcodes#T_INT} or {@link Opcodes#T_LONG}.
196      */
visitIntInsn(int opcode, int operand)197     void visitIntInsn(int opcode, int operand);
198 
199     /**
200      * Visits a local variable instruction. A local variable instruction is an
201      * instruction that loads or stores the value of a local variable.
202      *
203      * @param opcode the opcode of the local variable instruction to be visited.
204      *        This opcode is either ILOAD, LLOAD, FLOAD, DLOAD, ALOAD, ISTORE,
205      *        LSTORE, FSTORE, DSTORE, ASTORE or RET.
206      * @param var the operand of the instruction to be visited. This operand is
207      *        the index of a local variable.
208      */
visitVarInsn(int opcode, int var)209     void visitVarInsn(int opcode, int var);
210 
211     /**
212      * Visits a type instruction. A type instruction is an instruction that
213      * takes the internal name of a class as parameter.
214      *
215      * @param opcode the opcode of the type instruction to be visited. This
216      *        opcode is either NEW, ANEWARRAY, CHECKCAST or INSTANCEOF.
217      * @param type the operand of the instruction to be visited. This operand
218      *        must be the internal name of an object or array class (see {@link
219      *        Type#getInternalName() getInternalName}).
220      */
visitTypeInsn(int opcode, String type)221     void visitTypeInsn(int opcode, String type);
222 
223     /**
224      * Visits a field instruction. A field instruction is an instruction that
225      * loads or stores the value of a field of an object.
226      *
227      * @param opcode the opcode of the type instruction to be visited. This
228      *        opcode is either GETSTATIC, PUTSTATIC, GETFIELD or PUTFIELD.
229      * @param owner the internal name of the field's owner class (see {@link
230      *        Type#getInternalName() getInternalName}).
231      * @param name the field's name.
232      * @param desc the field's descriptor (see {@link Type Type}).
233      */
visitFieldInsn(int opcode, String owner, String name, String desc)234     void visitFieldInsn(int opcode, String owner, String name, String desc);
235 
236     /**
237      * Visits a method instruction. A method instruction is an instruction that
238      * invokes a method.
239      *
240      * @param opcode the opcode of the type instruction to be visited. This
241      *        opcode is either INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC or
242      *        INVOKEINTERFACE.
243      * @param owner the internal name of the method's owner class (see {@link
244      *        Type#getInternalName() getInternalName}).
245      * @param name the method's name.
246      * @param desc the method's descriptor (see {@link Type Type}).
247      */
visitMethodInsn(int opcode, String owner, String name, String desc)248     void visitMethodInsn(int opcode, String owner, String name, String desc);
249 
250     /**
251      * Visits a jump instruction. A jump instruction is an instruction that may
252      * jump to another instruction.
253      *
254      * @param opcode the opcode of the type instruction to be visited. This
255      *        opcode is either IFEQ, IFNE, IFLT, IFGE, IFGT, IFLE, IF_ICMPEQ,
256      *        IF_ICMPNE, IF_ICMPLT, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ACMPEQ,
257      *        IF_ACMPNE, GOTO, JSR, IFNULL or IFNONNULL.
258      * @param label the operand of the instruction to be visited. This operand
259      *        is a label that designates the instruction to which the jump
260      *        instruction may jump.
261      */
visitJumpInsn(int opcode, Label label)262     void visitJumpInsn(int opcode, Label label);
263 
264     /**
265      * Visits a label. A label designates the instruction that will be visited
266      * just after it.
267      *
268      * @param label a {@link Label Label} object.
269      */
visitLabel(Label label)270     void visitLabel(Label label);
271 
272     // -------------------------------------------------------------------------
273     // Special instructions
274     // -------------------------------------------------------------------------
275 
276     /**
277      * Visits a LDC instruction.
278      *
279      * @param cst the constant to be loaded on the stack. This parameter must be
280      *        a non null {@link Integer}, a {@link Float}, a {@link Long}, a
281      *        {@link Double} a {@link String} (or a {@link Type} for
282      *        <tt>.class</tt> constants, for classes whose version is 49.0 or
283      *        more).
284      */
visitLdcInsn(Object cst)285     void visitLdcInsn(Object cst);
286 
287     /**
288      * Visits an IINC instruction.
289      *
290      * @param var index of the local variable to be incremented.
291      * @param increment amount to increment the local variable by.
292      */
visitIincInsn(int var, int increment)293     void visitIincInsn(int var, int increment);
294 
295     /**
296      * Visits a TABLESWITCH instruction.
297      *
298      * @param min the minimum key value.
299      * @param max the maximum key value.
300      * @param dflt beginning of the default handler block.
301      * @param labels beginnings of the handler blocks. <tt>labels[i]</tt> is
302      *        the beginning of the handler block for the <tt>min + i</tt> key.
303      */
visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels)304     void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels);
305 
306     /**
307      * Visits a LOOKUPSWITCH instruction.
308      *
309      * @param dflt beginning of the default handler block.
310      * @param keys the values of the keys.
311      * @param labels beginnings of the handler blocks. <tt>labels[i]</tt> is
312      *        the beginning of the handler block for the <tt>keys[i]</tt> key.
313      */
visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels)314     void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels);
315 
316     /**
317      * Visits a MULTIANEWARRAY instruction.
318      *
319      * @param desc an array type descriptor (see {@link Type Type}).
320      * @param dims number of dimensions of the array to allocate.
321      */
visitMultiANewArrayInsn(String desc, int dims)322     void visitMultiANewArrayInsn(String desc, int dims);
323 
324     // -------------------------------------------------------------------------
325     // Exceptions table entries, debug information, max stack and max locals
326     // -------------------------------------------------------------------------
327 
328     /**
329      * Visits a try catch block.
330      *
331      * @param start beginning of the exception handler's scope (inclusive).
332      * @param end end of the exception handler's scope (exclusive).
333      * @param handler beginning of the exception handler's code.
334      * @param type internal name of the type of exceptions handled by the
335      *        handler, or <tt>null</tt> to catch any exceptions (for "finally"
336      *        blocks).
337      * @throws IllegalArgumentException if one of the labels has already been
338      *         visited by this visitor (by the {@link #visitLabel visitLabel}
339      *         method).
340      */
visitTryCatchBlock(Label start, Label end, Label handler, String type)341     void visitTryCatchBlock(Label start, Label end, Label handler, String type);
342 
343     /**
344      * Visits a local variable declaration.
345      *
346      * @param name the name of a local variable.
347      * @param desc the type descriptor of this local variable.
348      * @param signature the type signature of this local variable. May be
349      *        <tt>null</tt> if the local variable type does not use generic
350      *        types.
351      * @param start the first instruction corresponding to the scope of this
352      *        local variable (inclusive).
353      * @param end the last instruction corresponding to the scope of this local
354      *        variable (exclusive).
355      * @param index the local variable's index.
356      * @throws IllegalArgumentException if one of the labels has not already
357      *         been visited by this visitor (by the
358      *         {@link #visitLabel visitLabel} method).
359      */
visitLocalVariable( String name, String desc, String signature, Label start, Label end, int index)360     void visitLocalVariable(
361         String name,
362         String desc,
363         String signature,
364         Label start,
365         Label end,
366         int index);
367 
368     /**
369      * Visits a line number declaration.
370      *
371      * @param line a line number. This number refers to the source file from
372      *        which the class was compiled.
373      * @param start the first instruction corresponding to this line number.
374      * @throws IllegalArgumentException if <tt>start</tt> has not already been
375      *         visited by this visitor (by the {@link #visitLabel visitLabel}
376      *         method).
377      */
visitLineNumber(int line, Label start)378     void visitLineNumber(int line, Label start);
379 
380     /**
381      * Visits the maximum stack size and the maximum number of local variables
382      * of the method.
383      *
384      * @param maxStack maximum stack size of the method.
385      * @param maxLocals maximum number of local variables for the method.
386      */
visitMaxs(int maxStack, int maxLocals)387     void visitMaxs(int maxStack, int maxLocals);
388 
389     /**
390      * Visits the end of the method. This method, which is the last one to be
391      * called, is used to inform the visitor that all the annotations and
392      * attributes of the method have been visited.
393      */
visitEnd()394     void visitEnd();
395 }
396