Example usage for org.objectweb.asm Opcodes INVOKEINTERFACE

List of usage examples for org.objectweb.asm Opcodes INVOKEINTERFACE

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes INVOKEINTERFACE.

Prototype

int INVOKEINTERFACE

To view the source code for org.objectweb.asm Opcodes INVOKEINTERFACE.

Click Source Link

Usage

From source file:edu.illinois.nondex.instr.PriorityQueueShufflingAdder.java

License:Open Source License

public void addHasNext() {
    MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC, "hasNext", "()Z", null, null);
    mv.visitCode();/* ww  w .  j a v a  2 s  .  c om*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/PriorityQueue$Itr", "iter", "Ljava/util/Iterator;");
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z", true);
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:edu.illinois.nondex.instr.PriorityQueueShufflingAdder.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if ("hasNext".equals(name)) {
        return super.visitMethod(access, "originalHasNext", desc, signature, exceptions);
    }//from w ww  .  ja  va 2  s .  c  o m
    if ("next".equals(name)) {
        return super.visitMethod(access, "originalNext", desc, signature, exceptions);
    }
    if ("<init>".equals(name)) {
        return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
            @Override
            public void visitInsn(int opcode) {
                if (opcode == Opcodes.RETURN) {
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 1);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/PriorityQueue$Itr", "this$0",
                            "Ljava/util/PriorityQueue;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ICONST_0);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/PriorityQueue$Itr", "cursor", "I");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ICONST_M1);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/PriorityQueue$Itr", "lastRet", "I");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/PriorityQueue$Itr", "forgetMeNot",
                            "Ljava/util/ArrayDeque;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/PriorityQueue$Itr", "lastRetElt",
                            "Ljava/lang/Object;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/PriorityQueue$Itr", "this$0",
                            "Ljava/util/PriorityQueue;");
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/PriorityQueue", "modCount", "I");
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/PriorityQueue$Itr", "expectedModCount",
                            "I");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList");
                    super.visitInsn(Opcodes.DUP);
                    super.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/PriorityQueue$Itr", "elements",
                            "Ljava/util/List;");
                    Label l0 = new Label();
                    super.visitLabel(l0);
                    super.visitFrame(Opcodes.F_FULL, 2,
                            new Object[] { "java/util/PriorityQueue$Itr", "java/util/PriorityQueue" }, 0,
                            new Object[] {});
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/PriorityQueue$Itr",
                            "originalHasNext", "()Z", false);
                    Label l1 = new Label();
                    super.visitJumpInsn(Opcodes.IFEQ, l1);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/PriorityQueue$Itr", "elements",
                            "Ljava/util/List;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/PriorityQueue$Itr", "originalNext",
                            "()Ljava/lang/Object;", false);
                    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add",
                            "(Ljava/lang/Object;)Z", true);
                    super.visitInsn(Opcodes.POP);
                    super.visitJumpInsn(Opcodes.GOTO, l0);
                    super.visitLabel(l1);
                    super.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/PriorityQueue$Itr", "elements",
                            "Ljava/util/List;");
                    super.visitMethodInsn(Opcodes.INVOKESTATIC,
                            "edu/illinois/nondex/shuffling/ControlNondeterminism", "shuffle",
                            "(Ljava/util/List;)Ljava/util/List;", false);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/PriorityQueue$Itr", "elements",
                            "Ljava/util/List;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/PriorityQueue$Itr", "elements",
                            "Ljava/util/List;");
                    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "iterator",
                            "()Ljava/util/Iterator;", true);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/PriorityQueue$Itr", "iter",
                            "Ljava/util/Iterator;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ICONST_M1);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/PriorityQueue$Itr", "lastRet", "I");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/PriorityQueue$Itr", "lastRetElt",
                            "Ljava/lang/Object;");
                }
                super.visitInsn(opcode);
            }
        };
    }
    return super.visitMethod(access, name, desc, signature, exceptions);
}

From source file:edu.illinois.nondex.instr.WeakHashMapShufflingAdder.java

License:Open Source License

public void addHasNext() {
    MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC, "hasNext", "()Z", null, null);
    mv.visitCode();//w ww  .  ja v  a  2  s.  c om
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "iter", "Ljava/util/Iterator;");
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z", true);
    mv.visitInsn(Opcodes.IRETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:edu.illinois.nondex.instr.WeakHashMapShufflingAdder.java

License:Open Source License

public void addNextEntry() {
    MethodVisitor mv = super.visitMethod(Opcodes.ACC_PUBLIC, "nextEntry", "()Ljava/util/WeakHashMap$Entry;",
            "()Ljava/util/WeakHashMap$Entry<TK;TV;>;", null);
    mv.visitCode();/* ww w . j a va 2  s  .  com*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "this$0",
            "Ljava/util/WeakHashMap;");
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap", "modCount", "I");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "expectedModCount", "I");
    Label l0 = new Label();
    mv.visitJumpInsn(Opcodes.IF_ICMPEQ, l0);
    mv.visitTypeInsn(Opcodes.NEW, "java/util/ConcurrentModificationException");
    mv.visitInsn(Opcodes.DUP);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ConcurrentModificationException", "<init>", "()V",
            false);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitLabel(l0);
    mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "iter", "Ljava/util/Iterator;");
    mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;", true);
    mv.visitTypeInsn(Opcodes.CHECKCAST, "java/util/WeakHashMap$Entry");
    mv.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "lastReturned",
            "Ljava/util/WeakHashMap$Entry;");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "lastReturned",
            "Ljava/util/WeakHashMap$Entry;");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/WeakHashMap$Entry", "get", "()Ljava/lang/Object;",
            false);
    mv.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "currentKey",
            "Ljava/lang/Object;");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "lastReturned",
            "Ljava/util/WeakHashMap$Entry;");
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(2, 1);
    mv.visitEnd();
}

From source file:edu.illinois.nondex.instr.WeakHashMapShufflingAdder.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if ("hasNext".equals(name)) {
        return super.visitMethod(access, "originalHasNext", desc, signature, exceptions);
    }/*from w w w .jav  a  2 s  .  c o  m*/
    if ("nextEntry".equals(name)) {
        return super.visitMethod(access, "originalNextEntry", desc, signature, exceptions);
    }
    if ("<init>".equals(name)) {
        return new MethodVisitor(Opcodes.ASM5, super.visitMethod(access, name, desc, signature, exceptions)) {
            @Override
            public void visitInsn(int opcode) {
                if (opcode == Opcodes.RETURN) {
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 1);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "this$0",
                            "Ljava/util/WeakHashMap;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "entry",
                            "Ljava/util/WeakHashMap$Entry;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "lastReturned",
                            "Ljava/util/WeakHashMap$Entry;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap$HashIterator", "this$0",
                            "Ljava/util/WeakHashMap;");
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap", "modCount", "I");
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator",
                            "expectedModCount", "I");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "nextKey",
                            "Ljava/lang/Object;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "currentKey",
                            "Ljava/lang/Object;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "iter",
                            "Ljava/util/Iterator;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 1);
                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/WeakHashMap", "isEmpty", "()Z",
                            false);
                    Label l0 = new Label();
                    super.visitJumpInsn(Opcodes.IFEQ, l0);
                    super.visitInsn(Opcodes.ICONST_0);
                    Label l1 = new Label();
                    super.visitJumpInsn(Opcodes.GOTO, l1);
                    super.visitLabel(l0);
                    super.visitFrame(Opcodes.F_FULL, 2,
                            new Object[] { "java/util/WeakHashMap$HashIterator", "java/util/WeakHashMap" }, 1,
                            new Object[] { "java/util/WeakHashMap$HashIterator" });
                    super.visitVarInsn(Opcodes.ALOAD, 1);
                    super.visitFieldInsn(Opcodes.GETFIELD, "java/util/WeakHashMap", "table",
                            "[Ljava/util/WeakHashMap$Entry;");
                    super.visitInsn(Opcodes.ARRAYLENGTH);
                    super.visitLabel(l1);
                    super.visitFrame(Opcodes.F_FULL, 2,
                            new Object[] { "java/util/WeakHashMap$HashIterator", "java/util/WeakHashMap" }, 2,
                            new Object[] { "java/util/WeakHashMap$HashIterator", Opcodes.INTEGER });
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "index", "I");
                    super.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList");
                    super.visitInsn(Opcodes.DUP);
                    super.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
                    super.visitVarInsn(Opcodes.ASTORE, 2);
                    Label l2 = new Label();
                    super.visitLabel(l2);
                    super.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/util/List" }, 0, null);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/WeakHashMap$HashIterator",
                            "originalHasNext", "()Z", false);
                    Label l3 = new Label();
                    super.visitJumpInsn(Opcodes.IFEQ, l3);
                    super.visitVarInsn(Opcodes.ALOAD, 2);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/WeakHashMap$HashIterator",
                            "originalNextEntry", "()Ljava/util/WeakHashMap$Entry;", false);
                    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add",
                            "(Ljava/lang/Object;)Z", true);
                    super.visitInsn(Opcodes.POP);
                    super.visitJumpInsn(Opcodes.GOTO, l2);
                    super.visitLabel(l3);
                    super.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
                    super.visitVarInsn(Opcodes.ALOAD, 2);
                    super.visitMethodInsn(Opcodes.INVOKESTATIC,
                            "edu/illinois/nondex/shuffling/ControlNondeterminism", "shuffle",
                            "(Ljava/util/List;)Ljava/util/List;", false);
                    super.visitVarInsn(Opcodes.ASTORE, 2);
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitVarInsn(Opcodes.ALOAD, 2);
                    super.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "iterator",
                            "()Ljava/util/Iterator;", true);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "iter",
                            "Ljava/util/Iterator;");
                    super.visitVarInsn(Opcodes.ALOAD, 0);
                    super.visitInsn(Opcodes.ACONST_NULL);
                    super.visitFieldInsn(Opcodes.PUTFIELD, "java/util/WeakHashMap$HashIterator", "lastReturned",
                            "Ljava/util/WeakHashMap$Entry;");
                }
                super.visitInsn(opcode);
            }
        };
    }
    return super.visitMethod(access, name, desc, signature, exceptions);
}

From source file:edu.mit.streamjit.util.bytecode.MethodUnresolver.java

License:Open Source License

private void emit(CallInst i, InsnList insns) {
    Method m = i.getMethod();// w  ww . ja va  2s.  c o m
    boolean callingSuperCtor = false;
    if (m.isConstructor()) {
        //If we're calling super(), load this.
        //TODO: this will get confused if we call a superclass constructor
        //for any reason other than our own initialization!
        if (method.isConstructor() && method.getParent().getSuperclass().equals(m.getParent())) {
            load(method.arguments().get(0), insns);
            callingSuperCtor = true;
        } else {
            insns.add(new TypeInsnNode(Opcodes.NEW, internalName(m.getType().getReturnType().getKlass())));
            insns.add(new InsnNode(Opcodes.DUP));
        }
    }
    int opcode;
    if (m.modifiers().contains(Modifier.STATIC))
        opcode = Opcodes.INVOKESTATIC;
    else if (m.isConstructor() || m.getAccess().equals(Access.PRIVATE) ||
    //We're calling a superclass method we've overridden.
            (Iterables.contains(method.getParent().superclasses(), m.getParent()))
                    && method.getParent().getMethodByVirtual(m.getName(), m.getType()) != m)
        opcode = Opcodes.INVOKESPECIAL;
    else if (m.getParent().modifiers().contains(Modifier.INTERFACE))
        //TODO: may not be correct?
        opcode = Opcodes.INVOKEINTERFACE;
    else
        opcode = Opcodes.INVOKEVIRTUAL;

    String owner = internalName(m.getParent());
    //hack to make cloning arrays work
    if (opcode == Opcodes.INVOKESPECIAL && m.getName().equals("clone")
            && i.getArgument(0).getType() instanceof ArrayType) {
        opcode = Opcodes.INVOKEVIRTUAL;
        owner = internalName(((ArrayType) i.getArgument(0).getType()).getKlass());
    }

    for (Value v : i.arguments())
        load(v, insns);
    insns.add(new MethodInsnNode(opcode, owner, m.getName(), i.callDescriptor()));

    if (!(i.getType() instanceof VoidType) && !callingSuperCtor)
        store(i, insns);
}

From source file:fr.insalyon.telecom.jooflux.InvokeMethodTransformer.java

License:Mozilla Public License

private boolean isInvokeInterface(MethodInsnNode methodInsnNode) {
    return methodInsnNode.getOpcode() == Opcodes.INVOKEINTERFACE;
}

From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java

License:Open Source License

private void generateStub() throws IOException {
    stubname = fullclassname + "_Stub";
    String stubclassname = classname + "_Stub";
    File file = new File((destination == null ? "." : destination) + File.separator
            + stubname.replace('.', File.separatorChar) + ".class");

    if (verbose)/*ww w.ja  va  2  s  .c  o m*/
        System.out.println("[Generating class " + stubname + "]");

    final ClassWriter stub = new ClassWriter(true);
    classInternalName = stubname.replace('.', '/');
    final String superInternalName = Type.getType(RemoteStub.class).getInternalName();

    String[] remoteInternalNames = internalNameArray((Class[]) mRemoteInterfaces.toArray(new Class[] {}));
    stub.visit(Opcodes.V1_2, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, classInternalName, null, superInternalName,
            remoteInternalNames);

    if (need12Stubs) {
        stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "serialVersionUID",
                Type.LONG_TYPE.getDescriptor(), null, new Long(2L));
    }

    if (need11Stubs) {
        stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "interfaceHash",
                Type.LONG_TYPE.getDescriptor(), null, new Long(RMIHashes.getInterfaceHash(clazz)));

        if (need12Stubs) {
            stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC, "useNewInvoke",
                    Type.BOOLEAN_TYPE.getDescriptor(), null, null);
        }

        stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, "operations",
                Type.getDescriptor(Operation[].class), null, null);
    }

    // Set of method references.
    if (need12Stubs) {
        for (int i = 0; i < remotemethods.length; i++) {
            Method m = remotemethods[i].meth;
            String slotName = "$method_" + m.getName() + "_" + i;
            stub.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC, slotName,
                    Type.getDescriptor(Method.class), null, null);
        }
    }

    MethodVisitor clinit = stub.visitMethod(Opcodes.ACC_STATIC, "<clinit>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null);

    if (need11Stubs) {
        fillOperationArray(clinit);
        if (!need12Stubs)
            clinit.visitInsn(Opcodes.RETURN);
    }

    if (need12Stubs) {
        // begin of try
        Label begin = new Label();

        // beginning of catch
        Label handler = new Label();
        clinit.visitLabel(begin);

        // Initialize the methods references.
        if (need11Stubs) {
            /*
             * RemoteRef.class.getMethod("invoke", new Class[] {
             *   Remote.class, Method.class, Object[].class, long.class })
             */
            generateClassConstant(clinit, RemoteRef.class);
            clinit.visitLdcInsn("invoke");
            generateClassArray(clinit, new Class[] { Remote.class, Method.class, Object[].class, long.class });
            clinit.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Class.class), "getMethod",
                    Type.getMethodDescriptor(Type.getType(Method.class),
                            new Type[] { Type.getType(String.class), Type.getType(Class[].class) }));

            // useNewInvoke = true
            clinit.visitInsn(Opcodes.ICONST_1);
            clinit.visitFieldInsn(Opcodes.PUTSTATIC, classInternalName, "useNewInvoke",
                    Type.BOOLEAN_TYPE.getDescriptor());
        }

        generateStaticMethodObjs(clinit);

        // jump past handler
        clinit.visitInsn(Opcodes.RETURN);
        clinit.visitLabel(handler);
        if (need11Stubs) {
            // useNewInvoke = false
            clinit.visitInsn(Opcodes.ICONST_0);
            clinit.visitFieldInsn(Opcodes.PUTSTATIC, classInternalName, "useNewInvoke",
                    Type.BOOLEAN_TYPE.getDescriptor());
            clinit.visitInsn(Opcodes.RETURN);
        } else {
            // throw NoSuchMethodError
            clinit.visitTypeInsn(Opcodes.NEW, typeArg(NoSuchMethodError.class));
            clinit.visitInsn(Opcodes.DUP);
            clinit.visitLdcInsn("stub class initialization failed");
            clinit.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(NoSuchMethodError.class),
                    "<init>",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(String.class) }));
            clinit.visitInsn(Opcodes.ATHROW);
        }

        clinit.visitTryCatchBlock(begin, handler, handler, Type.getInternalName(NoSuchMethodException.class));

    }

    clinit.visitMaxs(-1, -1);

    generateClassForNamer(stub);

    // Constructors
    if (need11Stubs) {
        // no arg public constructor
        MethodVisitor code = stub.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null);
        code.visitVarInsn(Opcodes.ALOAD, 0);
        code.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "<init>",
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
        code.visitInsn(Opcodes.RETURN);

        code.visitMaxs(-1, -1);
    }

    // public RemoteRef constructor
    MethodVisitor constructor = stub.visitMethod(Opcodes.ACC_PUBLIC, "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteRef.class) }), null, null);
    constructor.visitVarInsn(Opcodes.ALOAD, 0);
    constructor.visitVarInsn(Opcodes.ALOAD, 1);
    constructor.visitMethodInsn(Opcodes.INVOKESPECIAL, superInternalName, "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteRef.class) }));
    constructor.visitInsn(Opcodes.RETURN);
    constructor.visitMaxs(-1, -1);

    // Method implementations
    for (int i = 0; i < remotemethods.length; i++) {
        Method m = remotemethods[i].meth;
        Class[] sig = m.getParameterTypes();
        Class returntype = m.getReturnType();
        Class[] except = sortExceptions((Class[]) remotemethods[i].exceptions.toArray(new Class[0]));

        MethodVisitor code = stub.visitMethod(Opcodes.ACC_PUBLIC, m.getName(),
                Type.getMethodDescriptor(Type.getType(returntype), typeArray(sig)), null,
                internalNameArray(typeArray(except)));

        final Variables var = new Variables();

        // this and parameters are the declared vars
        var.declare("this");
        for (int j = 0; j < sig.length; j++)
            var.declare(param(m, j), size(sig[j]));

        Label methodTryBegin = new Label();
        code.visitLabel(methodTryBegin);

        if (need12Stubs) {
            Label oldInvoke = new Label();
            if (need11Stubs) {
                // if not useNewInvoke jump to old invoke
                code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "useNewInvoke",
                        Type.getDescriptor(boolean.class));
                code.visitJumpInsn(Opcodes.IFEQ, oldInvoke);
            }

            // this.ref
            code.visitVarInsn(Opcodes.ALOAD, var.get("this"));
            code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref",
                    Type.getDescriptor(RemoteRef.class));

            // "this" is first arg to invoke
            code.visitVarInsn(Opcodes.ALOAD, var.get("this"));

            // method object is second arg to invoke
            String methName = "$method_" + m.getName() + "_" + i;
            code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, methName,
                    Type.getDescriptor(Method.class));

            // args to remote method are third arg to invoke
            if (sig.length == 0)
                code.visitInsn(Opcodes.ACONST_NULL);
            else {
                // create arg Object[] (with boxed primitives) and push it
                code.visitLdcInsn(new Integer(sig.length));
                code.visitTypeInsn(Opcodes.ANEWARRAY, typeArg(Object.class));

                var.allocate("argArray");
                code.visitVarInsn(Opcodes.ASTORE, var.get("argArray"));

                for (int j = 0; j < sig.length; j++) {
                    int size = size(sig[j]);
                    int insn = loadOpcode(sig[j]);
                    Class box = sig[j].isPrimitive() ? box(sig[j]) : null;

                    code.visitVarInsn(Opcodes.ALOAD, var.get("argArray"));
                    code.visitLdcInsn(new Integer(j));

                    // put argument on stack
                    if (box != null) {
                        code.visitTypeInsn(Opcodes.NEW, typeArg(box));
                        code.visitInsn(Opcodes.DUP);
                        code.visitVarInsn(insn, var.get(param(m, j)));
                        code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(box), "<init>",
                                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(sig[j]) }));
                    } else
                        code.visitVarInsn(insn, var.get(param(m, j)));

                    code.visitInsn(Opcodes.AASTORE);
                }

                code.visitVarInsn(Opcodes.ALOAD, var.deallocate("argArray"));
            }

            // push remote operation opcode
            code.visitLdcInsn(new Long(remotemethods[i].hash));
            code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "invoke",
                    Type.getMethodDescriptor(Type.getType(Object.class),
                            new Type[] { Type.getType(Remote.class), Type.getType(Method.class),
                                    Type.getType(Object[].class), Type.LONG_TYPE }));

            if (!returntype.equals(Void.TYPE)) {
                int retcode = returnOpcode(returntype);
                Class boxCls = returntype.isPrimitive() ? box(returntype) : null;
                code.visitTypeInsn(Opcodes.CHECKCAST, typeArg(boxCls == null ? returntype : boxCls));
                if (returntype.isPrimitive()) {
                    // unbox
                    code.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(boxCls).getInternalName(),
                            unboxMethod(returntype),
                            Type.getMethodDescriptor(Type.getType(returntype), new Type[] {}));
                }

                code.visitInsn(retcode);
            } else
                code.visitInsn(Opcodes.RETURN);

            if (need11Stubs)
                code.visitLabel(oldInvoke);
        }

        if (need11Stubs) {

            // this.ref.newCall(this, operations, index, interfaceHash)
            code.visitVarInsn(Opcodes.ALOAD, var.get("this"));
            code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref",
                    Type.getDescriptor(RemoteRef.class));

            // "this" is first arg to newCall
            code.visitVarInsn(Opcodes.ALOAD, var.get("this"));

            // operations is second arg to newCall
            code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "operations",
                    Type.getDescriptor(Operation[].class));

            // method index is third arg
            code.visitLdcInsn(new Integer(i));

            // interface hash is fourth arg
            code.visitFieldInsn(Opcodes.GETSTATIC, classInternalName, "interfaceHash",
                    Type.LONG_TYPE.getDescriptor());

            code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "newCall",
                    Type.getMethodDescriptor(Type.getType(RemoteCall.class),
                            new Type[] { Type.getType(RemoteObject.class), Type.getType(Operation[].class),
                                    Type.INT_TYPE, Type.LONG_TYPE }));

            // store call object on stack and leave copy on stack
            var.allocate("call");
            code.visitInsn(Opcodes.DUP);
            code.visitVarInsn(Opcodes.ASTORE, var.get("call"));

            Label beginArgumentTryBlock = new Label();
            code.visitLabel(beginArgumentTryBlock);

            // ObjectOutput out = call.getOutputStream();
            code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class),
                    "getOutputStream",
                    Type.getMethodDescriptor(Type.getType(ObjectOutput.class), new Type[] {}));

            for (int j = 0; j < sig.length; j++) {
                // dup the ObjectOutput
                code.visitInsn(Opcodes.DUP);

                // get j'th arg to remote method
                code.visitVarInsn(loadOpcode(sig[j]), var.get(param(m, j)));

                Class argCls = sig[j].isPrimitive() ? sig[j] : Object.class;

                // out.writeFoo
                code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class),
                        writeMethod(sig[j]),
                        Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(argCls) }));
            }

            // pop ObjectOutput
            code.visitInsn(Opcodes.POP);

            Label iohandler = new Label();
            Label endArgumentTryBlock = new Label();
            code.visitJumpInsn(Opcodes.GOTO, endArgumentTryBlock);
            code.visitLabel(iohandler);

            // throw new MarshalException(msg, ioexception);
            code.visitVarInsn(Opcodes.ASTORE, var.allocate("exception"));
            code.visitTypeInsn(Opcodes.NEW, typeArg(MarshalException.class));
            code.visitInsn(Opcodes.DUP);
            code.visitLdcInsn("error marshalling arguments");
            code.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception"));
            code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(MarshalException.class), "<init>",
                    Type.getMethodDescriptor(Type.VOID_TYPE,
                            new Type[] { Type.getType(String.class), Type.getType(Exception.class) }));
            code.visitInsn(Opcodes.ATHROW);

            code.visitLabel(endArgumentTryBlock);
            code.visitTryCatchBlock(beginArgumentTryBlock, iohandler, iohandler,
                    Type.getInternalName(IOException.class));

            // this.ref.invoke(call)
            code.visitVarInsn(Opcodes.ALOAD, var.get("this"));
            code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref",
                    Type.getDescriptor(RemoteRef.class));
            code.visitVarInsn(Opcodes.ALOAD, var.get("call"));
            code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "invoke",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteCall.class) }));

            // handle return value
            boolean needcastcheck = false;

            Label beginReturnTryCatch = new Label();
            code.visitLabel(beginReturnTryCatch);

            int returncode = returnOpcode(returntype);

            if (!returntype.equals(Void.TYPE)) {
                // call.getInputStream()
                code.visitVarInsn(Opcodes.ALOAD, var.get("call"));
                code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class),
                        "getInputStream",
                        Type.getMethodDescriptor(Type.getType(ObjectInput.class), new Type[] {}));

                Class readCls = returntype.isPrimitive() ? returntype : Object.class;
                code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectInput.class),
                        readMethod(returntype), Type.getMethodDescriptor(Type.getType(readCls), new Type[] {}));

                boolean castresult = false;

                if (!returntype.isPrimitive()) {
                    if (!returntype.equals(Object.class))
                        castresult = true;
                    else
                        needcastcheck = true;
                }

                if (castresult)
                    code.visitTypeInsn(Opcodes.CHECKCAST, typeArg(returntype));

                // leave result on stack for return
            }

            // this.ref.done(call)
            code.visitVarInsn(Opcodes.ALOAD, var.get("this"));
            code.visitFieldInsn(Opcodes.GETFIELD, Type.getInternalName(RemoteObject.class), "ref",
                    Type.getDescriptor(RemoteRef.class));
            code.visitVarInsn(Opcodes.ALOAD, var.deallocate("call"));
            code.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteRef.class), "done",
                    Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(RemoteCall.class) }));

            // return; or return result;
            code.visitInsn(returncode);

            // exception handler
            Label handler = new Label();
            code.visitLabel(handler);
            code.visitVarInsn(Opcodes.ASTORE, var.allocate("exception"));

            // throw new UnmarshalException(msg, e)
            code.visitTypeInsn(Opcodes.NEW, typeArg(UnmarshalException.class));
            code.visitInsn(Opcodes.DUP);
            code.visitLdcInsn("error unmarshalling return");
            code.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception"));
            code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnmarshalException.class),
                    "<init>", Type.getMethodDescriptor(Type.VOID_TYPE,
                            new Type[] { Type.getType(String.class), Type.getType(Exception.class) }));
            code.visitInsn(Opcodes.ATHROW);

            Label endReturnTryCatch = new Label();

            // catch IOException
            code.visitTryCatchBlock(beginReturnTryCatch, handler, handler,
                    Type.getInternalName(IOException.class));

            if (needcastcheck) {
                // catch ClassNotFoundException
                code.visitTryCatchBlock(beginReturnTryCatch, handler, handler,
                        Type.getInternalName(ClassNotFoundException.class));
            }
        }

        Label rethrowHandler = new Label();
        code.visitLabel(rethrowHandler);
        // rethrow declared exceptions
        code.visitInsn(Opcodes.ATHROW);

        boolean needgeneral = true;
        for (int j = 0; j < except.length; j++) {
            if (except[j] == Exception.class)
                needgeneral = false;
        }

        for (int j = 0; j < except.length; j++) {
            code.visitTryCatchBlock(methodTryBegin, rethrowHandler, rethrowHandler,
                    Type.getInternalName(except[j]));
        }

        if (needgeneral) {
            // rethrow unchecked exceptions
            code.visitTryCatchBlock(methodTryBegin, rethrowHandler, rethrowHandler,
                    Type.getInternalName(RuntimeException.class));

            Label generalHandler = new Label();
            code.visitLabel(generalHandler);
            String msg = "undeclared checked exception";

            // throw new java.rmi.UnexpectedException(msg, e)
            code.visitVarInsn(Opcodes.ASTORE, var.allocate("exception"));
            code.visitTypeInsn(Opcodes.NEW, typeArg(UnexpectedException.class));
            code.visitInsn(Opcodes.DUP);
            code.visitLdcInsn(msg);
            code.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception"));
            code.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnexpectedException.class),
                    "<init>", Type.getMethodDescriptor(Type.VOID_TYPE,
                            new Type[] { Type.getType(String.class), Type.getType(Exception.class) }));
            code.visitInsn(Opcodes.ATHROW);

            code.visitTryCatchBlock(methodTryBegin, rethrowHandler, generalHandler,
                    Type.getInternalName(Exception.class));
        }

        code.visitMaxs(-1, -1);
    }

    stub.visitEnd();
    byte[] classData = stub.toByteArray();
    if (!noWrite) {
        if (file.exists())
            file.delete();
        if (file.getParentFile() != null)
            file.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(classData);
        fos.flush();
        fos.close();
    }
}

From source file:gnu.classpath.tools.rmic.ClassRmicCompiler.java

License:Open Source License

private void generateMethodSkel(MethodVisitor cv, Method m, Variables var) {
    Class[] sig = m.getParameterTypes();

    Label readArgs = new Label();
    cv.visitLabel(readArgs);//  ww  w . j a  va 2  s. co  m

    boolean needcastcheck = false;

    // ObjectInput in = call.getInputStream();
    cv.visitVarInsn(Opcodes.ALOAD, var.get("remotecall"));
    cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "getInputStream",
            Type.getMethodDescriptor(Type.getType(ObjectInput.class), new Type[] {}));
    cv.visitVarInsn(Opcodes.ASTORE, var.allocate("objectinput"));

    for (int i = 0; i < sig.length; i++) {
        // dup input stream
        cv.visitVarInsn(Opcodes.ALOAD, var.get("objectinput"));

        Class readCls = sig[i].isPrimitive() ? sig[i] : Object.class;

        // in.readFoo()
        cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectInput.class), readMethod(sig[i]),
                Type.getMethodDescriptor(Type.getType(readCls), new Type[] {}));

        if (!sig[i].isPrimitive() && !sig[i].equals(Object.class)) {
            needcastcheck = true;
            cv.visitTypeInsn(Opcodes.CHECKCAST, typeArg(sig[i]));
        }

        // store arg in variable
        cv.visitVarInsn(storeOpcode(sig[i]), var.allocate(param(m, i), size(sig[i])));
    }

    var.deallocate("objectinput");

    Label doCall = new Label();
    Label closeInput = new Label();

    cv.visitJumpInsn(Opcodes.JSR, closeInput);
    cv.visitJumpInsn(Opcodes.GOTO, doCall);

    // throw new UnmarshalException
    Label handler = new Label();
    cv.visitLabel(handler);
    cv.visitVarInsn(Opcodes.ASTORE, var.allocate("exception"));
    cv.visitTypeInsn(Opcodes.NEW, typeArg(UnmarshalException.class));
    cv.visitInsn(Opcodes.DUP);
    cv.visitLdcInsn("error unmarshalling arguments");
    cv.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception"));
    cv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(UnmarshalException.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { Type.getType(String.class), Type.getType(Exception.class) }));
    cv.visitVarInsn(Opcodes.ASTORE, var.allocate("toThrow"));
    cv.visitJumpInsn(Opcodes.JSR, closeInput);
    cv.visitVarInsn(Opcodes.ALOAD, var.get("toThrow"));
    cv.visitInsn(Opcodes.ATHROW);

    cv.visitTryCatchBlock(readArgs, handler, handler, Type.getInternalName(IOException.class));
    if (needcastcheck) {
        cv.visitTryCatchBlock(readArgs, handler, handler, Type.getInternalName(ClassCastException.class));
    }

    // finally block
    cv.visitLabel(closeInput);
    cv.visitVarInsn(Opcodes.ASTORE, var.allocate("retAddress"));
    cv.visitVarInsn(Opcodes.ALOAD, var.get("remotecall"));
    cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "releaseInputStream",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
    cv.visitVarInsn(Opcodes.RET, var.deallocate("retAddress"));
    var.deallocate("toThrow");

    // do the call using args stored as variables
    cv.visitLabel(doCall);
    cv.visitVarInsn(Opcodes.ALOAD, var.get("remoteobj"));
    for (int i = 0; i < sig.length; i++)
        cv.visitVarInsn(loadOpcode(sig[i]), var.deallocate(param(m, i)));
    cv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(clazz), m.getName(),
            Type.getMethodDescriptor(m));

    Class returntype = m.getReturnType();
    if (!returntype.equals(Void.TYPE)) {
        cv.visitVarInsn(storeOpcode(returntype), var.allocate("result", size(returntype)));
    }

    // write result to result stream
    Label writeResult = new Label();
    cv.visitLabel(writeResult);
    cv.visitVarInsn(Opcodes.ALOAD, var.get("remotecall"));
    cv.visitInsn(Opcodes.ICONST_1);
    cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(RemoteCall.class), "getResultStream",
            Type.getMethodDescriptor(Type.getType(ObjectOutput.class), new Type[] { Type.BOOLEAN_TYPE }));

    if (!returntype.equals(Void.TYPE)) {
        // out.writeFoo(result)
        cv.visitVarInsn(loadOpcode(returntype), var.deallocate("result"));
        Class writeCls = returntype.isPrimitive() ? returntype : Object.class;
        cv.visitMethodInsn(Opcodes.INVOKEINTERFACE, Type.getInternalName(ObjectOutput.class),
                writeMethod(returntype),
                Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(writeCls) }));
    }

    cv.visitInsn(Opcodes.RETURN);

    // throw new MarshalException
    Label marshalHandler = new Label();
    cv.visitLabel(marshalHandler);
    cv.visitVarInsn(Opcodes.ASTORE, var.allocate("exception"));
    cv.visitTypeInsn(Opcodes.NEW, typeArg(MarshalException.class));
    cv.visitInsn(Opcodes.DUP);
    cv.visitLdcInsn("error marshalling return");
    cv.visitVarInsn(Opcodes.ALOAD, var.deallocate("exception"));
    cv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(MarshalException.class), "<init>",
            Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { Type.getType(String.class), Type.getType(Exception.class) }));
    cv.visitInsn(Opcodes.ATHROW);
    cv.visitTryCatchBlock(writeResult, marshalHandler, marshalHandler, Type.getInternalName(IOException.class));
}

From source file:io.awacs.plugin.springmvc.ServiceWrapper.java

License:Apache License

/**
 * origin method: (XXX)X/*w w w  .  ja  v  a  2s.c  om*/
 * <p>
 * proxy method: (XXXLjavax/servlet/http/HttpServletRequest;)X
 * req -> localIndex - 1
 *
 * @param cn
 * @param origin
 * @return
 */
private MethodNode doProxy(ClassNode cn, MethodNode origin) {

    MethodNode newNode = copyMethod(origin);
    //move annotations
    hideAnnotations(origin);

    LabelNode l0 = new LabelNode();
    LabelNode l1 = new LabelNode();
    LabelNode l2 = new LabelNode();

    String returnType = origin.desc.substring(origin.desc.indexOf(')') + 1);

    int localIndex = copyParameters(origin, newNode, l0, l2);

    newNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Exception"));

    //localIndex -> elapsedTime
    newNode.localVariables.add(new LocalVariableNode("elapsedTime", "J", null, l0, l1, localIndex));

    //
    newNode.instructions.add(l0);

    // long elapsedTime = System.currentTimeMillis();
    newNode.instructions
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J"));
    newNode.instructions.add(new VarInsnNode(Opcodes.LSTORE, localIndex));

    int paramCount = getParamCount(origin.desc, origin.access);

    //load?
    for (int i = 0; i < paramCount; i++) {
        LocalVariableNode node = (LocalVariableNode) newNode.localVariables.get(i);
        switch (node.desc) {
        case "J":
            newNode.instructions.add(new VarInsnNode(Opcodes.LLOAD, node.index));
            break;
        case "D":
            newNode.instructions.add(new VarInsnNode(Opcodes.DLOAD, node.index));
            break;
        case "F":
            newNode.instructions.add(new VarInsnNode(Opcodes.FLOAD, node.index));
            break;
        case "I":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        case "S":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        case "Z":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        case "B":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        case "C":
            newNode.instructions.add(new VarInsnNode(Opcodes.ILOAD, node.index));
            break;
        default:
            newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, node.index));
            break;
        }
    }
    //
    newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, cn.name, origin.name, origin.desc));

    //
    newNode.instructions
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J"));
    newNode.instructions.add(new VarInsnNode(Opcodes.LLOAD, localIndex));
    newNode.instructions.add(new InsnNode(Opcodes.LSUB));
    newNode.instructions.add(new VarInsnNode(Opcodes.LSTORE, localIndex));

    newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, localIndex - 1));
    newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE,
            "javax/servlet/http/HttpServletRequest", "getRequestURI", "()Ljava/lang/String;"));

    //?
    newNode.instructions.add(new VarInsnNode(Opcodes.LLOAD, localIndex));
    //SpringmvcPlugin.incrAccess(uri, elapsedTime);
    newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            "io/awacs/plugin/springmvc/SpringmvcPlugin", "incrAccess", "(Ljava/lang/String;J)V"));
    //
    newNode.instructions.add(l1);
    switch (returnType) {
    case "J":
        newNode.instructions.add(new InsnNode(Opcodes.LRETURN));
        break;
    case "D":
        newNode.instructions.add(new InsnNode(Opcodes.DRETURN));
        break;
    case "F":
        newNode.instructions.add(new InsnNode(Opcodes.FRETURN));
        break;
    case "I":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "S":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "C":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "B":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    case "Z":
        newNode.instructions.add(new InsnNode(Opcodes.IRETURN));
        break;
    default:
        newNode.instructions.add(new InsnNode(Opcodes.ARETURN));
        break;
    }

    newNode.instructions.add(l2);
    newNode.instructions
            .add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Exception" }));
    newNode.instructions.add(new VarInsnNode(Opcodes.ASTORE, localIndex));

    newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, localIndex - 1));
    newNode.instructions.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE,
            "javax/servlet/http/HttpServletRequest", "getRequestURI", "()Ljava/lang/String;"));

    newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, localIndex));
    newNode.instructions
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, "io/awacs/plugin/springmvc/SpringmvcPlugin",
                    "incrFailure", "(Ljava/lang/String;Ljava/lang/Throwable;)V"));

    newNode.instructions.add(new VarInsnNode(Opcodes.ALOAD, localIndex));
    newNode.instructions.add(new InsnNode(Opcodes.ATHROW));
    newNode.maxLocals = origin.maxLocals + 3;
    newNode.maxStack = Math.max(localIndex + 2, 6);
    return newNode;
}