MethodVisitor to insert before, after and around advices in methods and constructors.
For constructors, the code keeps track of the elements on the stack in order to detect when the
super class constructor is called (note that there can be multiple such calls in different
branches). onMethodEnter is called after each super class constructor call, because the
object cannot be used before it is properly initialized.
| Modifier and Type | Field and Description |
|---|---|
| private Map | forwardJumpStackFrames
The stack map frames corresponding to the labels of the forward jumps made *before* the super class constructor has been called (note that the Java Virtual Machine forbids backward jumps before the super class constructor is called). |
| private static final String | INVALID_OPCODE
Prefix of the error message when invalid opcodes are found. |
| private final boolean | isConstructor
Whether the visited method is a constructor. |
| protected int | methodAccess
The access flags of the visited method. |
| protected String | methodDesc
The descriptor of the visited method. |
| private static final Object | OTHER
Any value other than "uninitialized this". |
| private List | stackFrame
The values on the current execution stack frame (long and double are represented by two elements). |
| private boolean | superClassConstructorCalled
Whether the super class constructor has been called (if the visited method is a constructor), at the current instruction. |
| private static final Object | UNINITIALIZED_THIS
Hides org. |
| Access | Constructor and Description |
|---|---|
| protected | AdviceAdapter(final int
the ASM API version implemented by this visitor. Must be one of the api, final MethodVisitor ASMx values in Opcodes.the method visitor to which this adapter delegates calls. methodVisitor, final int the method's access flags (see access, final String Opcodes).the method's name. name, final String the method's descriptor (see descriptor)Type).Constructs a new |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| private void | |
| private void | |
| private void | |
| protected void | |
| protected void | |
| private Object | |
| private Object | |
| private void | |
| public void | visitCode()
Overrides org. |
| public void | visitFieldInsn(final int
the opcode of the type instruction to be visited. This opcode is either
GETSTATIC, PUTSTATIC, GETFIELD or PUTFIELD. opcode, final String the internal name of the field's owner class (see owner, final String Type#getInternalName()).the field's name. name, final String the field's descriptor (see descriptor)Type).Overrides org. |
| public void | visitInsn(final int
the opcode of the instruction to be visited. This opcode is either NOP,
ACONST_NULL, ICONST_M1, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5,
LCONST_0, LCONST_1, FCONST_0, FCONST_1, FCONST_2, DCONST_0, DCONST_1, IALOAD, LALOAD,
FALOAD, DALOAD, AALOAD, BALOAD, CALOAD, SALOAD, IASTORE, LASTORE, FASTORE, DASTORE,
AASTORE, BASTORE, CASTORE, SASTORE, POP, POP2, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2,
SWAP, IADD, LADD, FADD, DADD, ISUB, LSUB, FSUB, DSUB, IMUL, LMUL, FMUL, DMUL, IDIV, LDIV,
FDIV, DDIV, IREM, LREM, FREM, DREM, INEG, LNEG, FNEG, DNEG, ISHL, LSHL, ISHR, LSHR, IUSHR,
LUSHR, IAND, LAND, IOR, LOR, IXOR, LXOR, I2L, I2F, I2D, L2I, L2F, L2D, F2I, F2L, F2D, D2I,
D2L, D2F, I2B, I2C, I2S, LCMP, FCMPL, FCMPG, DCMPL, DCMPG, IRETURN, LRETURN, FRETURN,
DRETURN, ARETURN, RETURN, ARRAYLENGTH, ATHROW, MONITORENTER, or MONITOREXIT. opcode)Overrides org. |
| public void | visitIntInsn(final int
the opcode of the instruction to be visited. This opcode is either BIPUSH, SIPUSH
or NEWARRAY. opcode, final int the operand of the instruction to be visited. operand)When opcode is BIPUSH, operand value should be between Byte.MIN_VALUE and Byte.MAX_VALUE. When opcode is SIPUSH, operand value should be between Short.MIN_VALUE and Short.MAX_VALUE. When opcode is NEWARRAY, operand value should be one of Opcodes#T_BOOLEAN, Opcodes#T_CHAR, Opcodes#T_FLOAT, Opcodes#T_DOUBLE, Opcodes#T_BYTE,
Opcodes#T_SHORT, Opcodes#T_INT or Opcodes#T_LONG.Overrides org. |
| public void | visitInvokeDynamicInsn(final String
the method's name. name, final String the method's descriptor (see descriptor, final Handle Type).the bootstrap method. bootstrapMethodHandle, final Object... bootstrapMethodArguments)Overrides org. |
| public void | visitJumpInsn(final int
the opcode of the type instruction to be visited. This opcode is either IFEQ,
IFNE, IFLT, IFGE, IFGT, IFLE, IF_ICMPEQ, IF_ICMPNE, IF_ICMPLT, IF_ICMPGE, IF_ICMPGT,
IF_ICMPLE, IF_ACMPEQ, IF_ACMPNE, GOTO, JSR, IFNULL or IFNONNULL. opcode, final Label the operand of the instruction to be visited. This operand is a label that
designates the instruction to which the jump instruction may jump. label)Overrides org. |
| public void | visitLabel(final Label
a label)Label object.Overrides org. |
| public void | visitLdcInsn(final Object
the constant to be loaded on the stack. This parameter must be a non null value)Integer, a Float, a Long, a Double, a String, a Type of OBJECT or ARRAY sort for .class constants, for classes whose version is
49, a Type of METHOD sort for MethodType, a Handle for MethodHandle
constants, for classes whose version is 51 or a ConstantDynamic for a constant
dynamic for classes whose version is 55.Overrides org. |
| public void | visitLookupSwitchInsn(final Label
beginning of the default handler block. dflt, final int[] the values of the keys. keys, final Label[] beginnings of the handler blocks. labels)labels[i] is the beginning of the
handler block for the keys[i] key.Overrides org. |
| public void | visitMethodInsn(final int
the opcode of the type instruction to be visited. This opcode is either
INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC or INVOKEINTERFACE. opcodeAndSource, final String the internal name of the method's owner class (see owner, final String Type#getInternalName()).the method's name. name, final String the method's descriptor (see descriptor, final boolean Type).if the method's owner class is an interface. isInterface)Overrides org. |
| public void | visitMultiANewArrayInsn(final String
an array type descriptor (see descriptor, final int Type).the number of dimensions of the array to allocate. numDimensions)Overrides org. |
| public void | visitTableSwitchInsn(final int
the minimum key value. min, final int the maximum key value. max, final Label beginning of the default handler block. dflt, final Label... beginnings of the handler blocks. labels)labels[i] is the beginning of the
handler block for the min + i key.Overrides org. |
| public void | visitTryCatchBlock(final Label
the beginning of the exception handler's scope (inclusive). start, final Label the end of the exception handler's scope (exclusive). end, final Label the beginning of the exception handler's code. handler, final String the internal name of the type of exceptions handled by the handler (see type)Type#getInternalName()), or null to catch any exceptions (for "finally"
blocks).Overrides org. |
| public void | visitTypeInsn(final int
the opcode of the type instruction to be visited. This opcode is either NEW,
ANEWARRAY, CHECKCAST or INSTANCEOF. opcode, final String the operand of the instruction to be visited. This operand must be the internal
name of an object or array class (see type)Type#getInternalName()).Overrides org. |
| public void | visitVarInsn(final int
the opcode of the local variable instruction to be visited. This opcode is either
ILOAD, LLOAD, FLOAD, DLOAD, ALOAD, ISTORE, LSTORE, FSTORE, DSTORE, ASTORE or RET. opcode, final int the operand of the instruction to be visited. This operand is the index of a
local variable. varIndex)Overrides org. |
| forwardJumpStackFrames | back to summary |
|---|---|
| private Map<Label, List<Object>> forwardJumpStackFrames The stack map frames corresponding to the labels of the forward jumps made *before* the super
class constructor has been called (note that the Java Virtual Machine forbids backward jumps
before the super class constructor is called). Note that by definition (cf. the 'before'), when
we reach a label from this map, | |
| INVALID_OPCODE | back to summary |
|---|---|
| private static final String INVALID_OPCODE Prefix of the error message when invalid opcodes are found. | |
| isConstructor | back to summary |
|---|---|
| private final boolean isConstructor Whether the visited method is a constructor. | |
| methodAccess | back to summary |
|---|---|
| protected int methodAccess The access flags of the visited method. | |
| methodDesc | back to summary |
|---|---|
| protected String methodDesc The descriptor of the visited method. | |
| OTHER | back to summary |
|---|---|
| private static final Object OTHER Any value other than "uninitialized this". | |
| stackFrame | back to summary |
|---|---|
| private List<Object> stackFrame The values on the current execution stack frame (long and double are represented by two
elements). Each value is either | |
| superClassConstructorCalled | back to summary |
|---|---|
| private boolean superClassConstructorCalled Whether the super class constructor has been called (if the visited method is a constructor),
at the current instruction. There can be multiple call sites to the super constructor (e.g. for
Java code such as | |
| UNINITIALIZED_THIS | back to summary |
|---|---|
| private static final Object UNINITIALIZED_THIS Hides org. The "uninitialized this" value. | |
| AdviceAdapter | back to summary |
|---|---|
| protected AdviceAdapter(final int api, final MethodVisitor methodVisitor, final int access, final String name, final String descriptor) Constructs a new
| |
| addForwardJump | back to summary |
|---|---|
| private void addForwardJump(final Label label) | |
| addForwardJumps | back to summary |
|---|---|
| private void addForwardJumps(final Label dflt, final Label[] labels) | |
| doVisitMethodInsn | back to summary |
|---|---|
| private void doVisitMethodInsn(final int opcode, final String name, final String descriptor) | |
| endConstructorBasicBlockWithoutSuccessor | back to summary |
|---|---|
| private void endConstructorBasicBlockWithoutSuccessor() | |
| onMethodEnter | back to summary |
|---|---|
| protected void onMethodEnter() Generates the "before" advice for the visited method. The default implementation of this method does nothing. Subclasses can use or change all the local variables, but should not change state of the stack. This method is called at the beginning of the method or after super class constructor has been called (in constructors). | |
| onMethodExit | back to summary |
|---|---|
| protected void onMethodExit(final int opcode) Generates the "after" advice for the visited method. The default implementation of this method does nothing. Subclasses can use or change all the local variables, but should not change state of the stack. This method is called at the end of the method, just before return and athrow instructions. The top element on the stack contains the return value or the exception instance. For example:
public void onMethodExit(final int opcode) {
if (opcode == RETURN) {
visitInsn(ACONST_NULL);
} else if (opcode == ARETURN || opcode == ATHROW) {
dup();
} else {
if (opcode == LRETURN || opcode == DRETURN) {
dup2();
} else {
dup();
}
box(Type.getReturnType(this.methodDesc));
}
visitIntInsn(SIPUSH, opcode);
visitMethodInsn(INVOKESTATIC, owner, "onExit", "(Ljava/lang/Object;I)V");
}
// An actual call back method.
public static void onExit(final Object exitValue, final int opcode) {
...
}
| |
| peekValue | back to summary |
|---|---|
| private Object peekValue() | |
| popValue | back to summary |
|---|---|
| private Object popValue() | |
| pushValue | back to summary |
|---|---|
| private void pushValue(final Object value) | |
| visitCode | back to summary |
|---|---|
| public void visitCode() Overrides org. Doc from org. Starts the visit of the method's code, if any (i.e. non abstract method).
| |
| visitFieldInsn | back to summary |
|---|---|
| public void visitFieldInsn(final int opcode, final String owner, final String name, final String descriptor) Overrides org. Doc from org. Visits a field instruction. A field instruction is an instruction that loads or stores the value of a field of an object.
| |
| visitInsn | back to summary |
|---|---|
| public void visitInsn(final int opcode) Overrides org. Doc from org. Visits a zero operand instruction.
| |
| visitIntInsn | back to summary |
|---|---|
| public void visitIntInsn(final int opcode, final int operand) Overrides org. Doc from org. Visits an instruction with a single int operand.
| |
| visitInvokeDynamicInsn | back to summary |
|---|---|
| public void visitInvokeDynamicInsn(final String name, final String descriptor, final Handle bootstrapMethodHandle, final Object... bootstrapMethodArguments) Overrides org. Doc from org. Visits an invokedynamic instruction.
| |
| visitJumpInsn | back to summary |
|---|---|
| public void visitJumpInsn(final int opcode, final Label label) Overrides org. Doc from org. Visits a jump instruction. A jump instruction is an instruction that may jump to another instruction.
| |
| visitLabel | back to summary |
|---|---|
| public void visitLabel(final Label label) Overrides org. Doc from org. Visits a label. A label designates the instruction that will be visited just after it. | |
| visitLdcInsn | back to summary |
|---|---|
| public void visitLdcInsn(final Object value) Overrides org. Doc from org. Visits a LDC instruction. Note that new constant types may be added in future versions of the Java Virtual Machine. To easily detect new constant types, implementations of this method should check for unexpected constant types, like this:
if (cst instanceof Integer) {
// ...
} else if (cst instanceof Float) {
// ...
} else if (cst instanceof Long) {
// ...
} else if (cst instanceof Double) {
// ...
} else if (cst instanceof String) {
// ...
} else if (cst instanceof Type) {
int sort = ((Type) cst).getSort();
if (sort == Type.OBJECT) {
// ...
} else if (sort == Type.ARRAY) {
// ...
} else if (sort == Type.METHOD) {
// ...
} else {
// throw an exception
}
} else if (cst instanceof Handle) {
// ...
} else if (cst instanceof ConstantDynamic) {
// ...
} else {
// throw an exception
}
| |
| visitLookupSwitchInsn | back to summary |
|---|---|
| public void visitLookupSwitchInsn(final Label dflt, final int[] keys, final Label[] labels) Overrides org. Doc from org. Visits a LOOKUPSWITCH instruction. | |
| visitMethodInsn | back to summary |
|---|---|
| public void visitMethodInsn(final int opcodeAndSource, final String owner, final String name, final String descriptor, final boolean isInterface) Overrides org. Doc from org. Visits a method instruction. A method instruction is an instruction that invokes a method.
| |
| visitMultiANewArrayInsn | back to summary |
|---|---|
| public void visitMultiANewArrayInsn(final String descriptor, final int numDimensions) Overrides org. Doc from org. Visits a MULTIANEWARRAY instruction. | |
| visitTableSwitchInsn | back to summary |
|---|---|
| public void visitTableSwitchInsn(final int min, final int max, final Label dflt, final Label... labels) Overrides org. Doc from org. Visits a TABLESWITCH instruction. | |
| visitTryCatchBlock | back to summary |
|---|---|
| public void visitTryCatchBlock(final Label start, final Label end, final Label handler, final String type) Overrides org. Doc from org. Visits a try catch block.
| |
| visitTypeInsn | back to summary |
|---|---|
| public void visitTypeInsn(final int opcode, final String type) Overrides org. Doc from org. Visits a type instruction. A type instruction is an instruction that takes the internal name of
a class as parameter (see
| |
| visitVarInsn | back to summary |
|---|---|
| public void visitVarInsn(final int opcode, final int varIndex) Overrides org. Doc from org. Visits a local variable instruction. A local variable instruction is an instruction that loads or stores the value of a local variable.
| |