Example usage for org.objectweb.asm Opcodes ICONST_1

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

Introduction

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

Prototype

int ICONST_1

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

Click Source Link

Usage

From source file:org.gradle.model.internal.manage.schema.extract.ManagedProxyClassGenerator.java

License:Apache License

private void writeConfigureMethod(ClassVisitor visitor, Type generatedType, ModelProperty<?> property,
        boolean writable) {
    if (!writable && property.getSchema() instanceof CompositeSchema) {
        // Adds a void $propName(Closure<?> cl) method that delegates to model state

        MethodVisitor methodVisitor = declareMethod(visitor, property.getName(),
                Type.getMethodDescriptor(Type.VOID_TYPE, CLOSURE_TYPE), null);
        putNodeStateFieldValueOnStack(methodVisitor, generatedType);
        putConstantOnStack(methodVisitor, property.getName());
        putFirstMethodArgumentOnStack(methodVisitor);
        methodVisitor.visitMethodInsn(INVOKEINTERFACE, MODEL_ELEMENT_STATE_TYPE_INTERNAL_NAME, "apply",
                STATE_APPLY_METHOD_DESCRIPTOR, true);
        finishVisitingMethod(methodVisitor);
        return;//from   w  ww .ja v a 2s.  c o  m
    }
    if (!writable && property.getSchema() instanceof UnmanagedImplStructSchema) {
        UnmanagedImplStructSchema<?> structSchema = (UnmanagedImplStructSchema<?>) property.getSchema();
        if (!structSchema.isAnnotated()) {
            return;
        }

        // Adds a void $propName(Closure<?> cl) method that executes the closure
        MethodVisitor methodVisitor = declareMethod(visitor, property.getName(),
                Type.getMethodDescriptor(Type.VOID_TYPE, CLOSURE_TYPE), null);
        putThisOnStack(methodVisitor);
        methodVisitor.visitMethodInsn(INVOKEVIRTUAL, generatedType.getInternalName(),
                property.getGetter().getName(),
                Type.getMethodDescriptor(Type.getType(property.getType().getConcreteClass())), false);
        putFirstMethodArgumentOnStack(methodVisitor);
        methodVisitor.visitMethodInsn(INVOKESTATIC, Type.getInternalName(ClosureBackedAction.class), "execute",
                Type.getMethodDescriptor(Type.VOID_TYPE, OBJECT_TYPE, CLOSURE_TYPE), false);
        finishVisitingMethod(methodVisitor);
        return;
    }

    // Adds a void $propName(Closure<?> cl) method that throws MME, to avoid attempts to convert closure to something else
    MethodVisitor methodVisitor = declareMethod(visitor, property.getName(),
            Type.getMethodDescriptor(Type.VOID_TYPE, CLOSURE_TYPE), null);
    putThisOnStack(methodVisitor);
    putConstantOnStack(methodVisitor, property.getName());
    methodVisitor.visitInsn(Opcodes.ICONST_1);
    methodVisitor.visitTypeInsn(Opcodes.ANEWARRAY, OBJECT_TYPE.getInternalName());
    methodVisitor.visitInsn(Opcodes.DUP);
    methodVisitor.visitInsn(Opcodes.ICONST_0);
    putFirstMethodArgumentOnStack(methodVisitor);
    methodVisitor.visitInsn(Opcodes.AASTORE);
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, generatedType.getInternalName(), "methodMissing",
            METHOD_MISSING_METHOD_DESCRIPTOR, false);
    finishVisitingMethod(methodVisitor);
}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

private void handleNumberOperator(ASTNode node, Operator op, Type type) throws ASTVisitorException {
    if (op.equals(Operator.PLUS)) {

        // FIXME: IADD or DADD, etc.
        //        use type.getOpcode(Opcodes.IADD) to avoid if-then
        mn.instructions.add(new InsnNode(type.getOpcode(Opcodes.IADD)));

    } else if (op.equals(Operator.MINUS)) {

        // FIXME: ISUB or DSUB, etc.
        //        use type.getOpcode() to avoid if-then
        mn.instructions.add(new InsnNode(type.getOpcode(Opcodes.ISUB)));

    } else if (op.equals(Operator.MULTIPLY)) {

        // FIXME: IMUL or DMUL, etc.
        //        use type.getOpcode() to avoid if-then
        mn.instructions.add(new InsnNode(type.getOpcode(Opcodes.IMUL)));

    } else if (op.equals(Operator.DIVISION)) {

        // FIXME: IDIV or DDIV, etc.
        //        use type.getOpcode() to avoid if-then
        mn.instructions.add(new InsnNode(type.getOpcode(Opcodes.IDIV)));

    } else if (op.isRelational()) {
        if (type.equals(Type.DOUBLE_TYPE)) {
            mn.instructions.add(new InsnNode(Opcodes.DCMPG));
            JumpInsnNode jmp = null;//from  w  w  w  .  ja va 2 s .c  om
            switch (op) {
            case EQUAL:
                jmp = new JumpInsnNode(Opcodes.IFEQ, null);
                mn.instructions.add(jmp);
                break;
            case NOT_EQUAL:
                jmp = new JumpInsnNode(Opcodes.IFNE, null);
                mn.instructions.add(jmp);
                break;
            case GREATER:
                jmp = new JumpInsnNode(Opcodes.IFGT, null);
                mn.instructions.add(jmp);
                break;
            case GREATER_EQUAL:
                jmp = new JumpInsnNode(Opcodes.IFGE, null);
                mn.instructions.add(jmp);
                break;
            case LESS:
                jmp = new JumpInsnNode(Opcodes.IFLT, null);
                mn.instructions.add(jmp);
                break;
            case LESS_EQUAL:
                jmp = new JumpInsnNode(Opcodes.IFLE, null);
                mn.instructions.add(jmp);
                break;
            default:
                ASTUtils.error(node, "Operator not supported");
                break;
            }
            mn.instructions.add(new InsnNode(Opcodes.ICONST_0));
            LabelNode endLabelNode = new LabelNode();
            mn.instructions.add(new JumpInsnNode(Opcodes.GOTO, endLabelNode));
            LabelNode trueLabelNode = new LabelNode();
            jmp.label = trueLabelNode;
            mn.instructions.add(trueLabelNode);
            mn.instructions.add(new InsnNode(Opcodes.ICONST_1));
            mn.instructions.add(endLabelNode);
        } else if (type.equals(Type.INT_TYPE)) {
            LabelNode trueLabelNode = new LabelNode();
            switch (op) {
            case EQUAL:
                mn.instructions.add(new JumpInsnNode(Opcodes.IF_ICMPEQ, trueLabelNode));
                break;
            case NOT_EQUAL:
                mn.instructions.add(new JumpInsnNode(Opcodes.IF_ICMPNE, trueLabelNode));
                break;
            case GREATER:
                mn.instructions.add(new JumpInsnNode(Opcodes.IF_ICMPGT, trueLabelNode));
                break;
            case GREATER_EQUAL:
                mn.instructions.add(new JumpInsnNode(Opcodes.IF_ICMPGE, trueLabelNode));
                break;
            case LESS:
                mn.instructions.add(new JumpInsnNode(Opcodes.IF_ICMPLT, trueLabelNode));
                break;
            case LESS_EQUAL:
                mn.instructions.add(new JumpInsnNode(Opcodes.IF_ICMPLE, trueLabelNode));
                break;
            default:
                break;
            }
            mn.instructions.add(new InsnNode(Opcodes.ICONST_0));
            LabelNode endLabelNode = new LabelNode();
            mn.instructions.add(new JumpInsnNode(Opcodes.GOTO, endLabelNode));
            mn.instructions.add(trueLabelNode);
            mn.instructions.add(new InsnNode(Opcodes.ICONST_1));
            mn.instructions.add(endLabelNode);
        } else {
            ASTUtils.error(node, "Cannot compare such types.");
        }
    } else {
        ASTUtils.error(node, "Operator not recognized.");
    }
}

From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java

/**
 * Assumes top of stack contains two strings
 *///from w ww.jav a 2s  .c  o  m
private void handleStringOperator(ASTNode node, Operator op) throws ASTVisitorException {
    if (op.equals(Operator.PLUS)) {
        mn.instructions.add(new TypeInsnNode(Opcodes.NEW, "java/lang/StringBuilder"));
        mn.instructions.add(new InsnNode(Opcodes.DUP));
        mn.instructions.add(
                new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "()V", false));
        mn.instructions.add(new InsnNode(Opcodes.SWAP));
        mn.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false));
        mn.instructions.add(new InsnNode(Opcodes.SWAP));
        mn.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
                "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false));
        mn.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString",
                "()Ljava/lang/String;", false));
    } else if (op.isRelational()) {
        LabelNode trueLabelNode = new LabelNode();
        switch (op) {
        case EQUAL:
            mn.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals",
                    "(Ljava/lang/Object;)Z", false));
            mn.instructions.add(new JumpInsnNode(Opcodes.IFNE, trueLabelNode));
            break;
        case NOT_EQUAL:
            mn.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals",
                    "(Ljava/lang/Object;)Z", false));
            mn.instructions.add(new JumpInsnNode(Opcodes.IFEQ, trueLabelNode));
            break;
        default:
            ASTUtils.error(node, "Operator not supported on strings");
            break;
        }
        mn.instructions.add(new InsnNode(Opcodes.ICONST_0));
        LabelNode endLabelNode = new LabelNode();
        mn.instructions.add(new JumpInsnNode(Opcodes.GOTO, endLabelNode));
        mn.instructions.add(trueLabelNode);
        mn.instructions.add(new InsnNode(Opcodes.ICONST_1));
        mn.instructions.add(endLabelNode);
    } else {
        ASTUtils.error(node, "Operator not recognized");
    }
}

From source file:org.jacoco.core.instr.ResizeInstructionsTest.java

License:Open Source License

/**
 * Adds code that triggers usage of/* ww w.  ja  v  a2  s  .co m*/
 * {@link org.objectweb.asm.MethodWriter#COMPUTE_INSERTED_FRAMES} during
 * instrumentation.
 */
private static void addCauseOfResizeInstructions(final MethodVisitor mv) {
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitInsn(Opcodes.ICONST_1);
    final Label target = new Label();
    mv.visitJumpInsn(Opcodes.IFLE, target);
    for (int i = 0; i < Short.MAX_VALUE; i++) {
        mv.visitInsn(Opcodes.NOP);
    }
    mv.visitLabel(target);
}

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);//  w ww. j  a v a  2  s . c  om
    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 ww .  java  2s  . com*/
 * 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)));
}

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

License:Open Source License

/**
 * <pre>//w  w w. j av  a2s .  c o m
 * class C(a: Int = 42)
 * </pre>
 */
@Test
public void should_filter_constructors() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_SYNTHETIC, "<init>",
            "(IILkotlin/jvm/internal/DefaultConstructorMarker;)V", null, null);
    context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);

    m.visitVarInsn(Opcodes.ILOAD, 2);
    m.visitInsn(Opcodes.ICONST_1);
    m.visitInsn(Opcodes.IAND);
    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, "Owner", "<init>", "(I)V", false);
    m.visitInsn(Opcodes.RETURN);

    filter.filter(m, context, output);

    assertIgnored(new Range(m.instructions.get(3), m.instructions.get(3)));
}

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

License:Open Source License

/**
 * <pre>//  w w w  .j av  a  2s . c o m
 * data class C(val x: Long = 42)
 * </pre>
 */
@Test
public void should_filter_methods_with_parameters_that_consume_two_slots() {
    final MethodNode m = new MethodNode(InstrSupport.ASM_API_VERSION, Opcodes.ACC_SYNTHETIC, "<init>",
            "(JILkotlin/jvm/internal/DefaultConstructorMarker;)V", null, null);
    context.classAnnotations.add(KotlinGeneratedFilter.KOTLIN_METADATA_DESC);

    m.visitVarInsn(Opcodes.ILOAD, 3);
    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, "Owner", "<init>", "(J)V", false);
    m.visitInsn(Opcodes.RETURN);

    filter.filter(m, context, output);

    assertIgnored(new Range(m.instructions.get(3), m.instructions.get(3)));
}

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

License:Open Source License

@Test
public void should_filter_implicit_default() {
    final Label case1 = new Label();
    final Label caseDefault = new Label();
    final Label after = new Label();

    m.visitInsn(Opcodes.NOP);/* www  . j  a  v  a2s  . com*/

    m.visitTableSwitchInsn(0, 0, caseDefault, case1);
    final AbstractInsnNode switchNode = m.instructions.getLast();
    final Set<AbstractInsnNode> newTargets = new HashSet<AbstractInsnNode>();

    m.visitLabel(case1);
    m.visitInsn(Opcodes.ICONST_1);
    newTargets.add(m.instructions.getLast());
    m.visitJumpInsn(Opcodes.GOTO, after);

    final Range range1 = new Range();
    m.visitLabel(caseDefault);
    range1.fromInclusive = m.instructions.getLast();
    m.visitTypeInsn(Opcodes.NEW, "kotlin/NoWhenBranchMatchedException");
    m.visitInsn(Opcodes.DUP);
    m.visitMethodInsn(Opcodes.INVOKESPECIAL, "kotlin/NoWhenBranchMatchedException", "<init>", "()V", false);
    m.visitInsn(Opcodes.ATHROW);
    range1.toInclusive = m.instructions.getLast();

    m.visitLabel(after);

    filter.filter(m, context, output);

    assertIgnored(range1);
    assertReplacedBranches(switchNode, newTargets);
}

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

License:Open Source License

private void createFirstSwitch() {
    final Label h1 = new Label();
    final Label h1_2 = new Label();
    final Label h2 = new Label();
    final Label secondSwitch = new Label();

    m.visitInsn(Opcodes.ICONST_M1);//from  www .  j  a v a 2  s  . c om
    m.visitVarInsn(Opcodes.ISTORE, 2);

    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "hashCode", "()I", false);
    m.visitLookupSwitchInsn(secondSwitch, new int[] { 97, 98 }, new Label[] { h1, h2 });
    expectedFromInclusive = m.instructions.getLast();

    m.visitLabel(h1);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitLdcInsn("a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if not equal "a", then goto next comparison
    m.visitJumpInsn(Opcodes.IFEQ, h1_2);
    m.visitInsn(Opcodes.ICONST_0);
    m.visitVarInsn(Opcodes.ISTORE, 2);

    // goto secondSwitch
    m.visitJumpInsn(Opcodes.GOTO, secondSwitch);

    m.visitLabel(h1_2);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitLdcInsn("\0a");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if not equal "\0a", then goto second switch
    m.visitJumpInsn(Opcodes.IFEQ, secondSwitch);
    m.visitInsn(Opcodes.ICONST_1);
    m.visitVarInsn(Opcodes.ISTORE, 2);

    // goto secondSwitch
    m.visitJumpInsn(Opcodes.GOTO, secondSwitch);

    m.visitLabel(h2);
    m.visitVarInsn(Opcodes.ALOAD, 1);
    m.visitLdcInsn("b");
    m.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z", false);
    // if not equal "b", then goto second switch
    m.visitJumpInsn(Opcodes.IFEQ, secondSwitch);
    m.visitInsn(Opcodes.ICONST_2);
    m.visitVarInsn(Opcodes.ISTORE, 2);

    m.visitLabel(secondSwitch);
    expectedToInclusive = m.instructions.getLast();
    m.visitVarInsn(Opcodes.ILOAD, 2);
}