Example usage for org.objectweb.asm Opcodes ARETURN

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

Introduction

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

Prototype

int ARETURN

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

Click Source Link

Usage

From source file:org.jacoco.core.internal.analysis.filter.TryWithResourcesJavacFilterTest.java

License:Open Source License

/**
 * javac 7 and 8 for/*  w  ww .  j ava 2 s  .  c  o m*/
 *
 * <pre>
 *     try (r0 = ...; r1 = ...) {
 *         return ...
 *     } finally {
 *         ...
 *     }
 * </pre>
 *
 * generate
 *
 * <pre>
 *     ...
 *     ASTORE r0
 *     ACONST_NULL
 *     ASTORE primaryExc0
 *
 *     ...
 *     ASTORE r1
 *     ACONST_NULL
 *     ASTORE primaryExc1
 *
 *     ... // body
 *
 *     ALOAD r1
 *     IFNULL n
 *     ALOAD primaryExc1
 *     IFNULL c
 *     ALOAD r1
 *     INVOKEINTERFACE close:()V
 *     GOTO n
 *     ASTORE t
 *     ALOAD primaryExc1
 *     ALOAD t
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     GOTO n
 *     c:
 *     ALOAD r1
 *     INVOKEINTERFACE close:()V
 *     n:
 *
 *     ALOAD r0
 *     IFNULL n
 *     ALOAD primaryExc0
 *     IFNULL c
 *     ALOAD r0
 *     INVOKEVIRTUAL close:()V
 *     GOTO n
 *     ASTORE t
 *     ALOAD primaryExc0
 *     ALOAD t
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     GOTO n
 *     c:
 *     ALOAD r0
 *     INVOKEVIRTUAL close:()V
 *     n:
 *
 *     ... // finally on normal path
 *     ARETURN
 *
 *     ASTORE t
 *     ALOAD t
 *     ASTORE primaryExc1
 *     ALOAD t
 *     ATHROW
 *
 *     ASTORE t1
 *     ALOAD r1
 *     IFNULL e
 *     ALOAD primaryExc1
 *     IFNULL c
 *     ALOAD r1
 *     INVOKEINTERFACE close:()V
 *     GOTO e
 *     ASTORE t2
 *     ALOAD primaryExc1
 *     ALOAD t2
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     GOTO e
 *     c:
 *     ALOAD r1
 *     INVOKEINTERFACE close:()V
 *     e:
 *     ALOAD t1
 *     ATHROW
 *
 *     ASTORE t
 *     ALOAD t
 *     ASTORE primaryExc0
 *     ALOAD t
 *     ATHROW
 *
 *     ASTORE t1
 *     ALOAD r0
 *     IFNULL e
 *     ALOAD primaryExc0
 *     IFNULL c
 *     ALOAD r0
 *     INVOKEVIRTUAL close:()V
 *     GOTO e
 *     ASTORE t2
 *     ALOAD primaryExc0
 *     ALOAD t2
 *     INVOKEVIRTUAL java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V
 *     GOTO e
 *     c:
 *     ALOAD r0
 *     INVOKEVIRTUAL close:()V
 *     e:
 *     ALOAD t1
 *     ATHROW
 *
 *     ... // additional handlers for catch blocks and finally on exceptional path
 * </pre>
 */
@Test
public void javac_7_8() {
    final Range range0 = new Range();
    final Range range1 = new Range();
    final Range range2 = new Range();
    final Range range3 = new Range();

    final Label handler1 = new Label();
    m.visitTryCatchBlock(handler1, handler1, handler1, "java/lang/Throwable");
    final Label handler2 = new Label();
    m.visitTryCatchBlock(handler2, handler2, handler2, "java/lang/Throwable");

    // r1 = ...
    m.visitVarInsn(Opcodes.ASTORE, 1);

    // primaryExc1 = null
    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 2);

    // r2 = ...
    m.visitVarInsn(Opcodes.ASTORE, 3);
    // primaryExc2 = null
    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 4);

    // body
    m.visitInsn(Opcodes.NOP);

    m.visitInsn(Opcodes.ACONST_NULL);
    m.visitVarInsn(Opcodes.ASTORE, 5);

    final Label l15 = new Label();
    // if (r2 != null)
    m.visitVarInsn(Opcodes.ALOAD, 3);
    range0.fromInclusive = m.instructions.getLast();
    m.visitJumpInsn(Opcodes.IFNULL, l15);
    // if (primaryExc2 != null)
    m.visitVarInsn(Opcodes.ALOAD, 4);
    final Label l26 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l26);
    // r2.close
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitMethodInsn(Opcodes.INVOKEINTERFACE, "Fun$Resource2", "close", "()V", false);
    m.visitJumpInsn(Opcodes.GOTO, l15);

    m.visitVarInsn(Opcodes.ASTORE, 6);
    m.visitVarInsn(Opcodes.ALOAD, 4);
    m.visitVarInsn(Opcodes.ALOAD, 6);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed", "(Ljava/lang/Throwable;)V",
            false);
    m.visitJumpInsn(Opcodes.GOTO, l15);

    m.visitLabel(l26);

    // r2.close
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitMethodInsn(Opcodes.INVOKEINTERFACE, "Fun$Resource2", "close", "()V", false);
    range0.toInclusive = m.instructions.getLast();
    m.visitLabel(l15);

    // if (r1 != null)
    m.visitVarInsn(Opcodes.ALOAD, 1);
    range2.fromInclusive = m.instructions.getLast();
    final Label l23 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l23);
    // if (primaryExc1 != null)
    m.visitVarInsn(Opcodes.ALOAD, 2);
    final Label l27 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l27);
    // r1.close
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource1", "close", "()V", false);
    m.visitJumpInsn(Opcodes.GOTO, l23);

    m.visitVarInsn(Opcodes.ASTORE, 6);
    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitVarInsn(Opcodes.ALOAD, 6);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed", "(Ljava/lang/Throwable;)V",
            false);
    m.visitJumpInsn(Opcodes.GOTO, l23);

    m.visitLabel(l27);
    // r1.close
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource1", "close", "()V", false);
    range2.toInclusive = m.instructions.getLast();
    m.visitLabel(l23);

    // finally
    m.visitInsn(Opcodes.NOP);
    m.visitInsn(Opcodes.ARETURN);

    // catch (Throwable t)
    m.visitLabel(handler1);
    range1.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ASTORE, 5);
    // primaryExc2 = t
    m.visitVarInsn(Opcodes.ALOAD, 5);
    m.visitVarInsn(Opcodes.ASTORE, 4);
    // throw t
    m.visitVarInsn(Opcodes.ALOAD, 5);
    m.visitInsn(Opcodes.ATHROW);

    // catch (any t)
    m.visitVarInsn(Opcodes.ASTORE, 7);
    // if (r2 != null)
    m.visitVarInsn(Opcodes.ALOAD, 3);
    final Label l28 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l28);
    // if (primaryExc2 != null)
    m.visitVarInsn(Opcodes.ALOAD, 4);
    final Label l29 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l29);
    // r2.close
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitMethodInsn(Opcodes.INVOKEINTERFACE, "Fun$Resource2", "close", "()V", false);
    m.visitJumpInsn(Opcodes.GOTO, l28);

    m.visitVarInsn(Opcodes.ASTORE, 8);
    m.visitVarInsn(Opcodes.ALOAD, 4);
    m.visitVarInsn(Opcodes.ALOAD, 8);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed", "(Ljava/lang/Throwable;)V",
            false);
    m.visitJumpInsn(Opcodes.GOTO, l28);

    m.visitLabel(l29);
    // r2.close
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitMethodInsn(Opcodes.INVOKEINTERFACE, "Fun$Resource2", "close", "()V", false);
    m.visitLabel(l28);
    // throw t
    m.visitVarInsn(Opcodes.ALOAD, 7);
    m.visitInsn(Opcodes.ATHROW);
    range1.toInclusive = m.instructions.getLast();

    // catch (Throwable t)
    m.visitLabel(handler2);
    range3.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ASTORE, 3);
    // primaryExc2 = t
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitVarInsn(Opcodes.ASTORE, 2);
    // throw t
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitInsn(Opcodes.ATHROW);

    // catch (any t)
    m.visitVarInsn(Opcodes.ASTORE, 9);
    // if (r1 != null)
    m.visitVarInsn(Opcodes.ALOAD, 1);
    final Label l30 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l30);
    // if (primaryExc1 != null)
    m.visitVarInsn(Opcodes.ALOAD, 2);
    final Label l31 = new Label();
    m.visitJumpInsn(Opcodes.IFNULL, l31);
    // r1.close
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource1", "close", "()V", false);
    m.visitJumpInsn(Opcodes.GOTO, l30);

    m.visitVarInsn(Opcodes.ASTORE, 10);
    m.visitVarInsn(Opcodes.ALOAD, 2);
    m.visitVarInsn(Opcodes.ALOAD, 10);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Throwable", "addSuppressed", "(Ljava/lang/Throwable;)V",
            false);
    m.visitJumpInsn(Opcodes.GOTO, l30);

    m.visitLabel(l31);
    // r1.close
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "Fun$Resource1", "close", "()V", false);
    m.visitLabel(l30);
    // throw t
    m.visitVarInsn(Opcodes.ALOAD, 9);
    m.visitInsn(Opcodes.ATHROW);
    range3.toInclusive = m.instructions.getLast();

    m.visitVarInsn(Opcodes.ASTORE, 11);
    // finally
    m.visitInsn(Opcodes.NOP);
    m.visitVarInsn(Opcodes.ALOAD, 11);
    m.visitInsn(Opcodes.ATHROW);

    filter.filter(m, context, output);

    assertIgnored(range0, range1, range2, range3);
}

From source file:org.jacoco.core.internal.analysis.MethodAnalyzerTest.java

License:Open Source License

private void createIfBranch() {
    method.visitLineNumber(1001, new Label());
    method.visitVarInsn(Opcodes.ILOAD, 1);
    Label l1 = new Label();
    method.visitJumpInsn(Opcodes.IFEQ, l1);
    method.visitLineNumber(1002, new Label());
    method.visitLdcInsn("a");
    method.visitInsn(Opcodes.ARETURN);
    method.visitLabel(l1);/*from   ww w.j  av  a  2 s  . co m*/
    method.visitLineNumber(1003, l1);
    method.visitLdcInsn("b");
    method.visitInsn(Opcodes.ARETURN);
}

From source file:org.jacoco.core.internal.flow.LabelFlowAnalyzer.java

License:Open Source License

@Override
public void visitInsn(final int opcode) {
    switch (opcode) {
    case Opcodes.RET:
        throw new AssertionError("Subroutines not supported.");
    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
    case Opcodes.FRETURN:
    case Opcodes.DRETURN:
    case Opcodes.ARETURN:
    case Opcodes.RETURN:
    case Opcodes.ATHROW:
        successor = false;//  ww  w . ja  va  2  s  . c  o m
        break;
    default:
        successor = true;
        break;
    }
    first = false;
}

From source file:org.jacoco.core.internal.flow.MethodProbesAdapter.java

License:Open Source License

@Override
public void visitInsn(final int opcode) {
    switch (opcode) {
    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
    case Opcodes.FRETURN:
    case Opcodes.DRETURN:
    case Opcodes.ARETURN:
    case Opcodes.RETURN:
    case Opcodes.ATHROW:
        probesVisitor.visitInsnWithProbe(opcode, idGenerator.nextId());
        break;/*from   w  w w.  j  av a  2s .c o m*/
    default:
        probesVisitor.visitInsn(opcode);
        break;
    }
}

From source file:org.jacoco.core.internal.instr.ClassFieldProbeArrayStrategy.java

License:Open Source License

private void createInitMethod(final ClassVisitor cv, final int probeCount) {
    final MethodVisitor mv = cv.visitMethod(InstrSupport.INITMETHOD_ACC, InstrSupport.INITMETHOD_NAME,
            InstrSupport.INITMETHOD_DESC, null, null);
    mv.visitCode();/*from  ww w  .  ja va 2  s . c o m*/

    // Load the value of the static data field:
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC);
    mv.visitInsn(Opcodes.DUP);

    // Stack[1]: [Z
    // Stack[0]: [Z

    // Skip initialization when we already have a data array:
    final Label alreadyInitialized = new Label();
    mv.visitJumpInsn(Opcodes.IFNONNULL, alreadyInitialized);

    // Stack[0]: [Z

    mv.visitInsn(Opcodes.POP);
    final int size = genInitializeDataField(mv, probeCount);

    // Stack[0]: [Z

    // Return the class' probe array:
    if (withFrames) {
        mv.visitFrame(Opcodes.F_NEW, 0, FRAME_LOCALS_EMPTY, 1, FRAME_STACK_ARRZ);
    }
    mv.visitLabel(alreadyInitialized);
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitMaxs(Math.max(size, 2), 0); // Maximum local stack size is 2
    mv.visitEnd();
}

From source file:org.jacoco.core.internal.instr.CondyProbeArrayStrategy.java

License:Open Source License

public void addMembers(final ClassVisitor cv, final int probeCount) {
    final MethodVisitor mv = cv.visitMethod(InstrSupport.INITMETHOD_ACC, InstrSupport.INITMETHOD_NAME, B_DESC,
            null, null);/*from w  w w . j  av  a  2s . com*/
    final int maxStack = accessorGenerator.generateDataAccessor(classId, className, probeCount, mv);
    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(maxStack, 3);
    mv.visitEnd();
}

From source file:org.jacoco.core.internal.instr.FrameTracker.java

License:Open Source License

@Override
public void visitInsn(final int opcode) {
    final Object t1, t2, t3, t4;
    switch (opcode) {
    case Opcodes.NOP:
    case Opcodes.RETURN:
        break;/*  w ww .  j  a va  2 s . c om*/
    case Opcodes.ARETURN:
    case Opcodes.ATHROW:
    case Opcodes.FRETURN:
    case Opcodes.IRETURN:
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT:
    case Opcodes.POP:
        pop(1);
        break;
    case Opcodes.DRETURN:
    case Opcodes.LRETURN:
    case Opcodes.POP2:
        pop(2);
        break;
    case Opcodes.AASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.FASTORE:
    case Opcodes.IASTORE:
    case Opcodes.SASTORE:
        pop(3);
        break;
    case Opcodes.LASTORE:
    case Opcodes.DASTORE:
        pop(4);
        break;
    case Opcodes.ICONST_M1:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
        push(Opcodes.INTEGER);
        break;
    case Opcodes.ARRAYLENGTH:
    case Opcodes.F2I:
    case Opcodes.I2B:
    case Opcodes.I2C:
    case Opcodes.I2S:
    case Opcodes.INEG:
        pop(1);
        push(Opcodes.INTEGER);
        break;
    case Opcodes.BALOAD:
    case Opcodes.CALOAD:
    case Opcodes.D2I:
    case Opcodes.FCMPG:
    case Opcodes.FCMPL:
    case Opcodes.IADD:
    case Opcodes.IALOAD:
    case Opcodes.IAND:
    case Opcodes.IDIV:
    case Opcodes.IMUL:
    case Opcodes.IOR:
    case Opcodes.IREM:
    case Opcodes.ISHL:
    case Opcodes.ISHR:
    case Opcodes.ISUB:
    case Opcodes.IUSHR:
    case Opcodes.IXOR:
    case Opcodes.L2I:
    case Opcodes.SALOAD:
        pop(2);
        push(Opcodes.INTEGER);
        break;
    case Opcodes.DCMPG:
    case Opcodes.DCMPL:
    case Opcodes.LCMP:
        pop(4);
        push(Opcodes.INTEGER);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        push(Opcodes.FLOAT);
        break;
    case Opcodes.FNEG:
    case Opcodes.I2F:
        pop(1);
        push(Opcodes.FLOAT);
        break;
    case Opcodes.D2F:
    case Opcodes.FADD:
    case Opcodes.FALOAD:
    case Opcodes.FDIV:
    case Opcodes.FMUL:
    case Opcodes.FREM:
    case Opcodes.FSUB:
    case Opcodes.L2F:
        pop(2);
        push(Opcodes.FLOAT);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        push(Opcodes.LONG);
        push(Opcodes.TOP);
        break;
    case Opcodes.F2L:
    case Opcodes.I2L:
        pop(1);
        push(Opcodes.LONG);
        push(Opcodes.TOP);
        break;
    case Opcodes.D2L:
    case Opcodes.LALOAD:
    case Opcodes.LNEG:
        pop(2);
        push(Opcodes.LONG);
        push(Opcodes.TOP);
        break;
    case Opcodes.LSHL:
    case Opcodes.LSHR:
    case Opcodes.LUSHR:
        pop(3);
        push(Opcodes.LONG);
        push(Opcodes.TOP);
        break;
    case Opcodes.LADD:
    case Opcodes.LAND:
    case Opcodes.LDIV:
    case Opcodes.LMUL:
    case Opcodes.LOR:
    case Opcodes.LREM:
    case Opcodes.LSUB:
    case Opcodes.LXOR:
        pop(4);
        push(Opcodes.LONG);
        push(Opcodes.TOP);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        push(Opcodes.DOUBLE);
        push(Opcodes.TOP);
        break;
    case Opcodes.F2D:
    case Opcodes.I2D:
        pop(1);
        push(Opcodes.DOUBLE);
        push(Opcodes.TOP);
        break;
    case Opcodes.DALOAD:
    case Opcodes.DNEG:
    case Opcodes.L2D:
        pop(2);
        push(Opcodes.DOUBLE);
        push(Opcodes.TOP);
        break;
    case Opcodes.DADD:
    case Opcodes.DDIV:
    case Opcodes.DMUL:
    case Opcodes.DREM:
    case Opcodes.DSUB:
        pop(4);
        push(Opcodes.DOUBLE);
        push(Opcodes.TOP);
        break;
    case Opcodes.ACONST_NULL:
        push(Opcodes.NULL);
        break;
    case Opcodes.AALOAD:
        pop(1);
        t1 = pop();
        push(Type.getType(((String) t1).substring(1)));
        break;
    case Opcodes.DUP:
        t1 = pop();
        push(t1);
        push(t1);
        break;
    case Opcodes.DUP_X1:
        t1 = pop();
        t2 = pop();
        push(t1);
        push(t2);
        push(t1);
        break;
    case Opcodes.DUP_X2:
        t1 = pop();
        t2 = pop();
        t3 = pop();
        push(t1);
        push(t3);
        push(t2);
        push(t1);
        break;
    case Opcodes.DUP2:
        t1 = pop();
        t2 = pop();
        push(t2);
        push(t1);
        push(t2);
        push(t1);
        break;
    case Opcodes.DUP2_X1:
        t1 = pop();
        t2 = pop();
        t3 = pop();
        push(t2);
        push(t1);
        push(t3);
        push(t2);
        push(t1);
        break;
    case Opcodes.DUP2_X2:
        t1 = pop();
        t2 = pop();
        t3 = pop();
        t4 = pop();
        push(t2);
        push(t1);
        push(t4);
        push(t3);
        push(t2);
        push(t1);
        break;
    case Opcodes.SWAP:
        t1 = pop();
        t2 = pop();
        push(t1);
        push(t2);
        break;
    default:
        throw new IllegalArgumentException();
    }
    mv.visitInsn(opcode);
}

From source file:org.jacoco.core.internal.instr.InterfaceFieldProbeArrayStrategy.java

License:Open Source License

private void createInitMethod(final ClassVisitor cv, final int probeCount) {
    final MethodVisitor mv = cv.visitMethod(InstrSupport.INITMETHOD_ACC, InstrSupport.INITMETHOD_NAME,
            InstrSupport.INITMETHOD_DESC, null, null);
    mv.visitCode();//from ww w  .  ja  v  a2  s.c om

    // Load the value of the static data field:
    mv.visitFieldInsn(Opcodes.GETSTATIC, className, InstrSupport.DATAFIELD_NAME, InstrSupport.DATAFIELD_DESC);
    mv.visitInsn(Opcodes.DUP);

    // Stack[1]: [Z
    // Stack[0]: [Z

    // Skip initialization when we already have a data array:
    final Label alreadyInitialized = new Label();
    mv.visitJumpInsn(Opcodes.IFNONNULL, alreadyInitialized);

    // Stack[0]: [Z

    mv.visitInsn(Opcodes.POP);
    final int size = accessorGenerator.generateDataAccessor(classId, className, probeCount, mv);

    // Stack[0]: [Z

    // Return the class' probe array:
    mv.visitFrame(Opcodes.F_NEW, 0, FRAME_LOCALS_EMPTY, 1, FRAME_STACK_ARRZ);
    mv.visitLabel(alreadyInitialized);
    mv.visitInsn(Opcodes.ARETURN);

    mv.visitMaxs(Math.max(size, 2), 0); // Maximum local stack size is 2
    mv.visitEnd();
}

From source file:org.jacoco.core.runtime.ExecutionDataAccessTest.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();/*from www. j a  v 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();

    // call()
    mv = writer.visitMethod(Opcodes.ACC_PUBLIC, "call", "()Ljava/lang/Object;", null, new String[0]);
    mv.visitCode();
    ExecutionDataAccess.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.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  w  w w  .j  a va2  s.  c  om*/
    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());
}