Class AdviceAdapter

  • All Implemented Interfaces:
    Opcodes

    public abstract class AdviceAdapter
    extends GeneratorAdapter
    implements Opcodes
    A 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.
    Author:
    Eugene Kuleshov, Eric Bruneton
    • Field Detail

      • methodAccess

        protected int methodAccess
        The access flags of the visited method.
      • methodDesc

        protected String methodDesc
        The descriptor of the visited method.
    • Constructor Detail

      • AdviceAdapter

        protected AdviceAdapter​(int api,
                                MethodVisitor methodVisitor,
                                int access,
                                String name,
                                String descriptor)
        Constructs a new AdviceAdapter.
        Parameters:
        api - the ASM API version implemented by this visitor. Must be one of the ASMx values in Opcodes.
        methodVisitor - the method visitor to which this adapter delegates calls.
        access - the method's access flags (see Opcodes).
        name - the method's name.
        descriptor - the method's descriptor (see Type).
    • Method Detail

      • onMethodEnter

        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

        protected void onMethodExit​(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) {
           ...
         }
         
        Parameters:
        opcode - one of Opcodes.RETURN, Opcodes.IRETURN, Opcodes.FRETURN, Opcodes.ARETURN, Opcodes.LRETURN, Opcodes.DRETURN or Opcodes.ATHROW.