我如何申请我写过的AdviceAdapter?

时间:2014-05-26 15:33:20

标签: java java-bytecode-asm

我有以下课程:

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.commons.AdviceAdapter;

public class NotEmptyAdvice extends AdviceAdapter
{
    protected NotEmptyAdvice(final MethodVisitor visitor, final int i, final String s, final String s2)
    {
        super(visitor, i, s, s2);
    }

    @Override
    protected void onMethodEnter()
    {
        super.mv.visitVarInsn(ALOAD, 1);
        super.mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "isEmpty", "()Z" );
        super.mv.visitVarInsn(IFEQ, 1);
        super.mv.visitTypeInsn(NEW, "java/lang/IllegalArgumentException");
        super.mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
        super.mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V");
        super.mv.visitVarInsn(ALOAD, 1);
        super.mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;" );
        super.mv.visitLdcInsn(" can not be empty!");
        super.mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;" );
        super.mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;" );
        super.mv.visitMethodInsn(INVOKESPECIAL, "java/lang/IllegalArgumentException", "<init>", "(Ljava/lang/String;)V");
        super.mv.visitInsn(ATHROW);
    }
}

asm库文档简洁而模糊,至少可以说。我想出了这么多,但广泛的谷歌搜索并没有透露我现在应该怎么做这个类我已经创建了它。

如何在运行时将此代码应用于我的对象?

1 个答案:

答案 0 :(得分:1)

我在这里汇总了一个快速代码示例:gist.github.com/VijayKrishna/1ca807c952187a7d8c4d,它通过相应的class-visitor / class-adapter显示如何使用方法适配器。

总之,你首先要调用&#34;类适配器(比如在main方法中)如下:

ClassReader cr = new ClassReader(in);
ClassWriter cw = new ClassWriter(ClassReader.EXPAND_FRAMES);
ReturnAdapter returnAdapter = new ReturnAdapter(cw, className);
cr.accept(returnAdapter, 0);

然后,您可以在类适配器的visitMethod方法中按照以下方法调整方法:

public MethodVisitor visitMethod(int access ... ... ... ) {
  MethodVisitor mv;
  mv = cv.visitMethod(access, name, desc, signature, exceptions);
  mv = new MethodReturnAdapter(Opcodes.ASM4, className, access, name, desc, mv);
  return mv;
}

以下是gist不可访问的整个代码段:

public class ReturnAdapter extends ClassVisitor {
  private String className;

  public ReturnAdapter(ClassVisitor cv, String className) {
    super(Opcodes.ASM4, cv);
  }

  @Override
  public MethodVisitor visitMethod(
    int access,
    String name,
    String desc,
    String signature,
    String[] exceptions) {
    MethodVisitor mv;
    mv = cv.visitMethod(access, name, desc, signature, exceptions);
    mv = new MethodReturnAdapter(Opcodes.ASM4, className, access, name, desc, mv);
    return mv;
  }

  public static void main(String[] args) throws IOException {
    String classFile = args[0];//path of the class file
    String className = args[1];//name of the class
    File inFile = new File(classFile);
    FileInputStream in = new FileInputStream(inFile);

    // adapting the class.
    ClassReader cr = new ClassReader(in);
    ClassWriter cw = new ClassWriter(ClassReader.EXPAND_FRAMES);
    ReturnAdapter returnAdapter = new ReturnAdapter(cw, className);
    cr.accept(returnAdapter, 0);
  }
}

/**
* Method Visitor that inserts code right before its return instruction(s),
* using the onMethodExit(int opcode) method of the AdviceAdapter class,
* from ASM(.ow2.org).
* @author vijay
*
*/
class MethodReturnAdapter extends AdviceAdapter {
  public MethodReturnAdapter(
      int api,
      String owner,
      int access,
      String name,
      String desc,
      MethodVisitor mv) {
    super(Opcodes.ASM4, mv, access, name, desc);
  }

  public MethodReturnAdapter(
      MethodVisitor mv,
      int access,
      String name,
      String desc) {
    super(Opcodes.ASM4, mv, access, name, desc);
  }

  @Override
  protected void onMethodEnter(int opcode) {
      mv.visitVarInsn(ALOAD, 1);
      // and/or any other visit instructions.
  }

  @Override
  protected void onMethodExit(int opcode) {
    if(opcode != Opcodes.ATHROW) {
      mv.visitVarInsn(Opcodes.ALOAD, 1);
      // and/or any other visit instructions.
    }
  }
}