Class AdviceAdapter

  • All Implemented Interfaces:
    org.objectweb.asm.Opcodes

    public abstract class AdviceAdapter
    extends GeneratorAdapter
    implements org.objectweb.asm.Opcodes
    A MethodVisitor to insert before, after and around advices in methods and constructors.

    The behavior for constructors is the following:

    1. as long as the INVOKESPECIAL for the object initialization has not been reached, every bytecode instruction is dispatched in the ctor code visitor
    2. when this one is reached, it is only added in the ctor code visitor and a JP invoke is added
    3. after that, only the other code visitor receives the instructions
    Author:
    Eugene Kuleshov, Eric Bruneton
    • Field Detail

      • methodAccess

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

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

      • AdviceAdapter

        protected AdviceAdapter​(int api,
                                org.objectweb.asm.MethodVisitor methodVisitor,
                                int access,
                                java.lang.String name,
                                java.lang.String descriptor)
        Constructs a new AdviceAdapter.
        Parameters:
        api - the ASM API version implemented by this visitor. Must be one of Opcodes.ASM4, Opcodes.ASM5, Opcodes.ASM6 or Opcodes.ASM7_EXPERIMENTAL.
        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

      • visitCode

        public void visitCode()
        Overrides:
        visitCode in class org.objectweb.asm.MethodVisitor
      • visitLabel

        public void visitLabel​(org.objectweb.asm.Label label)
        Overrides:
        visitLabel in class org.objectweb.asm.MethodVisitor
      • visitInsn

        public void visitInsn​(int opcode)
        Overrides:
        visitInsn in class org.objectweb.asm.MethodVisitor
      • visitFieldInsn

        public void visitFieldInsn​(int opcode,
                                   java.lang.String owner,
                                   java.lang.String name,
                                   java.lang.String descriptor)
        Overrides:
        visitFieldInsn in class org.objectweb.asm.MethodVisitor
      • visitIntInsn

        public void visitIntInsn​(int opcode,
                                 int operand)
        Overrides:
        visitIntInsn in class org.objectweb.asm.MethodVisitor
      • visitLdcInsn

        public void visitLdcInsn​(java.lang.Object value)
        Overrides:
        visitLdcInsn in class org.objectweb.asm.MethodVisitor
      • visitMultiANewArrayInsn

        public void visitMultiANewArrayInsn​(java.lang.String descriptor,
                                            int numDimensions)
        Overrides:
        visitMultiANewArrayInsn in class org.objectweb.asm.MethodVisitor
      • visitTypeInsn

        public void visitTypeInsn​(int opcode,
                                  java.lang.String type)
        Overrides:
        visitTypeInsn in class org.objectweb.asm.MethodVisitor
      • visitMethodInsn

        @Deprecated
        public void visitMethodInsn​(int opcode,
                                    java.lang.String owner,
                                    java.lang.String name,
                                    java.lang.String descriptor)
        Deprecated.
        Overrides:
        visitMethodInsn in class org.objectweb.asm.MethodVisitor
      • visitMethodInsn

        public void visitMethodInsn​(int opcode,
                                    java.lang.String owner,
                                    java.lang.String name,
                                    java.lang.String descriptor,
                                    boolean isInterface)
        Overrides:
        visitMethodInsn in class org.objectweb.asm.MethodVisitor
      • visitInvokeDynamicInsn

        public void visitInvokeDynamicInsn​(java.lang.String name,
                                           java.lang.String descriptor,
                                           org.objectweb.asm.Handle bootstrapMethodHandle,
                                           java.lang.Object... bootstrapMethodArguments)
        Overrides:
        visitInvokeDynamicInsn in class org.objectweb.asm.MethodVisitor
      • visitJumpInsn

        public void visitJumpInsn​(int opcode,
                                  org.objectweb.asm.Label label)
        Overrides:
        visitJumpInsn in class org.objectweb.asm.MethodVisitor
      • visitLookupSwitchInsn

        public void visitLookupSwitchInsn​(org.objectweb.asm.Label dflt,
                                          int[] keys,
                                          org.objectweb.asm.Label[] labels)
        Overrides:
        visitLookupSwitchInsn in class org.objectweb.asm.MethodVisitor
      • visitTableSwitchInsn

        public void visitTableSwitchInsn​(int min,
                                         int max,
                                         org.objectweb.asm.Label dflt,
                                         org.objectweb.asm.Label... labels)
        Overrides:
        visitTableSwitchInsn in class org.objectweb.asm.MethodVisitor
      • visitTryCatchBlock

        public void visitTryCatchBlock​(org.objectweb.asm.Label start,
                                       org.objectweb.asm.Label end,
                                       org.objectweb.asm.Label handler,
                                       java.lang.String type)
        Overrides:
        visitTryCatchBlock in class org.objectweb.asm.MethodVisitor
      • 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.