Example usage for org.objectweb.asm Opcodes POP

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

Introduction

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

Prototype

int POP

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

Click Source Link

Usage

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

private void instrumentPUTXXXFieldAccesses(final ClassNode cn, final String internalClassName,
        final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;

    AbstractInsnNode ins = null;/* ww w  .j a va 2 s .  c o  m*/
    FieldInsnNode fieldIns = null;

    // needed get right receiver var in case of PUTFIELD

    for (int i = 0; i < instructions.size(); i++) {
        ins = instructions.get(i);
        if (ins instanceof FieldInsnNode) {
            fieldIns = (FieldInsnNode) ins;

            /*
             * Is field referencing outermost instance? if yes, ignore it
             * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html
             */
            if (fieldIns.name.endsWith("$0")) {
                continue;
            }

            final int opcode = ins.getOpcode();
            if (opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC) {
                // construction of  
                //   Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams)
                // call
                final InsnList il = new InsnList();

                if (opcode == Opcodes.PUTFIELD) {
                    Type fieldType = Type.getType(fieldIns.desc);
                    if (fieldType.getSize() == 1) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP2));
                        il.add(new InsnNode(Opcodes.POP));
                    } else if (fieldType.getSize() == 2) {
                        InsnList uglyList = new InsnList();
                        // v, w
                        uglyList.add(new InsnNode(Opcodes.DUP2_X1));
                        // w, v, w
                        uglyList.add(new InsnNode(Opcodes.POP2));
                        // w, v
                        uglyList.add(new InsnNode(Opcodes.DUP));
                        // w, v, v
                        uglyList.add(new InsnNode(Opcodes.DUP2_X2));
                        // v, v, w, v, v
                        uglyList.add(new InsnNode(Opcodes.POP2));
                        // v, v, w
                        instructions.insertBefore(fieldIns, uglyList);
                        // PUTFIELD
                        // v
                    }
                } else
                    il.add(new InsnNode(Opcodes.ACONST_NULL));

                il.add(new LdcInsnNode(this.captureId));
                il.add(new LdcInsnNode(fieldIns.owner));
                il.add(new LdcInsnNode(fieldIns.name));
                il.add(new LdcInsnNode(fieldIns.desc));

                il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                        PackageInfo.getNameWithSlash(FieldRegistry.class), "notifyModification",
                        "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"));

                // PUTFIELDRegistry.notifyModification also adds corresponding GETFIELD capture instructions
                this.captureId++;
                i += il.size();

                instructions.insert(fieldIns, il);
                this.captureId++;
            }
        }
    }
}

From source file:org.fabric3.implementation.bytecode.proxy.common.ProxyFactoryImpl.java

License:Open Source License

private void writeReturn(Method method, Label endLabel, MethodVisitor mv) {
    Class<?> returnType = method.getReturnType();

    if (Void.TYPE.equals(returnType)) {
        mv.visitInsn(Opcodes.POP);
        mv.visitLabel(endLabel);/*from w w w . j ava 2  s  .  c  o m*/
        mv.visitInsn(RETURN);
    } else if (returnType.isPrimitive()) {
        if (Double.TYPE.equals(returnType)) {
            mv.visitTypeInsn(CHECKCAST, "java/lang/Double");
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D");
            mv.visitLabel(endLabel);
            mv.visitInsn(Opcodes.DRETURN);
        } else if (Long.TYPE.equals(returnType)) {
            mv.visitTypeInsn(CHECKCAST, "java/lang/Long");
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J");
            mv.visitLabel(endLabel);
            mv.visitInsn(Opcodes.LRETURN);
        } else if (Integer.TYPE.equals(returnType)) {
            mv.visitTypeInsn(CHECKCAST, "java/lang/Integer");
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I");
            mv.visitLabel(endLabel);
            mv.visitInsn(Opcodes.IRETURN);
        } else if (Float.TYPE.equals(returnType)) {
            mv.visitTypeInsn(CHECKCAST, "java/lang/Float");
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F");
            mv.visitLabel(endLabel);
            mv.visitInsn(Opcodes.FRETURN);
        } else if (Short.TYPE.equals(returnType)) {
            mv.visitTypeInsn(CHECKCAST, "java/lang/Short");
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S");
            mv.visitLabel(endLabel);
            mv.visitInsn(Opcodes.IRETURN);
        } else if (Byte.TYPE.equals(returnType)) {
            mv.visitTypeInsn(CHECKCAST, "java/lang/Byte");
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B");
            mv.visitLabel(endLabel);
            mv.visitInsn(Opcodes.IRETURN);
        } else if (Boolean.TYPE.equals(returnType)) {
            mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean");
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
            mv.visitLabel(endLabel);
            mv.visitInsn(Opcodes.IRETURN);
        }
    } else {
        String internalTypeName = Type.getInternalName(returnType);
        mv.visitTypeInsn(CHECKCAST, internalTypeName);
        mv.visitLabel(endLabel);
        mv.visitInsn(ARETURN);
    }
}

From source file:org.formulacompiler.compiler.internal.bytecode.ExpressionCompiler.java

License:Open Source License

protected void compilePop() {
    mv().visitInsn(Opcodes.POP);
}

From source file:org.glassfish.pfl.tf.tools.enhancer.StaticInitVisitor.java

License:Open Source License

@Override
public void visitCode() {
    if (SHORT_FORM) {
        super.visitCode();
        mv.visitLdcInsn(Type.getType("L" + ecd.getClassName() + ";"));
        Type mmrType = Type.getType(MethodMonitorRegistry.class);
        String mdesc = "(Ljava/lang/Class;)V";
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, mmrType.getInternalName(), "registerClass", mdesc);
    } else {//from w ww. j ava  2 s  .c o m
        int line = 1;
        util.info(2, "StaticInitVisitor.visitCode");
        super.visitCode();

        Label start = new Label();
        Label end = new Label();

        mv.visitLabel(start);

        LocalVariableNode thisClass = defineLocal(mv, "thisClass", Class.class, start, end);
        LocalVariableNode mnameList = defineLocal(mv, "mnameList", List.class, start, end);
        LocalVariableNode holderMap = defineLocal(mv, "holderMap", Map.class, start, end);

        generateTraceMsg(mv, "initialize the holders", line++);
        for (String str : ecd.getAnnotationToHolderName().values()) {
            generateTraceMsg(mv, "Generating to initialize holder " + str, line++);
            util.info(2, "Generating code to initialize holder " + str);
            util.newWithSimpleConstructor(mv, SynchronizedHolder.class);
            mv.visitFieldInsn(Opcodes.PUTSTATIC, ecd.getClassName(), str,
                    Type.getDescriptor(SynchronizedHolder.class));
        }

        generateTraceMsg(mv, "Store the Class of this class", line++);
        mv.visitLdcInsn(Type.getType("L" + ecd.getClassName() + ";"));
        mv.visitVarInsn(Opcodes.ASTORE, thisClass.index);

        generateTraceMsg(mv, "Create list of method names", line++);
        util.newWithSimpleConstructor(mv, ArrayList.class);
        mv.visitVarInsn(Opcodes.ASTORE, mnameList.index);

        for (String str : ecd.getMethodNames()) {
            util.info(2, "Generating code to add " + str + " to methodNames");
            mv.visitVarInsn(Opcodes.ALOAD, mnameList.index);
            mv.visitLdcInsn(str);
            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z");
            mv.visitInsn(Opcodes.POP);
        }

        generateTraceMsg(mv, "create map from MM annotation class to Holder and init", line++);
        util.newWithSimpleConstructor(mv, HashMap.class);
        mv.visitVarInsn(Opcodes.ASTORE, holderMap.index);

        for (Map.Entry<String, String> entry : ecd.getAnnotationToHolderName().entrySet()) {

            util.info(2,
                    "Generating code to put " + entry.getKey() + "=>" + entry.getValue() + " into holderMap");

            mv.visitVarInsn(Opcodes.ALOAD, holderMap.index);

            Type annoType = Type.getType("L" + entry.getKey() + ";");
            mv.visitLdcInsn(annoType);

            mv.visitFieldInsn(Opcodes.GETSTATIC, ecd.getClassName(), entry.getValue(),
                    Type.getDescriptor(SynchronizedHolder.class));

            mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put",
                    "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");

            mv.visitInsn(Opcodes.POP);
        }

        generateTraceMsg(mv, "register with MethodMonitorRegistry", line++);
        util.info(2, "Generating code call MethodMonitorRegistry.registerClass");
        mv.visitVarInsn(Opcodes.ALOAD, thisClass.index);
        mv.visitVarInsn(Opcodes.ALOAD, mnameList.index);
        mv.visitVarInsn(Opcodes.ALOAD, holderMap.index);

        Type mmrType = Type.getType(MethodMonitorRegistry.class);
        String mdesc = "(Ljava/lang/Class;Ljava/util/List;Ljava/util/Map;)V";
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, mmrType.getInternalName(), "registerClass", mdesc);

        mv.visitLabel(end);

        thisClass.accept(mv);
        mnameList.accept(mv);
        holderMap.accept(mv);
    }
}

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

License:Open Source License

@Test
public void ecj_try_catch_finally() {
    final Label tryStart = new Label();
    final Label tryEnd = new Label();
    final Label catchStart = new Label();
    final Label catchEnd = new Label();
    final Label finallyStart = new Label();
    final Label finallyEnd = new Label();
    final Label after = new Label();

    m.visitTryCatchBlock(tryStart, tryEnd, catchStart, "java/lang/Exception");
    m.visitTryCatchBlock(tryStart, catchEnd, finallyStart, null);

    m.visitLabel(tryStart);//from   w  ww  . j  a v  a2  s . c o  m
    m.visitInsn(Opcodes.NOP); // try body
    m.visitLabel(tryEnd);
    m.visitJumpInsn(Opcodes.GOTO, finallyEnd);

    m.visitLabel(catchStart);
    m.visitInsn(Opcodes.POP);
    m.visitInsn(Opcodes.NOP); // catch body
    m.visitLabel(catchEnd);
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitJumpInsn(Opcodes.GOTO, after);
    shouldIgnoreLast();

    m.visitLabel(finallyStart);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitVarInsn(Opcodes.ALOAD, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.ATHROW);
    shouldIgnoreLast();
    m.visitLabel(finallyEnd);

    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitLabel(after);
    m.visitInsn(Opcodes.NOP);

    execute();
}

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

License:Open Source License

@Test
public void ecj_empty_catch() {
    final Label tryStart = new Label();
    final Label tryEnd = new Label();
    final Label catchStart = new Label();
    final Label catchEnd = new Label();
    final Label finallyStart = new Label();
    final Label finallyEnd = new Label();
    final Label after = new Label();

    m.visitTryCatchBlock(tryStart, tryEnd, catchStart, "java/lang/Exception");
    m.visitTryCatchBlock(tryStart, catchEnd, finallyStart, null);

    m.visitLabel(tryStart);//  www .  j av a 2 s  .co  m
    m.visitInsn(Opcodes.NOP); // try body
    m.visitLabel(tryEnd);
    m.visitJumpInsn(Opcodes.GOTO, finallyEnd);

    m.visitLabel(catchStart);
    m.visitInsn(Opcodes.POP);
    m.visitLabel(catchEnd);
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitJumpInsn(Opcodes.GOTO, after);
    shouldIgnoreLast();

    m.visitLabel(finallyStart);
    m.visitVarInsn(Opcodes.ASTORE, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitVarInsn(Opcodes.ALOAD, 1);
    shouldIgnoreLast();
    m.visitInsn(Opcodes.ATHROW);
    shouldIgnoreLast();
    m.visitLabel(finallyEnd);

    m.visitInsn(Opcodes.NOP); // finally body
    shouldMergeLast();
    m.visitLabel(after);
    m.visitInsn(Opcodes.NOP);

    execute();
}

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

License:Open Source License

@Test
public void ecj_always_completes_abruptly() {
    final Label tryStart = new Label();
    final Label tryEnd = new Label();
    final Label finallyStart = new Label();

    m.visitTryCatchBlock(tryStart, tryEnd, tryEnd, null);

    m.visitLabel(tryStart);/*from  w  w  w.  ja  v a  2  s.  c  o  m*/
    m.visitInsn(Opcodes.NOP); // try body
    m.visitJumpInsn(Opcodes.GOTO, finallyStart);
    m.visitLabel(tryEnd);

    m.visitInsn(Opcodes.POP);
    m.visitLabel(finallyStart);
    m.visitInsn(Opcodes.RETURN); // finally body

    execute();
}

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

License:Open Source License

/**
 * <pre>//from   w w w .jav  a2  s  . c om
 *     runBlocking {
 *         val x = 42
 *         nop(x)
 *         suspendingFunction()
 *         nop(x)
 *     }
 * </pre>
 */
@Test
public void should_filter_suspending_lambdas() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, 0, "invokeSuspend",
            "(Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);

    m.visitLabel(new Label());
    final Range range1 = new Range();
    range1.fromInclusive = m.instructions.getLast();
    m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/coroutines/intrinsics/IntrinsicsKt",
            "getCOROUTINE_SUSPENDED", "()Ljava/lang/Object;", false);
    m.visitVarInsn(Opcodes.ASTORE, 4);

    m.visitVarInsn(Opcodes.ALOAD, 0);
    // line of "runBlocking"
    m.visitFieldInsn(Opcodes.GETFIELD, "Target", "label", "I");
    final Label dflt = new Label();
    final Label state0 = new Label();
    final Label state1 = new Label();
    m.visitTableSwitchInsn(0, 1, dflt, state0, state1);

    m.visitLabel(state0);

    {
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitInsn(Opcodes.DUP);
        m.visitTypeInsn(Opcodes.INSTANCEOF, "kotlin/Result$Failure");
        Label label = new Label();
        m.visitJumpInsn(Opcodes.IFEQ, label);
        m.visitTypeInsn(Opcodes.CHECKCAST, "kotlin/Result$Failure");
        m.visitFieldInsn(Opcodes.GETFIELD, "kotlin/Result$Failure", "exception", "Ljava/lang/Throwable");
        m.visitInsn(Opcodes.ATHROW);
        m.visitInsn(Opcodes.POP);
        range1.toInclusive = m.instructions.getLast();
        m.visitLabel(label);
    }

    // line before "suspendingFunction"
    m.visitInsn(Opcodes.NOP);

    // line of "suspendingFunction"
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "", "suspendingFunction",
            "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", false);

    m.visitInsn(Opcodes.DUP);
    final Range range2 = new Range();
    range2.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ALOAD, 4);
    final Label continuationLabelAfterLoadedResult = new Label();
    m.visitJumpInsn(Opcodes.IF_ACMPNE, continuationLabelAfterLoadedResult);
    // line of "runBlocking"
    m.visitVarInsn(Opcodes.ALOAD, 4);
    m.visitInsn(Opcodes.ARETURN);

    m.visitLabel(state1);

    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitFieldInsn(Opcodes.GETFIELD, "Target", "I$0", "I");
    m.visitVarInsn(Opcodes.ISTORE, 3);

    {
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitInsn(Opcodes.DUP);
        m.visitTypeInsn(Opcodes.INSTANCEOF, "kotlin/Result$Failure");
        final Label label = new Label();
        m.visitJumpInsn(Opcodes.IFEQ, label);
        m.visitTypeInsn(Opcodes.CHECKCAST, "kotlin/Result$Failure");
        m.visitFieldInsn(Opcodes.GETFIELD, "kotlin/Result$Failure", "exception", "Ljava/lang/Throwable");
        m.visitInsn(Opcodes.ATHROW);
        m.visitInsn(Opcodes.POP);
        m.visitLabel(label);
    }
    m.visitVarInsn(Opcodes.ALOAD, 1);
    range2.toInclusive = m.instructions.getLast();
    m.visitLabel(continuationLabelAfterLoadedResult);

    // line after "suspendingFunction"
    m.visitInsn(Opcodes.NOP);
    m.visitInsn(Opcodes.ARETURN);

    m.visitLabel(dflt);
    final Range range0 = new Range();
    range0.fromInclusive = m.instructions.getLast();
    m.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalStateException");
    m.visitInsn(Opcodes.DUP);
    m.visitLdcInsn("call to 'resume' before 'invoke' with coroutine");
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalStateException", "<init>",
            "(Ljava/lang/String;)V", false);
    m.visitInsn(Opcodes.ATHROW);
    range0.toInclusive = m.instructions.getLast();

    filter.filter(m, context, output);

    assertIgnored(range0, range1, range2);
}

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

License:Open Source License

/**
 * <pre>/* w  ww.ja  va 2s .c o  m*/
 *     suspend fun example() {
 *         suspendingFunction()
 *         nop()
 *     }
 * </pre>
 */
@Test
public void should_filter_suspending_functions() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_STATIC, "example",
            "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", null, null);
    context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);

    final int continuationArgumentIndex = 0;
    final int continuationIndex = 2;

    m.visitVarInsn(Opcodes.ALOAD, continuationArgumentIndex);
    final Range range1 = new Range();
    range1.fromInclusive = m.instructions.getLast();
    m.visitTypeInsn(Opcodes.INSTANCEOF, "ExampleKt$example$1");
    final Label createStateInstance = new Label();
    m.visitJumpInsn(Opcodes.IFEQ, createStateInstance);

    m.visitVarInsn(Opcodes.ALOAD, continuationArgumentIndex);
    m.visitTypeInsn(Opcodes.CHECKCAST, "ExampleKt$example$1");
    m.visitVarInsn(Opcodes.ASTORE, continuationIndex);

    m.visitVarInsn(Opcodes.ALOAD, continuationIndex);
    m.visitFieldInsn(Opcodes.GETFIELD, "ExampleKt$example$1", "label", "I");

    m.visitLdcInsn(Integer.valueOf(Integer.MIN_VALUE));
    m.visitInsn(Opcodes.IAND);
    m.visitJumpInsn(Opcodes.IFEQ, createStateInstance);

    m.visitVarInsn(Opcodes.ALOAD, continuationIndex);
    m.visitInsn(Opcodes.DUP);
    m.visitFieldInsn(Opcodes.GETFIELD, "ExampleKt$example$1", "label", "I");

    m.visitLdcInsn(Integer.valueOf(Integer.MIN_VALUE));
    m.visitInsn(Opcodes.ISUB);
    m.visitFieldInsn(Opcodes.PUTFIELD, "ExampleKt$example$1", "label", "I");

    final Label afterCoroutineStateCreated = new Label();
    m.visitJumpInsn(Opcodes.GOTO, afterCoroutineStateCreated);

    m.visitLabel(createStateInstance);

    m.visitTypeInsn(Opcodes.NEW, "ExampleKt$example$1");
    m.visitInsn(Opcodes.DUP);
    m.visitVarInsn(Opcodes.ALOAD, continuationArgumentIndex);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "ExampleKt$example$1", "<init>",
            "(Lkotlin/coroutines/Continuation;)V", false);

    m.visitVarInsn(Opcodes.ASTORE, continuationIndex);

    m.visitLabel(afterCoroutineStateCreated);

    m.visitVarInsn(Opcodes.ALOAD, continuationIndex);
    m.visitFieldInsn(Opcodes.GETFIELD, "ExampleKt$example$1", "result", "Ljava/lang/Object;");
    m.visitVarInsn(Opcodes.ASTORE, 1);

    m.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/coroutines/intrinsics/IntrinsicsKt",
            "getCOROUTINE_SUSPENDED", "()Ljava/lang/Object;", false);

    // line of "fun"
    m.visitVarInsn(Opcodes.ASTORE, 3);

    m.visitVarInsn(Opcodes.ALOAD, continuationIndex);
    m.visitFieldInsn(Opcodes.GETFIELD, "ExampleKt$example$1", "label", "I");
    final Label dflt = new Label();
    final Label state0 = new Label();
    final Label state1 = new Label();
    m.visitTableSwitchInsn(0, 1, dflt, state0, state1);

    m.visitLabel(state0);

    {
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitInsn(Opcodes.DUP);
        m.visitTypeInsn(Opcodes.INSTANCEOF, "kotlin/Result$Failure");
        Label label = new Label();
        m.visitJumpInsn(Opcodes.IFEQ, label);
        m.visitTypeInsn(Opcodes.CHECKCAST, "kotlin/Result$Failure");
        m.visitFieldInsn(Opcodes.GETFIELD, "kotlin/Result$Failure", "exception", "Ljava/lang/Throwable");
        m.visitInsn(Opcodes.ATHROW);
        m.visitInsn(Opcodes.POP);
        range1.toInclusive = m.instructions.getLast();
        m.visitLabel(label);
    }

    // line of "suspendingFunction"
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "", "suspendingFunction",
            "(Lkotlin/coroutines/Continuation;)Ljava/lang/Object;", false);

    m.visitInsn(Opcodes.DUP);
    final Range range2 = new Range();
    range2.fromInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ALOAD, 3);
    final Label continuationLabelAfterLoadedResult = new Label();
    m.visitJumpInsn(Opcodes.IF_ACMPNE, continuationLabelAfterLoadedResult);
    // line of "fun"
    m.visitVarInsn(Opcodes.ALOAD, 3);
    m.visitInsn(Opcodes.ARETURN);

    m.visitLabel(state1);

    {
        m.visitVarInsn(Opcodes.ALOAD, 1);
        m.visitInsn(Opcodes.DUP);
        m.visitTypeInsn(Opcodes.INSTANCEOF, "kotlin/Result$Failure");
        final Label label = new Label();
        m.visitJumpInsn(Opcodes.IFEQ, label);
        m.visitTypeInsn(Opcodes.CHECKCAST, "kotlin/Result$Failure");
        m.visitFieldInsn(Opcodes.GETFIELD, "kotlin/Result$Failure", "exception", "Ljava/lang/Throwable");
        m.visitInsn(Opcodes.ATHROW);
        m.visitInsn(Opcodes.POP);
        m.visitLabel(label);
    }
    m.visitVarInsn(Opcodes.ALOAD, 1);
    range2.toInclusive = m.instructions.getLast();
    m.visitLabel(continuationLabelAfterLoadedResult);

    // line after "suspendingFunction"
    m.visitInsn(Opcodes.NOP);
    m.visitInsn(Opcodes.ARETURN);

    m.visitLabel(dflt);
    final Range range0 = new Range();
    range0.fromInclusive = m.instructions.getLast();
    m.visitTypeInsn(Opcodes.NEW, "java/lang/IllegalStateException");
    m.visitInsn(Opcodes.DUP);
    m.visitLdcInsn("call to 'resume' before 'invoke' with coroutine");
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/IllegalStateException", "<init>",
            "(Ljava/lang/String;)V", false);
    m.visitInsn(Opcodes.ATHROW);
    range0.toInclusive = m.instructions.getLast();

    filter.filter(m, context, output);

    assertIgnored(range0, range1, range2);
}

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  w  ww .jav a2s  .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();
}