Example usage for org.objectweb.asm Opcodes ICONST_0

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

Introduction

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

Prototype

int ICONST_0

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

Click Source Link

Usage

From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java

License:Open Source License

private static JavaTypeName encodeThenTrueElseFalse(Label trueContinuation, Label falseContinuation,
        GenerationContext context) {/*  www  . jav  a 2 s  . com*/

    MethodVisitor mv = context.getMethodVisitor();

    mv.visitLabel(trueContinuation);
    mv.visitInsn(Opcodes.ICONST_1);
    Label nextLabel = new Label();
    mv.visitJumpInsn(Opcodes.GOTO, nextLabel);
    mv.visitLabel(falseContinuation);
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitLabel(nextLabel);

    return JavaTypeName.BOOLEAN;
}

From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java

License:Open Source License

/**
 * Encodes instructions to push an int value onto the operand stack.
 * Does not reallocate the wrapper argument 'value'.
 * @param value to be pushed /*from   ww w .  ja  v  a  2 s.c o  m*/
 * @param context
 * @return JavaTypeName the type of the result on the operand stack.      
 */
private static JavaTypeName encodePushIntegerValue(Integer value, GenerationContext context) {

    MethodVisitor mv = context.getMethodVisitor();

    int v = value.intValue();

    if (v >= -1 && v <= 5) {
        // Use ICONST_n
        mv.visitInsn(Opcodes.ICONST_0 + v);

    } else if (v >= Byte.MIN_VALUE && v <= Byte.MAX_VALUE) {
        // Use BIPUSH
        mv.visitIntInsn(Opcodes.BIPUSH, (byte) v);

    } else if (v >= Short.MIN_VALUE && v <= Short.MAX_VALUE) {
        // Use SIPUSH
        mv.visitIntInsn(Opcodes.SIPUSH, (short) v);

    } else {
        // If everything fails create a Constant pool entry
        mv.visitLdcInsn(value);
    }

    return JavaTypeName.INT;
}

From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java

License:Open Source License

/**
 * Encodes instructions to push an int value onto the operand stack.
 * May need to box the argument 'value' if it is sufficiently large that
 * it needs to go into the constant pool.
 * @param value to be pushed // w w w  .  j  a v a2  s . com
 * @param context
 * @return JavaTypeName the type of the result on the operand stack. 
 */
private static JavaTypeName encodePushIntValue(int value, GenerationContext context) {

    MethodVisitor mv = context.getMethodVisitor();

    if (value >= -1 && value <= 5) {
        // Use ICONST_n
        mv.visitInsn(Opcodes.ICONST_0 + value);

    } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
        // Use BIPUSH
        mv.visitIntInsn(Opcodes.BIPUSH, (byte) value);

    } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
        // Use SIPUSH
        mv.visitIntInsn(Opcodes.SIPUSH, (short) value);

    } else {
        // If everything fails create a Constant pool entry
        mv.visitLdcInsn(Integer.valueOf(value));
    }

    return JavaTypeName.INT;
}

From source file:org.openquark.cal.internal.javamodel.AsmJavaBytecodeGenerator.java

License:Open Source License

/**
 * Encodes instructions to push a boolean value onto the operand stack.
 * @param value to be pushed    /*from w ww. j a  va2 s .co m*/
 * @param context
 * @return JavaTypeName the type of the result on the operand stack.      
 */
private static JavaTypeName encodePushBooleanValue(Boolean value, GenerationContext context) {
    MethodVisitor mv = context.getMethodVisitor();

    boolean v = value.booleanValue();
    if (v) {
        mv.visitInsn(Opcodes.ICONST_1);
    } else {
        mv.visitInsn(Opcodes.ICONST_0);
    }

    return JavaTypeName.BOOLEAN;
}

From source file:org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator.java

License:Apache License

private static ZeroOperandMutation ireturnMutation() {
    return new ZeroOperandMutation() {

        public void apply(final int opCode, final MethodVisitor mv) {
            final Label l1 = new Label();
            mv.visitJumpInsn(Opcodes.IFEQ, l1);
            mv.visitInsn(Opcodes.ICONST_0);
            mv.visitInsn(Opcodes.IRETURN);
            mv.visitLabel(l1);//from   w w w  .  j ava2 s. c  om
            mv.visitInsn(Opcodes.ICONST_1);
            mv.visitInsn(Opcodes.IRETURN);
        }

        public String decribe(final int opCode, final MethodInfo methodInfo) {
            return "replaced return of integer sized value with (x == 0 ? 1 : 0)";
        }

    };
}

From source file:org.sonar.java.bytecode.cfg.BytecodeCFGBuilderTest.java

License:Open Source License

@Test
public void visited_label_should_be_assigned_to_true_successor() throws Exception {
    Label label0 = new Label();
    Label label1 = new Label();
    BytecodeCFG cfg = new Instructions().visitVarInsn(Opcodes.ALOAD, 0).visitJumpInsn(Opcodes.IFNULL, label0)
            .visitJumpInsn(Opcodes.IFEQ, label0).visitInsn(Opcodes.ICONST_0).visitJumpInsn(Opcodes.GOTO, label1)
            .visitLabel(label0).visitInsn(Opcodes.ICONST_1).visitLabel(label1).visitInsn(Opcodes.IRETURN).cfg();

    BytecodeCFG.Block block3 = cfg.blocks.get(3);
    assertThat(block3.terminator.opcode).isEqualTo(Opcodes.IFEQ);
    assertThat(block3.falseSuccessor()).isNotNull().isSameAs(cfg.blocks.get(4));
    assertThat(block3.trueSuccessor()).isNotNull().isSameAs(cfg.blocks.get(2));
    assertThat(block3.successors).hasSize(2);
    assertThat(block3.successors()).hasSize(2);
}

From source file:org.sonar.java.bytecode.cfg.BytecodeCFGBuilderTest.java

License:Open Source License

@Test
public void goto_successors() throws Exception {
    Label label0 = new Label();
    Label label1 = new Label();
    BytecodeCFG cfg = new Instructions().visitVarInsn(Opcodes.ALOAD, 0).visitJumpInsn(Opcodes.IFNULL, label0)
            .visitVarInsn(Opcodes.ALOAD, 0).visitJumpInsn(Opcodes.IFNULL, label1).visitVarInsn(Opcodes.ALOAD, 0)
            .visitVarInsn(Opcodes.ALOAD, 0).visitJumpInsn(Opcodes.IFEQ, label0).visitInsn(Opcodes.ICONST_0)
            .visitJumpInsn(Opcodes.GOTO, label1).visitLabel(label0).visitInsn(Opcodes.ICONST_1)
            .visitLabel(label1).visitInsn(Opcodes.IRETURN).cfg();
    assertThat(cfg.blocks.get(6).successors).containsExactly(cfg.blocks.get(4));
}

From source file:org.sonar.java.bytecode.se.BytecodeEGWalkerExecuteTest.java

License:Open Source License

@Test
public void test_iconst() throws Exception {
    ProgramState programState = execute(new Instruction(Opcodes.ICONST_0));
    assertStack(programState, new Constraint[][] {
            { DivisionByZeroCheck.ZeroConstraint.ZERO, BooleanConstraint.FALSE, ObjectConstraint.NOT_NULL } });

    programState = execute(new Instruction(Opcodes.ICONST_1));
    assertStack(programState, new Constraint[][] { { DivisionByZeroCheck.ZeroConstraint.NON_ZERO,
            BooleanConstraint.TRUE, ObjectConstraint.NOT_NULL } });

    int[] opCodesConst = new int[] { Opcodes.ICONST_M1, Opcodes.ICONST_2, Opcodes.ICONST_3, Opcodes.ICONST_4,
            Opcodes.ICONST_5 };/* w ww.ja v  a2  s .c o  m*/
    for (int opcode : opCodesConst) {
        programState = execute(new Instruction(opcode));
        assertStack(programState, new Constraint[][] {
                { DivisionByZeroCheck.ZeroConstraint.NON_ZERO, ObjectConstraint.NOT_NULL } });
    }
}

From source file:org.spongepowered.asm.mixin.injection.callback.CallbackInjector.java

License:MIT License

/**
 * @param callback callback handle//ww  w .  j a va 2s  .c o m
 */
protected void invokeCallbackInfoCtor(final Callback callback) {
    callback.add(new LdcInsnNode(callback.target.method.name), true, false);
    callback.add(new InsnNode(this.cancellable ? Opcodes.ICONST_1 : Opcodes.ICONST_0), true, false);

    if (callback.isAtReturn) {
        callback.add(
                new VarInsnNode(callback.target.returnType.getOpcode(Opcodes.ILOAD), callback.marshallVar));
        callback.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, callback.target.callbackInfoClass, Injector.CTOR,
                CallbackInfo.getConstructorDescriptor(callback.target.returnType), false));
    } else {
        callback.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, callback.target.callbackInfoClass, Injector.CTOR,
                CallbackInfo.getConstructorDescriptor(), false));
    }
}

From source file:org.spongepowered.despector.emitter.bytecode.instruction.BytecodeIntConstantEmitter.java

License:Open Source License

@Override
public void emit(BytecodeEmitterContext ctx, IntConstant arg, TypeSignature type) {
    MethodVisitor mv = ctx.getMethodVisitor();
    int val = arg.getConstant();
    if (val == -1) {
        mv.visitInsn(Opcodes.ICONST_M1);
    } else if (val == 0) {
        mv.visitInsn(Opcodes.ICONST_0);
    } else if (val == 1) {
        mv.visitInsn(Opcodes.ICONST_1);/*from w w  w  .j av  a2s . co  m*/
    } else if (val == 2) {
        mv.visitInsn(Opcodes.ICONST_2);
    } else if (val == 3) {
        mv.visitInsn(Opcodes.ICONST_3);
    } else if (val == 4) {
        mv.visitInsn(Opcodes.ICONST_4);
    } else if (val == 5) {
        mv.visitInsn(Opcodes.ICONST_5);
    } else if (val <= Byte.MAX_VALUE && val >= Byte.MIN_VALUE) {
        mv.visitIntInsn(Opcodes.BIPUSH, val);
    } else if (val <= Short.MAX_VALUE && val >= Short.MIN_VALUE) {
        mv.visitIntInsn(Opcodes.SIPUSH, val);
    } else {
        mv.visitLdcInsn(val);
    }
    ctx.updateStack(1);
}