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:com.yahoo.yqlplus.engine.internal.compiler.EqualsExpression.java

private void emitNegate(MethodVisitor mv) {
    Label truth = new Label();
    Label skip = new Label();
    mv.visitJumpInsn(Opcodes.IFNE, truth);
    mv.visitInsn(Opcodes.ICONST_1);
    mv.visitJumpInsn(Opcodes.GOTO, skip);
    mv.visitLabel(truth);//from w  w  w .  j  av  a  2  s  .c o  m
    mv.visitInsn(Opcodes.ICONST_0);
    mv.visitLabel(skip);
}

From source file:de.kandid.model.Emitter.java

License:Apache License

/**
 * Assembles a class that implements the given interface by generating the byte code.
 * @param interfaze the interface to implement
 * @return the class/* w w w.  j  av  a2  s.  com*/
 */
private static Class<? extends Emitter<?>> makeClass(Class<?> interfaze) {
    String nameClass = _nameEmitter + '$' + interfaze.getName().replace('.', '$');
    String nameInterface = Type.getInternalName(interfaze);
    // ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, nameClass, null, _nameEmitter, new String[] { name(interfaze) });

    // Generate default construcotor
    MethodVisitor cv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    cv.visitVarInsn(ALOAD, 0);
    cv.visitMethodInsn(INVOKESPECIAL, _nameEmitter, "<init>", "()V");
    cv.visitInsn(RETURN);
    cv.visitMaxs(1, 1);

    // Generate methods
    Method[] methods = interfaze.getMethods();
    for (int i = 0; i < methods.length; ++i) {
        final Method m = methods[i];
        if (m.getReturnType() != void.class)
            throw new IllegalArgumentException("Method " + m.toGenericString() + " must not return a value");
        final String descMethod = Type.getMethodDescriptor(m);
        final MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_SYNCHRONIZED, m.getName(), descMethod, null,
                null);
        final Type[] argTypes = Type.getArgumentTypes(m);

        // for (int i = 0; i < _end; i += 2)
        //    ((Listener) _listeners[i]).send(...);

        int localStart = 1; // give one for "this"
        for (Type at : argTypes)
            localStart += at.getSize();
        Label entry = new Label();
        Label exit = new Label();
        mw.visitLabel(entry);

        // _isFiring = true;
        mw.visitVarInsn(ALOAD, 0);
        mw.visitInsn(Opcodes.ICONST_1);
        mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor());

        // setup local variables: i, _listeners, _end
        mw.visitLocalVariable("i", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart);
        mw.visitInsn(Opcodes.ICONST_0);
        mw.visitIntInsn(Opcodes.ISTORE, localStart);

        mw.visitLocalVariable("listeners", _descObjectArray, null, entry, exit, localStart + 1);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitFieldInsn(GETFIELD, nameClass, "_listeners", _descObjectArray);
        mw.visitIntInsn(Opcodes.ASTORE, localStart + 1);

        mw.visitLocalVariable("end", Type.INT_TYPE.getDescriptor(), null, entry, exit, localStart + 2);
        mw.visitVarInsn(ALOAD, 0);
        mw.visitFieldInsn(GETFIELD, nameClass, "_end", Type.INT_TYPE.getDescriptor());
        mw.visitIntInsn(Opcodes.ISTORE, localStart + 2);

        final Label condition = new Label();
        mw.visitJumpInsn(GOTO, condition);

        final Label loop = new Label();
        mw.visitLabel(loop);

        //((Listener) _listeners[i]).doSomething()
        mw.visitIntInsn(Opcodes.ALOAD, localStart + 1);
        mw.visitIntInsn(Opcodes.ILOAD, localStart);
        mw.visitInsn(Opcodes.AALOAD);
        mw.visitTypeInsn(CHECKCAST, nameInterface);
        int offs = 1; // give one for "this"
        for (Type at : argTypes) {
            mw.visitVarInsn(at.getOpcode(ILOAD), offs);
            offs += at.getSize();
        }
        mw.visitMethodInsn(INVOKEINTERFACE, nameInterface, m.getName(), descMethod);

        // i += 2
        mw.visitIincInsn(localStart, 2);

        // if (i < end) goto loop
        mw.visitLabel(condition);
        mw.visitIntInsn(Opcodes.ILOAD, localStart);
        mw.visitIntInsn(Opcodes.ILOAD, localStart + 2);
        mw.visitJumpInsn(Opcodes.IF_ICMPLT, loop);

        // _isFiring = false;
        mw.visitVarInsn(ALOAD, 0);
        mw.visitInsn(Opcodes.ICONST_0);
        mw.visitFieldInsn(Opcodes.PUTFIELD, nameClass, "_isFiring", Type.BOOLEAN_TYPE.getDescriptor());

        mw.visitLabel(exit);
        mw.visitInsn(RETURN);
        mw.visitMaxs(localStart + 2, localStart + 3);
        mw.visitEnd();
    }
    cw.visitEnd();
    return _loader.loadClass(cw, nameClass.replace('/', '.'));
}

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

License:Creative Commons License

private static void transformUpdateHorseSlots(MethodNode method) {
    InsnList needle = new InsnList();
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_HORSE_CHEST));
    needle.add(new InsnNode(Opcodes.ICONST_1));

    AbstractInsnNode pointer = ASMHelper.findFirstNodeFromNeedle(method.instructions, needle);

    InsnList newInstr = new InsnList();
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(ASMHelper.getFieldInsnNode(Opcodes.GETFIELD, ASMNames.FD_HORSE_CHEST));
    newInstr.add(new InsnNode(Opcodes.ICONST_1));
    newInstr.add(ASMHelper.getMethodInsnNode(Opcodes.INVOKEVIRTUAL, ASMNames.MD_ANIMALCHEST_GET_STACK_IN_SLOT,
            false));// ww  w . java  2s.com
    newInstr.add(
            ASMHelper.getMethodInsnNode(Opcodes.INVOKESPECIAL, ASMNames.MD_SAP_SET_CUSTOM_ARMOR_ITEM, false));
    newInstr.add(new LabelNode());

    method.instructions.insertBefore(pointer, newInstr);
}

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

License:Creative Commons License

private static void transformIsValidArmor(MethodNode method) {
    InsnList needle = new InsnList();
    needle.add(new LabelNode());
    needle.add(new LineNumberNode(-1, new LabelNode()));
    needle.add(new VarInsnNode(Opcodes.ALOAD, 0));
    needle.add(ASMHelper.getFieldInsnNode(Opcodes.GETSTATIC, ASMNames.FD_ITEMS_IRON_HORSE_ARMOR));
    needle.add(new JumpInsnNode(Opcodes.IF_ACMPEQ, new LabelNode()));

    AbstractInsnNode node = ASMHelper.findFirstNodeFromNeedle(method.instructions, needle);

    InsnList newInstr = new InsnList();
    newInstr.add(new LabelNode());
    newInstr.add(new VarInsnNode(Opcodes.ALOAD, 0));
    newInstr.add(new TypeInsnNode(Opcodes.INSTANCEOF, ASMNames.CL_ITEM_HORSE_ARMOR));
    LabelNode ln = new LabelNode();
    newInstr.add(new JumpInsnNode(Opcodes.IFEQ, ln));
    newInstr.add(new LabelNode());
    newInstr.add(new InsnNode(Opcodes.ICONST_1));
    newInstr.add(new InsnNode(Opcodes.IRETURN));
    newInstr.add(ln);//from  w  ww.  j  av a2s  .  c o  m

    method.instructions.insertBefore(node, newInstr);
}

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

License:Creative Commons License

/**
 * Transforms the Entity.class by adding a new method called _SAP_canDismountOnInput.<br>
 * This method can be overridden by any entity to control wether or not the rider can dismount via sneaking (usually by pressing LSHIFT for the player).
 *
 * @param bytes     the class bytes to be transformed
 * @return the transformed class bytes//from   w ww  .j av a 2  s  . c o  m
 */
private static byte[] transformEntity(byte[] bytes) {
    ClassNode clazz = ASMHelper.createClassNode(bytes);

    MethodNode method = ASMHelper.getMethodNode(Opcodes.ACC_PUBLIC, ASMNames.MD_SAP_CAN_DISMOUNT_ON_INPUT);
    method.visitCode();
    Label l0 = new Label();
    method.visitLabel(l0);
    method.visitInsn(Opcodes.ICONST_1);
    method.visitInsn(Opcodes.IRETURN);
    Label l1 = new Label();
    method.visitLabel(l1);
    method.visitLocalVariable("this", ASMNames.CL_T_ENTITY, null, l0, l1, 0);
    method.visitLocalVariable("player", ASMNames.CL_T_ENTITY_PLAYER, null, l0, l1, 1);
    method.visitMaxs(1, 2);
    method.visitEnd();

    clazz.methods.add(method);

    bytes = ASMHelper.createBytes(clazz, /*ClassWriter.COMPUTE_FRAMES |*/ ClassWriter.COMPUTE_MAXS);
    return bytes;
}

From source file:de.tuberlin.uebb.jbop.optimizer.arithmetic.ArithmeticExpressionInterpreterTest.java

License:Open Source License

/**
 * Tests that arithmeticExpressionInterpreter() of the Testobject is working correctly
 * if nothing is todo (no arithmetic Expression occur in instructions).
 *///from w ww. j a v  a 2 s.  c o m
@Test
public void testArithmeticExpressionInterpreterNoArithmeticInstructions() {
    // INIT
    builder.addInsn(new InsnNode(Opcodes.ICONST_1)).//
            addInsn(new VarInsnNode(Opcodes.ISTORE, 1)).//
            addInsn(new InsnNode(Opcodes.ICONST_2)).//
            addInsn(new VarInsnNode(Opcodes.ISTORE, 2)).//
            addInsn(new InsnNode(Opcodes.ICONST_3)).//
            addInsn(new VarInsnNode(Opcodes.ISTORE, 3)).//
            addInsn(new InsnNode(Opcodes.RETURN));
    // RUN
    final InsnList optimized = interpreter.optimize(builder.getMethod("testMethod").instructions,
            builder.getMethod("testMethod"));

    // ASSERT
    assertFalse(interpreter.isOptimized());
    assertEquals(7, optimized.size());
}

From source file:de.tuberlin.uebb.jbop.optimizer.arithmetic.ArithmeticExpressionInterpreterTest.java

License:Open Source License

/**
 * Tests that arithmeticExpressionInterpreter() of the Testobject is working correctly
 * if arithmetic Expression occur in instructions.
 *//*from  w w w .j av a2 s  .  c  o  m*/
@Test
public void testArithmeticExpressionInterpreter() {
    // INIT
    builder.addInsn(new InsnNode(Opcodes.ICONST_1)).//
            addInsn(new InsnNode(Opcodes.ICONST_2)).//
            addInsn(new InsnNode(Opcodes.IADD)).//
            addInsn(new InsnNode(Opcodes.ICONST_1)).//
            addInsn(new InsnNode(Opcodes.ICONST_1)).//
            addInsn(new InsnNode(Opcodes.IADD)).//
            addInsn(new InsnNode(Opcodes.IADD)).//
            addInsn(new InsnNode(Opcodes.RETURN));

    // RUN STEP1
    final InsnList optimized = interpreter.optimize(builder.getMethod("testMethod").instructions,
            builder.getMethod("testMethod"));

    // ASSERT STEP 1
    assertTrue(interpreter.isOptimized());
    assertEquals(2, optimized.size());
    assertEquals(Opcodes.ICONST_5, optimized.getFirst().getOpcode());

    // RUN STEP 2
    final InsnList optimized2 = interpreter.optimize(builder.getMethod("testMethod").instructions,
            builder.getMethod("testMethod"));

    // ASSERT STEP 2
    assertFalse(interpreter.isOptimized());
    assertEquals(2, optimized2.size());
    assertEquals(Opcodes.ICONST_5, optimized2.getFirst().getOpcode());
}

From source file:de.tuberlin.uebb.jbop.optimizer.arithmetic.ArithmeticExpressionInterpreterTest.java

License:Open Source License

/**
 * Tests that arithmeticExpressionInterpreter() of the Testobject is working correctly
 * for logical or expressions./*from  w w  w.j  av  a  2  s  .c  om*/
 */
@Test
public void testArithmeticExpressionInterpreterLogicalIntOr() {
    // INIT
    builder.addInsn(new InsnNode(Opcodes.ICONST_1)).//
            addInsn(new InsnNode(Opcodes.ICONST_2)).//
            addInsn(new InsnNode(Opcodes.IOR)).//
            addInsn(new InsnNode(Opcodes.RETURN));

    final InsnList optimized = interpreter.optimize(builder.getMethod("testMethod").instructions,
            builder.getMethod("testMethod"));

    assertEquals(2, optimized.size());
    assertEquals(3, NodeHelper.getNumberValue(optimized.get(0)));
}

From source file:de.tuberlin.uebb.jbop.optimizer.arithmetic.ArithmeticExpressionInterpreterTest.java

License:Open Source License

/**
 * Tests that arithmeticExpressionInterpreter() of the Testobject is working correctly
 * for logical xor expressions.//  ww  w  .  java  2  s.  c o  m
 */
@Test
public void testArithmeticExpressionInterpreterLogicalIntXOr() {
    // INIT
    builder.addInsn(new InsnNode(Opcodes.ICONST_1)).//
            addInsn(new InsnNode(Opcodes.ICONST_2)).//
            addInsn(new InsnNode(Opcodes.IXOR)).//
            addInsn(new InsnNode(Opcodes.RETURN));

    final InsnList optimized = interpreter.optimize(builder.getMethod("testMethod").instructions,
            builder.getMethod("testMethod"));

    assertEquals(2, optimized.size());
    assertEquals(3, NodeHelper.getNumberValue(optimized.get(0)));
}

From source file:de.tuberlin.uebb.jbop.optimizer.arithmetic.ArithmeticExpressionInterpreterTest.java

License:Open Source License

/**
 * Tests that arithmeticExpressionInterpreter() of the Testobject is working correctly
 * for logical and expressions./*ww w.ja v a 2 s . c  o m*/
 */
@Test
public void testArithmeticExpressionInterpreterLogicalIntAnd() {
    // INIT
    builder.addInsn(new InsnNode(Opcodes.ICONST_1)).//
            addInsn(new InsnNode(Opcodes.ICONST_2)).//
            addInsn(new InsnNode(Opcodes.IAND)).//
            addInsn(new InsnNode(Opcodes.RETURN));

    final InsnList optimized = interpreter.optimize(builder.getMethod("testMethod").instructions,
            builder.getMethod("testMethod"));

    assertEquals(2, optimized.size());
    assertEquals(0, NodeHelper.getNumberValue(optimized.get(0)));
}