Example usage for org.objectweb.asm.tree MethodNode visitJumpInsn

List of usage examples for org.objectweb.asm.tree MethodNode visitJumpInsn

Introduction

In this page you can find the example usage for org.objectweb.asm.tree MethodNode visitJumpInsn.

Prototype

@Override
    public void visitJumpInsn(final int opcode, final Label label) 

Source Link

Usage

From source file:clientapi.load.transform.impl.ValueAccessorTransformer.java

License:Apache License

/**
 * Creates the field-getter getter method in the specified {@code ClassNode}
 *
 * @param cn The ClassNode//w  ww.  j  a v a 2  s.  c  o  m
 */
private void createFieldGetter(ClassNode cn) {
    MethodNode mn = new MethodNode(ACC_PUBLIC | ACC_FINAL, "getFieldGetter",
            "(Ljava/lang/String;)Ljava/util/function/Supplier;", null, null);

    // Create a check for all labeled fields in the cache
    fieldCache.forEach((id, fn) -> {
        MethodNode handle;
        {
            // Create lambda handle method
            handle = new MethodNode(ACC_PRIVATE | ACC_SYNTHETIC, "lambda$getFieldGetter$" + current++,
                    "()Ljava/lang/Object;", null, null);

            // Get the field value
            handle.visitVarInsn(ALOAD, 0);
            handle.visitFieldInsn(GETFIELD, cn.name, fn.name, fn.desc);

            // If the field is a primitive type, get the object representation
            String object = getObject(fn.desc);
            if (!object.equals(fn.desc))
                handle.visitMethodInsn(INVOKESTATIC, object, "valueOf", "(" + fn.desc + ")L" + object + ";",
                        false);

            // Return the value
            handle.visitInsn(ARETURN);

            // Add the handle method to the class
            cn.methods.add(handle);
        }

        // Create label for IF statement jump
        Label skip = new Label();

        // Compare the target value with the expected value
        mn.visitVarInsn(ALOAD, 1);
        mn.visitLdcInsn(id);
        mn.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);

        // Jump if the input doesn't match the expected value
        mn.visitJumpInsn(IFEQ, skip);

        // Return the getter
        mn.visitVarInsn(ALOAD, 0);
        mn.visitInvokeDynamicInsn("get", "(L" + cn.name + ";)Ljava/util/function/Supplier;",
                // Define the bootstrap method
                METAFACTORY,
                // Fill the remaining 3 args for the method
                Type.getMethodType("()Ljava/lang/Object;"), createMethodHandle(H_INVOKESPECIAL, cn, handle),
                Type.getMethodType("()Ljava/lang/Object;"));
        mn.visitInsn(ARETURN);

        // Indicate where the IF statement should jump to if it fails
        mn.visitLabel(skip);
    });
    mn.visitInsn(ACONST_NULL);
    mn.visitInsn(ARETURN);

    cn.methods.add(mn);
}

From source file:clientapi.load.transform.impl.ValueAccessorTransformer.java

License:Apache License

/**
 * Creates the field-setter getter method in the specified {@code ClassNode}
 *
 * @see ValueAccessor/* w w w. ja v a  2  s.  c om*/
 *
 * @param cn The ClassNode
 */
private void createFieldSetter(ClassNode cn) {
    MethodNode mn = new MethodNode(ACC_PUBLIC | ACC_FINAL, "getFieldSetter",
            "(Ljava/lang/String;)Ljava/util/function/Consumer;", null, null);

    // Create a check for all labeled fields in the cache
    fieldCache.forEach((id, fn) -> {
        MethodNode handle;
        {
            // Create lambda handle method
            handle = new MethodNode(ACC_PRIVATE | ACC_SYNTHETIC, "lambda$getFieldSetter$" + current++,
                    "(Ljava/lang/Object;)V", null, null);

            handle.visitVarInsn(ALOAD, 0);
            handle.visitVarInsn(ALOAD, 1);
            handle.visitTypeInsn(CHECKCAST, getStrippedDesc(getObject(fn.desc)));

            // If the field is a primitive type, get the primitive value
            String object = getObject(fn.desc);
            if (!object.equals(fn.desc))
                handle.visitMethodInsn(INVOKEVIRTUAL, object, getClassName(fn.desc) + "Value", "()" + fn.desc,
                        false);

            // Set the field value
            handle.visitFieldInsn(PUTFIELD, cn.name, fn.name, fn.desc);
            handle.visitInsn(RETURN);

            // Add the handle method to the class
            cn.methods.add(handle);
        }

        // Create label for IF statement jump
        Label skip = new Label();

        // Compare the target value with the expected value
        mn.visitVarInsn(ALOAD, 1);
        mn.visitLdcInsn(id);
        mn.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);

        // Jump if the input doesn't match the expected value
        mn.visitJumpInsn(IFEQ, skip);

        // Return the setter
        mn.visitVarInsn(ALOAD, 0);
        mn.visitInvokeDynamicInsn("accept", "(L" + cn.name + ";)Ljava/util/function/Consumer;",
                // Define the bootstrap method
                METAFACTORY,
                // Fill the remaining 3 args for the method
                Type.getMethodType("(Ljava/lang/Object;)V"), createMethodHandle(H_INVOKESPECIAL, cn, handle),
                Type.getMethodType("(Ljava/lang/Object;)V"));
        mn.visitInsn(ARETURN);

        // Indicate where the IF statement should jump to if it fails
        mn.visitLabel(skip);
    });
    mn.visitInsn(ACONST_NULL);
    mn.visitInsn(ARETURN);

    cn.methods.add(mn);
}

From source file:com.seovic.pof.PortableTypeGenerator.java

License:Apache License

private void implementReadExternal() {
    int index = 0;

    MethodNode mn = new MethodNode(ACC_PRIVATE, "readExternal", "(Lcom/tangosol/io/pof/PofReader;)V", null,
            new String[] { "java/io/IOException" });
    mn.visitCode();/*from w  w  w  .  j a  va  2 s  .  c o m*/

    for (int version : properties.keySet()) {
        mn.visitVarInsn(ALOAD, 1);
        mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader", "getVersionId", "()I");
        mn.visitIntInsn(BIPUSH, version);
        Label l = new Label();
        mn.visitJumpInsn(IF_ICMPLT, l);

        SortedSet<FieldNode> fields = properties.get(version);
        for (FieldNode fn : fields) {
            Type type = Type.getType(fn.desc);

            if (isDebugEnabled()) {
                mn.visitLdcInsn("reading attribute " + index + " (" + fn.name + ") from POF stream");
                mn.visitIntInsn(BIPUSH, 7);
                mn.visitMethodInsn(INVOKESTATIC, "com/tangosol/net/CacheFactory", "log",
                        "(Ljava/lang/String;I)V");
            }
            mn.visitVarInsn(ALOAD, 0);
            mn.visitVarInsn(ALOAD, 1);
            mn.visitIntInsn(BIPUSH, index++);

            ReadMethod readMethod = getReadMethod(fn, type);
            readMethod.createTemplate(mn, fn, type);
            mn.visitMethodInsn(INVOKEINTERFACE, "com/tangosol/io/pof/PofReader", readMethod.getName(),
                    readMethod.getDescriptor());
            if (type.getSort() == Type.OBJECT || "readObjectArray".equals(readMethod.getName())) {
                mn.visitTypeInsn(CHECKCAST, type.getInternalName());
            }
            mn.visitFieldInsn(PUTFIELD, cn.name, fn.name, fn.desc);
        }

        mn.visitLabel(l);
        mn.visitFrame(F_SAME, 0, null, 0, null);
    }

    mn.visitInsn(RETURN);
    mn.visitMaxs(0, 0);
    mn.visitEnd();

    if (!hasMethod(mn)) {
        cn.methods.add(mn);
    }
    LOG.debug("Implemented method: " + mn.name);
}

From source file:de.sanandrew.core.manpack.transformer.TransformHorseArmor.java

License:Creative Commons License

private static MethodNode injectMethodSetCustomArmorItem() {
    MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PRIVATE, ASMNames.MD_SAP_SET_CUSTOM_ARMOR_ITEM);
    method.visitCode();// w ww. j  a va  2s. c  o  m
    Label l0 = new Label();
    method.visitLabel(l0);
    method.visitVarInsn(Opcodes.ALOAD, 1);
    Label l1 = new Label();
    method.visitJumpInsn(Opcodes.IFNONNULL, l1);
    method.visitTypeInsn(Opcodes.NEW, ASMNames.CL_ITEM_STACK);
    method.visitInsn(Opcodes.DUP);
    ASMHelper.visitFieldInsn(method, Opcodes.GETSTATIC, ASMNames.FD_ITEMS_IRON_SHOVEL);
    method.visitInsn(Opcodes.ICONST_0);
    ASMHelper.visitMethodInsn(method, Opcodes.INVOKESPECIAL, ASMNames.MD_ITEMSTACK_INIT, false);
    method.visitVarInsn(Opcodes.ASTORE, 1);
    method.visitLabel(l1);
    method.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    ASMHelper.visitFieldInsn(method, Opcodes.GETFIELD, ASMNames.FD_HORSE_DATAWATCHER);
    method.visitIntInsn(Opcodes.BIPUSH, 23);
    method.visitVarInsn(Opcodes.ALOAD, 1);
    ASMHelper.visitMethodInsn(method, Opcodes.INVOKEVIRTUAL, ASMNames.MD_DATAWATCHER_UPDATE_OBJ, false);
    Label l3 = new Label();
    method.visitLabel(l3);
    method.visitInsn(Opcodes.RETURN);
    Label l4 = new Label();
    method.visitLabel(l4);
    method.visitLocalVariable("this", ASMNames.CL_T_ENTITY_HORSE, null, l0, l3, 0);
    method.visitLocalVariable("stack", ASMNames.CL_T_ITEM_STACK, null, l0, l3, 1);
    method.visitMaxs(5, 2);
    method.visitEnd();

    return method;
}

From source file:naftoreiclag.dontdigleft.transformer.ResizePlayerBoudingBox.java

License:Open Source License

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (name.equals("net.minecraft.entity.player.EntityPlayer") || name.equals("xl")) {
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(basicClass);

        classReader.accept(classNode, 0);

        MethodNode mv = new MethodNode(ASM4, ACC_PROTECTED, "setSize", "(FF)V", null, null);

        mv.visitCode();//from www.  j ava 2s  . c  om
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(FLOAD, 1);
        mv.visitLdcInsn(new Float("0.6"));
        mv.visitInsn(FCMPL);
        Label l1 = new Label();
        mv.visitJumpInsn(IFNE, l1);
        mv.visitVarInsn(FLOAD, 2);
        mv.visitLdcInsn(new Float("1.8"));
        mv.visitInsn(FCMPL);
        mv.visitJumpInsn(IFNE, l1);
        Label l2 = new Label();
        mv.visitLabel(l2);
        mv.visitLdcInsn(new Float("0.6"));
        mv.visitVarInsn(FSTORE, 1);
        Label l3 = new Label();
        mv.visitLabel(l3);
        mv.visitLdcInsn(new Float("0.6"));
        mv.visitVarInsn(FSTORE, 2);
        mv.visitLabel(l1);
        mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(FLOAD, 1);
        mv.visitVarInsn(FLOAD, 2);
        mv.visitMethodInsn(INVOKESPECIAL, "net/minecraft/entity/Entity", "setSize", "(FF)V");
        Label l4 = new Label();
        mv.visitLabel(l4);
        mv.visitInsn(RETURN);
        Label l5 = new Label();
        mv.visitLabel(l5);
        mv.visitLocalVariable("this", "Lnet/minecraft/entity/player/EntityPlayer;", null, l0, l5, 0);
        mv.visitLocalVariable("par1", "F", null, l0, l5, 1);
        mv.visitLocalVariable("par2", "F", null, l0, l5, 2);
        mv.visitMaxs(3, 3);
        mv.visitEnd();

        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classNode.accept(cw);
        mv.accept(cw);

        return cw.toByteArray();
    } else {
        return basicClass;
    }
}

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

License:Open Source License

@Test
public void should_filter_suspending_lambdas_generated_by_Kotlin_1_3_30() {
    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);//from w w w  .ja v  a 2 s  .com

    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.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/ResultKt", "throwOnFailure", "(Ljava/lang/Object;)V",
                false);
        range1.toInclusive = m.instructions.getLast();
    }

    // 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.visitMethodInsn(Opcodes.INVOKESTATIC, "kotlin/ResultKt", "throwOnFailure", "(Ljava/lang/Object;)V",
                false);
    }
    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>//from   ww  w  .  j a  va 2 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>//from   w  w  w .j  a  v  a 2  s. 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.analysis.filter.KotlinDefaultArgumentsFilterTest.java

License:Open Source License

private static MethodNode createMethod(final int access, final String name, final String descriptor) {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, access, name, descriptor, null, null);

    m.visitVarInsn(Opcodes.ILOAD, 2);//from   w  w w  .ja v  a  2 s  .  co m
    m.visitInsn(Opcodes.ICONST_1);
    m.visitInsn(Opcodes.IAND);
    final Label label = new Label();
    m.visitJumpInsn(Opcodes.IFEQ, label);
    // default argument
    m.visitLdcInsn(Integer.valueOf(42));
    m.visitVarInsn(Opcodes.ISTORE, 1);
    m.visitLabel(label);

    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitVarInsn(Opcodes.ILOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "Target", "origin", "(I)V", false);
    m.visitInsn(Opcodes.RETURN);

    return m;
}

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

License:Open Source License

/**
 * <pre>/*  w w w  .  ja  va2 s . co  m*/
 * open class Open {
 *     open fun foo(a: Int = 42) {
 *     }
 * }
 * </pre>
 */
@Test
public void should_filter_open_functions() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_SYNTHETIC, "foo$default",
            "(LOpen;IILjava/lang/Object;)V", null, null);
    context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);
    {
        m.visitVarInsn(Opcodes.ALOAD, 3);
        final Label label = new Label();
        m.visitJumpInsn(Opcodes.IFNULL, label);
        m.visitTypeInsn(Opcodes.NEW, "java/lang/UnsupportedOperationException");
        m.visitInsn(Opcodes.DUP);
        m.visitLdcInsn("Super calls with default arguments not supported in this target, function: foo");
        m.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>",
                "(Ljava/lang/String;)V", false);
        m.visitInsn(Opcodes.ATHROW);
        m.visitLabel(label);
    }
    {
        m.visitVarInsn(Opcodes.ILOAD, 2);
        m.visitInsn(Opcodes.ICONST_1);
        m.visitInsn(Opcodes.IAND);
        final Label label = new Label();
        m.visitJumpInsn(Opcodes.IFEQ, label);
        // default argument
        m.visitLdcInsn(Integer.valueOf(42));
        m.visitVarInsn(Opcodes.ISTORE, 1);
        m.visitLabel(label);

        m.visitVarInsn(Opcodes.ALOAD, 0);
        m.visitVarInsn(Opcodes.ILOAD, 1);
        m.visitMethodInsn(Opcodes.INVOKESPECIAL, "Open", "foo", "(I)V", false);
        m.visitInsn(Opcodes.RETURN);
    }

    filter.filter(m, context, output);

    assertIgnored(new Range(m.instructions.getFirst(), m.instructions.get(6)),
            new Range(m.instructions.get(11), m.instructions.get(11)));
}