Example usage for org.objectweb.asm Opcodes RETURN

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

Introduction

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

Prototype

int RETURN

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

Click Source Link

Usage

From source file:org.jacoco.core.runtime.ExecutionDataAccessTest.java

License:Open Source License

@Test
public void testGenerateAccessCall() throws Exception {
    final boolean[] data = store.get(Long.valueOf(1234), "Sample", 5).getData();

    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object",
            new String[] { Type.getInternalName(Callable.class) });

    // Constructor
    MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null,
            new String[0]);
    mv.visitCode();/*from   ww w . j  av  a 2  s  .co m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, "Sample", "access", "Ljava/lang/Object;");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    // call()
    mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "Sample", "access", "Ljava/lang/Object;");
    ExecutionDataAccess.generateAccessCall(1234, "Sample", 5, mv);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(6, 1);
    mv.visitEnd();

    writer.visitField(Opcodes.ACC_PRIVATE, "access", "Ljava/lang/Object;", null, null);

    writer.visitEnd();
    final TargetLoader loader = new TargetLoader("Sample", writer.toByteArray());
    Callable<?> callable = (Callable<?>) loader.getTargetClass().getConstructor(Object.class)
            .newInstance(access);
    assertSame(data, callable.call());
}

From source file:org.jacoco.core.runtime.RuntimeDataTest.java

License:Open Source License

@Test
public void testGenerateArgumentArray() throws Exception {
    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object",
            new String[] { Type.getInternalName(Callable.class) });

    // Constructor
    MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, new String[0]);
    mv.visitCode();/*  w  w w. j  av  a  2s  .c om*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();

    // call()
    mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]);
    mv.visitCode();
    RuntimeData.generateArgumentArray(1000, "Sample", 15, mv);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(5, 1);
    mv.visitEnd();

    writer.visitEnd();
    final TargetLoader loader = new TargetLoader("Sample", writer.toByteArray());
    Callable<?> callable = (Callable<?>) loader.newTargetInstance();
    final Object[] args = (Object[]) callable.call();
    assertEquals(3, args.length, 0.0);
    assertEquals(Long.valueOf(1000), args[0]);
    assertEquals("Sample", args[1]);
    assertEquals(Integer.valueOf(15), args[2]);
}

From source file:org.jacoco.core.runtime.RuntimeDataTest.java

License:Open Source License

@Test
public void testGenerateAccessCall() throws Exception {
    final boolean[] probes = data.getExecutionData(Long.valueOf(1234), "Sample", 5).getProbes();

    final ClassWriter writer = new ClassWriter(0);
    writer.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "Sample", null, "java/lang/Object",
            new String[] { Type.getInternalName(Callable.class) });

    // Constructor
    MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null,
            new String[0]);
    mv.visitCode();//from  w  w w  . jav  a2 s .  co  m
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitFieldInsn(Opcodes.PUTFIELD, "Sample", "access", "Ljava/lang/Object;");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();

    // call()
    mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, "Sample", "access", "Ljava/lang/Object;");
    RuntimeData.generateAccessCall(1234, "Sample", 5, mv);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(6, 1);
    mv.visitEnd();

    writer.visitField(Opcodes.ACC_PRIVATE, "access", "Ljava/lang/Object;", null, null);

    writer.visitEnd();
    final TargetLoader loader = new TargetLoader("Sample", writer.toByteArray());
    Callable<?> callable = (Callable<?>) loader.getTargetClass().getConstructor(Object.class).newInstance(data);
    assertSame(probes, callable.call());
}

From source file:org.jacoco.core.test.validation.java5.StructuredLockingTest.java

License:Open Source License

private void assertStructuredLocking(String owner, MethodNode mn) throws Exception {
    Analyzer<BasicValue> analyzer = new Analyzer<BasicValue>(new BasicInterpreter()) {

        @Override/*from w  w  w.j  av  a2  s  . c  om*/
        protected Frame<BasicValue> newFrame(int nLocals, int nStack) {
            return new LockFrame(nLocals, nStack);
        }

        @Override
        protected Frame<BasicValue> newFrame(Frame<? extends BasicValue> src) {
            return new LockFrame(src);
        }
    };

    Frame<BasicValue>[] frames = analyzer.analyze(owner, mn);

    // Make sure no locks are left when method exits:
    for (int i = 0; i < frames.length; i++) {
        AbstractInsnNode insn = mn.instructions.get(i);
        switch (insn.getOpcode()) {
        case Opcodes.IRETURN:
        case Opcodes.LRETURN:
        case Opcodes.FRETURN:
        case Opcodes.DRETURN:
        case Opcodes.ARETURN:
        case Opcodes.RETURN:
            ((LockFrame) frames[i]).assertNoLock("Exit with lock");
            break;
        case Opcodes.ATHROW:
            List<TryCatchBlockNode> handlers = analyzer.getHandlers(i);
            if (handlers == null || handlers.isEmpty()) {
                ((LockFrame) frames[i]).assertNoLock("Exit with lock");
            }
            break;
        }
    }

    // Only instructions protected by a catch-all handler can hold locks:
    for (int i = 0; i < frames.length; i++) {
        AbstractInsnNode insn = mn.instructions.get(i);
        if (insn.getOpcode() > 0) {
            boolean catchAll = false;
            List<TryCatchBlockNode> handlers = analyzer.getHandlers(i);
            if (handlers != null) {
                for (TryCatchBlockNode node : handlers) {
                    catchAll |= node.type == null;
                }
            }
            if (!catchAll) {
                ((LockFrame) frames[i]).assertNoLock("No handlers for insn with lock");
            }
        }
    }

}

From source file:org.jboss.byteman.agent.adapter.RuleTriggerMethodAdapter.java

License:Open Source License

@Override
public void visitInsn(int opcode) {
    super.visitInsn(opcode);
    // look for interesting instructions which need inserting into the CFG
    switch (opcode) {
    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
    case Opcodes.FRETURN:
    case Opcodes.DRETURN:
    case Opcodes.ARETURN:
    case Opcodes.RETURN:
    case Opcodes.ATHROW: {
        // add this instruction to the current block and then start a new current block
        cfg.add(opcode);/*from   w w  w . j  a  v a  2 s.c o m*/
        Label newStart = super.newLabel();
        // must call split before visiting the label
        cfg.split(newStart);
        visitLabel(newStart);
    }
        break;
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT: {
        // just add this instruction to the current block
        cfg.add(opcode);
    }
        break;
    default: {
        cfg.add(opcode);
    }
    }
}

From source file:org.jboss.byteman.agent.adapter.RuleTriggerMethodAdapter.java

License:Open Source License

@Override
public void visitMaxs(int maxStack, int maxLocals) {
    Type returnType = Type.getReturnType(descriptor);

    // check whether there are outstanding monitor opens at the start of the trigger
    // block and, if so, insert a handler which unlocks the monitor and then rethrows
    // the exception.

    Iterator<TriggerDetails> iterator = cfg.triggerDetails();
    boolean noneLeft = true;

    while (iterator.hasNext()) {
        TriggerDetails details = iterator.next();
        // see if this trigger has any open monitor enters which will need closing
        Iterator<CodeLocation> openEnters = cfg.getOpenMonitors(details);
        if (openEnters.hasNext()) {
            // add a handler here which unlocks each object and rethrows the
            // saved exception then protect it with a try catch block and update
            // the trigger details so that it is the target of this block

            // generate a rethrow handler for each exception type

            Label newStart = newLabel();
            Label newEnd = newLabel();

            // if we get here the return and throw handlers labels should be null
            if (details.getEarlyReturnHandler() != null || details.getThrowHandler() != null) {
                System.out.println("unexpected : trigger region with open monitorenters has subtype handler!");
            }/*from w  ww  . ja va2s . com*/

            // generate rethrow code and mark the handler as a try catch block for all
            // three exception types
            newStart = newLabel();
            newEnd = newLabel();
            Label newEarlyReturn = newLabel();
            Label newThrow = newLabel();
            visitLabel(details.getExecuteHandler());
            Label newExecute = newLabel();
            visitLabel(newStart);
            while (openEnters.hasNext()) {
                CodeLocation enterLocation = openEnters.next();
                int varIdx = cfg.getSavedMonitorIdx(enterLocation);
                // call super method to avoid indexing these instructions
                visitVarInsn(Opcodes.ALOAD, varIdx);
                visitInsn(Opcodes.MONITOREXIT);
            }
            visitInsn(Opcodes.ATHROW);
            // add try catch blocks for the 3 exception types
            visitTryCatchBlock(newStart, newEnd, newEarlyReturn, CFG.EARLY_RETURN_EXCEPTION_TYPE_NAME);
            visitTryCatchBlock(newStart, newEnd, newThrow, CFG.THROW_EXCEPTION_TYPE_NAME);
            visitTryCatchBlock(newStart, newEnd, newExecute, CFG.EXECUTE_EXCEPTION_TYPE_NAME);
            // visit the end of the handler blocks so they gets processed
            visitLabel(newEnd);
            // update the details so it tracks these new ecetpion regions
            details.setThrowHandler(newThrow);
            details.setEarlyReturnHandler(newEarlyReturn);
            details.setExecuteHandler(newExecute);
        }
    }

    // ok, so now we have to add the handler code for trigger block try catch handlers
    // we only need to add the handler code once but we need to make sure it is the target of
    //  all the try catch blocks by visiting their handler label before we insert the code

    iterator = cfg.triggerDetails();

    while (iterator.hasNext()) {
        TriggerDetails details = iterator.next();
        visitLabel(details.getEarlyReturnHandler());
    }

    if (Transformer.isVerbose()) {
        getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class));
        visitLdcInsn("caught ReturnException");
        invokeVirtual(Type.getType(PrintStream.class), Method.getMethod("void println(String)"));
    }
    // add exception handling code subclass first
    if (returnType == Type.VOID_TYPE) {
        // drop exception and just return
        pop();
        visitInsn(Opcodes.RETURN);
    } else {
        // fetch value from exception, unbox if needed and return value
        Method getReturnValueMethod = Method.getMethod("Object getReturnValue()");
        invokeVirtual(CFG.EARLY_RETURN_EXCEPTION_TYPE, getReturnValueMethod);
        unbox(returnType);
        returnValue();
    }

    iterator = cfg.triggerDetails();

    while (iterator.hasNext()) {
        TriggerDetails details = iterator.next();
        visitLabel(details.getThrowHandler());
    }

    if (Transformer.isVerbose()) {
        getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class));
        visitLdcInsn("caught ThrowException");
        invokeVirtual(Type.getType(PrintStream.class), Method.getMethod("void println(String)"));
    }
    // fetch value from exception, unbox if needed and return value
    Method getThrowableMethod = Method.getMethod("Throwable getThrowable()");
    invokeVirtual(CFG.THROW_EXCEPTION_TYPE, getThrowableMethod);
    throwException();

    // execute exception  comes last because it is the super of the othher two classes

    iterator = cfg.triggerDetails();

    while (iterator.hasNext()) {
        TriggerDetails details = iterator.next();
        visitLabel(details.getExecuteHandler());
    }

    if (Transformer.isVerbose()) {
        getStatic(Type.getType(System.class), "out", Type.getType(PrintStream.class));
        visitLdcInsn("caught ExecuteException");
        invokeVirtual(Type.getType(PrintStream.class), Method.getMethod("void println(String)"));
    }
    // rethrow an execute exception
    throwException();

    super.visitMaxs(maxStack, maxLocals);

    // hmm, don't think we need this
    cfg.visitMaxs();
}

From source file:org.jvnet.jax_ws_commons.beans_generator.ambassador.impl.asm.ASMRequestBeanGenerator.java

License:Open Source License

/**
 * /*from w  w w .ja  va  2 s . c o  m*/
 */
private void generateNoArgsConstructor() {
    MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:org.jvnet.jax_ws_commons.beans_generator.ambassador.impl.asm.ASMResponseBeanGenerator.java

License:Open Source License

private void generateNoArgsConstructor() {
    MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();/*w  ww  .  j  av  a  2s.  c o m*/
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
}

From source file:org.kantega.notsoserial.CreateBytesIT.java

License:Apache License

private byte[] createTransletBytes() {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "com/example/InvokingTranslet", null,
            Type.getType(AbstractTranslet.class).getInternalName(),
            new String[] { Type.getType(Serializable.class).getInternalName() });

    MethodVisitor init = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    init.visitCode();//www  .  j  av a  2s.  c om
    init.visitVarInsn(Opcodes.ALOAD, 0);
    init.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getType(AbstractTranslet.class).getInternalName(),
            "<init>", "()V");
    init.visitVarInsn(Opcodes.ALOAD, 0);
    init.visitIntInsn(Opcodes.BIPUSH, 101);
    init.visitFieldInsn(Opcodes.PUTFIELD, Type.getType(AbstractTranslet.class).getInternalName(),
            "transletVersion", "I");
    init.visitInsn(Opcodes.RETURN);
    init.visitMaxs(2, 2);
    init.visitEnd();

    MethodVisitor transformMethod = cw.visitMethod(Opcodes.ACC_PUBLIC, "transform",
            Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { Type.getType(DOM.class), Type.getType(DTMAxisIterator.class) }),
            null, new String[] { Type.getType(TransletException.class).getInternalName() });

    transformMethod.visitCode();
    transformMethod.visitInsn(Opcodes.RETURN);
    transformMethod.visitEnd();

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();
    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    mv.visitLdcInsn("HMM..");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
    mv.visitLdcInsn("pwned");
    mv.visitLdcInsn("true");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "setProperty",
            "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
    mv.visitInsn(Opcodes.POP);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 0);
    mv.visitEnd();
    cw.visitEnd();

    return cw.toByteArray();
}

From source file:org.kantega.notsoserial.ReadObjectClassVisitor.java

License:Apache License

@Override
public void visitEnd() {
    if (!isInterface() && isSerializable() && !hasReadObject) {

        MethodVisitor mv = super.visitMethod(Opcodes.ACC_PRIVATE, "readObject", readObjectDescription, null,
                readObjectExceptions);//from  ww  w .ja v a 2 s .  c o m
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLdcInsn(className);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC,
                Type.getType(NotSoSerialClassFileTransformer.class).getInternalName(),
                onReadObjectCallbackMethod, "(Ljava/lang/String;)V", false);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/ObjectInputStream", "defaultReadObject", "()V",
                false);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitInsn(Opcodes.RETURN);
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLocalVariable("this", Type.getObjectType(className).getDescriptor(), classSignature, l0, l3, 0);
        mv.visitLocalVariable("stream", Type.getType(ObjectInputStream.class).getDescriptor(), null, l0, l3, 1);
        mv.visitMaxs(1, 2);
        mv.visitEnd();

    }
    super.visitEnd();
}