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:bytecode.InstructionExporter.java

License:Apache License

/**
 * Outputs constants into stack operations. This will use different JVM
 * instructions depending on whether there are short-hands for the given
 * constant etc./*  w  w w.  ja  v  a  2s .c  o m*/
 *
 * @param instruction Constant.
 * @return            <code>null</code>
 */
@Override
public Void visit(Constant instruction) {
    // NULL Pointer
    if (instruction.getConstant() == null) {
        mv.visitInsn(Opcodes.ACONST_NULL);
        // Integers
    } else if (instruction.getConstant() instanceof Integer) {
        Integer i = (Integer) instruction.getConstant();

        switch (i.intValue()) {
        case -1:
            mv.visitInsn(Opcodes.ICONST_M1);
            break;
        case 0:
            mv.visitInsn(Opcodes.ICONST_0);
            break;
        case 1:
            mv.visitInsn(Opcodes.ICONST_1);
            break;
        case 2:
            mv.visitInsn(Opcodes.ICONST_2);
            break;
        case 3:
            mv.visitInsn(Opcodes.ICONST_3);
            break;
        case 4:
            mv.visitInsn(Opcodes.ICONST_4);
            break;
        case 5:
            mv.visitInsn(Opcodes.ICONST_5);
            break;
        default:
            mv.visitLdcInsn(i);
        }
        // Longs
    } else if (instruction.getConstant() instanceof Long) {
        Long l = (Long) instruction.getConstant();

        if (l.longValue() == 0)
            mv.visitInsn(Opcodes.LCONST_0);
        else if (l.longValue() == 1)
            mv.visitInsn(Opcodes.ICONST_1);
        else
            mv.visitLdcInsn(l);
        // Floats
    } else if (instruction.getConstant() instanceof Float) {
        Float f = (Float) instruction.getConstant();

        // Should be safe since 0 and 1 are both exactly representable in FP.
        if (f.floatValue() == 0.0f)
            mv.visitInsn(Opcodes.FCONST_0);
        else if (f.floatValue() == 1.0f)
            mv.visitInsn(Opcodes.FCONST_1);
        else if (f.floatValue() == 2.0f)
            mv.visitInsn(Opcodes.FCONST_2);
        else
            mv.visitLdcInsn(f);
        // Doubles
    } else if (instruction.getConstant() instanceof Double) {
        Double d = (Double) instruction.getConstant();

        // Should be safe since 0 and 1 are both exactly representable in FP.
        if (d.doubleValue() == 0.0f)
            mv.visitInsn(Opcodes.DCONST_0);
        else if (d.doubleValue() == 1.0f)
            mv.visitInsn(Opcodes.DCONST_1);
        else
            mv.visitLdcInsn(d);
        // Byte Pushes
    } else if (instruction.getConstant() instanceof Byte) {
        Byte b = (Byte) instruction.getConstant();

        mv.visitIntInsn(Opcodes.BIPUSH, b.intValue());
        // Short Pushes
    } else if (instruction.getConstant() instanceof Short) {
        Short s = (Short) instruction.getConstant();

        mv.visitIntInsn(Opcodes.SIPUSH, s.intValue());
        // Otherwise a constant pool load.
    } else {
        mv.visitLdcInsn(instruction.getConstant());
    }

    return null;
}

From source file:bytecode.MethodImporter.java

License:Apache License

/**
 * Imports instructions with no immediate operands.
 *
 * @param opcode Opcode./* w  w w  .  j  av a  2 s . c  o m*/
 */
@Override
public void visitInsn(final int opcode) {
    Producer a, b, c, d;
    Type top;

    switch (opcode) {
    // Constants
    case Opcodes.ACONST_NULL:
        createConstant(null);
        break;
    case Opcodes.ICONST_M1:
        createConstant(new Integer(-1));
        break;
    case Opcodes.ICONST_0:
        createConstant(new Integer(0));
        break;
    case Opcodes.ICONST_1:
        createConstant(new Integer(1));
        break;
    case Opcodes.ICONST_2:
        createConstant(new Integer(2));
        break;
    case Opcodes.ICONST_3:
        createConstant(new Integer(3));
        break;
    case Opcodes.ICONST_4:
        createConstant(new Integer(4));
        break;
    case Opcodes.ICONST_5:
        createConstant(new Integer(5));
        break;
    case Opcodes.LCONST_0:
        createConstant(new Long(0));
        break;
    case Opcodes.LCONST_1:
        createConstant(new Long(1));
        break;
    case Opcodes.FCONST_0:
        createConstant(new Float(0.0f));
        break;
    case Opcodes.FCONST_1:
        createConstant(new Float(1.0f));
        break;
    case Opcodes.FCONST_2:
        createConstant(new Float(2.0f));
        break;
    case Opcodes.DCONST_0:
        createConstant(new Double(0.0f));
        break;
    case Opcodes.DCONST_1:
        createConstant(new Double(1.0f));
        break;

    // Binary Operations
    case Opcodes.IADD:
        createArithmetic(Arithmetic.Operator.ADD, Type.INT);
        break;
    case Opcodes.LADD:
        createArithmetic(Arithmetic.Operator.ADD, Type.LONG);
        break;
    case Opcodes.FADD:
        createArithmetic(Arithmetic.Operator.ADD, Type.FLOAT);
        break;
    case Opcodes.DADD:
        createArithmetic(Arithmetic.Operator.ADD, Type.DOUBLE);
        break;
    case Opcodes.ISUB:
        createArithmetic(Arithmetic.Operator.SUB, Type.INT);
        break;
    case Opcodes.LSUB:
        createArithmetic(Arithmetic.Operator.SUB, Type.LONG);
        break;
    case Opcodes.FSUB:
        createArithmetic(Arithmetic.Operator.SUB, Type.FLOAT);
        break;
    case Opcodes.DSUB:
        createArithmetic(Arithmetic.Operator.SUB, Type.DOUBLE);
        break;
    case Opcodes.IMUL:
        createArithmetic(Arithmetic.Operator.MUL, Type.INT);
        break;
    case Opcodes.LMUL:
        createArithmetic(Arithmetic.Operator.MUL, Type.LONG);
        break;
    case Opcodes.FMUL:
        createArithmetic(Arithmetic.Operator.MUL, Type.FLOAT);
        break;
    case Opcodes.DMUL:
        createArithmetic(Arithmetic.Operator.MUL, Type.DOUBLE);
        break;
    case Opcodes.IDIV:
        createArithmetic(Arithmetic.Operator.DIV, Type.INT);
        break;
    case Opcodes.LDIV:
        createArithmetic(Arithmetic.Operator.DIV, Type.LONG);
        break;
    case Opcodes.FDIV:
        createArithmetic(Arithmetic.Operator.DIV, Type.FLOAT);
        break;
    case Opcodes.DDIV:
        createArithmetic(Arithmetic.Operator.DIV, Type.DOUBLE);
        break;
    case Opcodes.IREM:
        createArithmetic(Arithmetic.Operator.REM, Type.INT);
        break;
    case Opcodes.LREM:
        createArithmetic(Arithmetic.Operator.REM, Type.LONG);
        break;
    case Opcodes.FREM:
        createArithmetic(Arithmetic.Operator.REM, Type.FLOAT);
        break;
    case Opcodes.DREM:
        createArithmetic(Arithmetic.Operator.REM, Type.DOUBLE);
        break;
    case Opcodes.IAND:
        createArithmetic(Arithmetic.Operator.AND, Type.INT);
        break;
    case Opcodes.LAND:
        createArithmetic(Arithmetic.Operator.AND, Type.LONG);
        break;
    case Opcodes.IOR:
        createArithmetic(Arithmetic.Operator.OR, Type.INT);
        break;
    case Opcodes.LOR:
        createArithmetic(Arithmetic.Operator.OR, Type.LONG);
        break;
    case Opcodes.IXOR:
        createArithmetic(Arithmetic.Operator.XOR, Type.INT);
        break;
    case Opcodes.LXOR:
        createArithmetic(Arithmetic.Operator.XOR, Type.LONG);
        break;
    case Opcodes.ISHL:
        createArithmetic(Arithmetic.Operator.SHL, Type.INT);
        break;
    case Opcodes.LSHL:
        createArithmetic(Arithmetic.Operator.SHL, Type.LONG);
        break;
    case Opcodes.ISHR:
        createArithmetic(Arithmetic.Operator.SHR, Type.INT);
        break;
    case Opcodes.LSHR:
        createArithmetic(Arithmetic.Operator.SHR, Type.LONG);
        break;
    case Opcodes.IUSHR:
        createArithmetic(Arithmetic.Operator.USHR, Type.INT);
        break;
    case Opcodes.LUSHR:
        createArithmetic(Arithmetic.Operator.USHR, Type.LONG);
        break;

    case Opcodes.LCMP:
        createCompare(false, Type.LONG);
        break;
    case Opcodes.FCMPL:
        createCompare(false, Type.FLOAT);
        break;
    case Opcodes.FCMPG:
        createCompare(true, Type.FLOAT);
        break;
    case Opcodes.DCMPL:
        createCompare(false, Type.DOUBLE);
        break;
    case Opcodes.DCMPG:
        createCompare(true, Type.DOUBLE);
        break;
    case Opcodes.INEG:
        createNegate();
        break;
    case Opcodes.LNEG:
        createNegate();
        break;
    case Opcodes.FNEG:
        createNegate();
        break;
    case Opcodes.DNEG:
        createNegate();
        break;
    case Opcodes.I2L:
        createConvert(Type.LONG);
        break;
    case Opcodes.I2F:
        createConvert(Type.FLOAT);
        break;
    case Opcodes.I2D:
        createConvert(Type.DOUBLE);
        break;
    case Opcodes.I2B:
        createConvert(Type.BYTE);
        break;
    case Opcodes.I2C:
        createConvert(Type.CHAR);
        break;
    case Opcodes.I2S:
        createConvert(Type.SHORT);
        break;
    case Opcodes.L2I:
        createConvert(Type.INT);
        break;
    case Opcodes.L2F:
        createConvert(Type.FLOAT);
        break;
    case Opcodes.L2D:
        createConvert(Type.DOUBLE);
        break;
    case Opcodes.F2I:
        createConvert(Type.INT);
        break;
    case Opcodes.F2L:
        createConvert(Type.LONG);
        break;
    case Opcodes.F2D:
        createConvert(Type.DOUBLE);
        break;
    case Opcodes.D2I:
        createConvert(Type.INT);
        break;
    case Opcodes.D2F:
        createConvert(Type.FLOAT);
        break;
    case Opcodes.D2L:
        createConvert(Type.LONG);
        break;
    case Opcodes.IALOAD:
        createArrayRead(Type.INT);
        break;
    case Opcodes.LALOAD:
        createArrayRead(Type.LONG);
        break;
    case Opcodes.FALOAD:
        createArrayRead(Type.FLOAT);
        break;
    case Opcodes.DALOAD:
        createArrayRead(Type.DOUBLE);
        break;
    case Opcodes.AALOAD:
        createArrayRead(Type.getFreshRef());
        break;
    case Opcodes.BALOAD:
        createArrayRead(Type.BYTE);
        break;
    case Opcodes.CALOAD:
        createArrayRead(Type.CHAR);
        break;
    case Opcodes.SALOAD:
        createArrayRead(Type.SHORT);
        break;
    case Opcodes.IASTORE:
        createArrayWrite(Type.INT);
        break;
    case Opcodes.LASTORE:
        createArrayWrite(Type.LONG);
        break;
    case Opcodes.FASTORE:
        createArrayWrite(Type.FLOAT);
        break;
    case Opcodes.DASTORE:
        createArrayWrite(Type.DOUBLE);
        break;
    case Opcodes.AASTORE:
        createArrayWrite(Type.getFreshRef());
        break;
    case Opcodes.BASTORE:
        createArrayWrite(Type.BYTE);
        break;
    case Opcodes.CASTORE:
        createArrayWrite(Type.CHAR);
        break;
    case Opcodes.SASTORE:
        createArrayWrite(Type.SHORT);
        break;
    case Opcodes.IRETURN:
        createReturn(Type.INT);
        break;
    case Opcodes.LRETURN:
        createReturn(Type.LONG);
        break;
    case Opcodes.FRETURN:
        createReturn(Type.FLOAT);
        break;
    case Opcodes.DRETURN:
        createReturn(Type.DOUBLE);
        break;
    case Opcodes.ARETURN:
        createReturn(Type.REF);
        break;
    case Opcodes.RETURN:
        createReturn(null);
        break;
    case Opcodes.ATHROW:
        createThrow();
        break;

    // Array Length
    case Opcodes.ARRAYLENGTH:
        ordered.add(stack.push(new ArrayLength(stack.pop())));
        break;

    // Swap
    case Opcodes.SWAP:
        a = stack.pop();
        b = stack.pop();

        stack.push(a);
        stack.push(b);

        ordered.add(new StackOperation(StackOperation.Sort.SWAP));

        break;

    // Duplicates
    case Opcodes.DUP:
        stack.push(stack.peek());
        ordered.add(new StackOperation(StackOperation.Sort.DUP));
        break;
    case Opcodes.DUP2:
        top = stack.peek().getType();

        // Type 2 Values
        if (top.getSize() == 2) {
            stack.push(stack.peek());
            // Type 1 Values
        } else {
            b = stack.pop();
            a = stack.pop();

            stack.push(a);
            stack.push(b);
            stack.push(a);
            stack.push(b);
        }

        ordered.add(new StackOperation(StackOperation.Sort.DUP2));

        break;
    case Opcodes.DUP_X1:
        b = stack.pop();
        a = stack.pop();

        stack.push(b);
        stack.push(a);
        stack.push(b);

        ordered.add(new StackOperation(StackOperation.Sort.DUP_X1));

        break;
    case Opcodes.DUP_X2:
        top = stack.peek().getType();

        // Type 2 Values
        if (top.getSize() == 2) {
            b = stack.pop();
            a = stack.pop();

            stack.push(b);
            stack.push(a);
            stack.push(b);
            // Type 1 Values
        } else {
            c = stack.pop();
            b = stack.pop();
            a = stack.pop();

            stack.push(c);
            stack.push(a);
            stack.push(b);
            stack.push(c);
        }

        ordered.add(new StackOperation(StackOperation.Sort.DUP_X2));

        break;

    // Pops
    case Opcodes.POP:
        stack.pop();
        ordered.add(new StackOperation(StackOperation.Sort.POP));
        break;
    case Opcodes.POP2:
        top = stack.peek().getType();

        // Type 2 Values
        if (top.getSize() == 2) {
            stack.pop();
            // Type 1 Values
        } else {
            stack.pop();
            stack.pop();
        }

        ordered.add(new StackOperation(StackOperation.Sort.POP2));

        break;

    // TODO: DUP2_X1, DUP2_X2, MONITORENTER, MONITOREXIT
    case Opcodes.MONITORENTER:
        throw new RuntimeException("visitInsn: MONITORENTER");
    case Opcodes.MONITOREXIT:
        throw new RuntimeException("visitInsn: MONITOREXIT");
    case Opcodes.DUP2_X1:
        throw new RuntimeException("visitInsn: DUP2_X1");
    case Opcodes.DUP2_X2:
        throw new RuntimeException("visitInsn: DUP2_X2");
    default:
        throw new RuntimeException("visitInsn: " + opcode);
    }
}

From source file:Client.JClassPatcher.java

License:Open Source License

private void patchRenderer(ClassNode node) {
    Logger.Info("Patching renderer (" + node.name + ".class)");

    Iterator<MethodNode> methodNodeList = node.methods.iterator();
    while (methodNodeList.hasNext()) {
        MethodNode methodNode = methodNodeList.next();

        // Renderer present hook
        if (methodNode.desc.equals("(Ljava/awt/Graphics;III)V")) {
            AbstractInsnNode findNode = methodNode.instructions.getFirst();
            FieldInsnNode imageNode = null;

            while (findNode.getOpcode() != Opcodes.POP) {
                findNode = findNode.getNext();
                if (findNode == null) {
                    Logger.Error("Unable to find present hook");
                    break;
                }/*from   w ww.j  a v a2s  .  c o  m*/
            }

            while (findNode.getOpcode() != Opcodes.INVOKESPECIAL) {
                if (findNode.getOpcode() == Opcodes.GETFIELD)
                    imageNode = (FieldInsnNode) findNode;

                AbstractInsnNode prev = findNode.getPrevious();
                methodNode.instructions.remove(findNode);
                findNode = prev;
            }

            methodNode.instructions.insert(findNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "Game/Renderer",
                    "present", "(Ljava/awt/Graphics;Ljava/awt/Image;)V", false));
            methodNode.instructions.insert(findNode,
                    new FieldInsnNode(Opcodes.GETFIELD, node.name, imageNode.name, imageNode.desc));
            methodNode.instructions.insert(findNode, new VarInsnNode(Opcodes.ALOAD, 0));
            methodNode.instructions.insert(findNode, new VarInsnNode(Opcodes.ALOAD, 1));
        } else if (methodNode.name.equals("a") && methodNode.desc.equals("(IILjava/lang/String;IIBI)V")) {
            AbstractInsnNode start = methodNode.instructions.getFirst();
            while (start != null) {
                if (start.getOpcode() == Opcodes.ALOAD && start.getNext().getOpcode() == Opcodes.ILOAD
                        && start.getNext().getNext().getOpcode() == Opcodes.INVOKEVIRTUAL
                        && start.getNext().getNext().getNext().getOpcode() == Opcodes.ISTORE) {
                    break;
                }

                start = start.getNext();
            }
            start = start.getPrevious();

            LabelNode finishLabel = ((JumpInsnNode) start.getPrevious().getPrevious()).label;
            LabelNode failLabel = new LabelNode();

            methodNode.instructions.insertBefore(start, new IntInsnNode(Opcodes.BIPUSH, 126));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ALOAD, 3));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C"));
            methodNode.instructions.insertBefore(start, new JumpInsnNode(Opcodes.IF_ICMPNE, failLabel));

            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.ICONST_5));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.IADD));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ALOAD, 3));
            methodNode.instructions.insertBefore(start,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I"));
            methodNode.instructions.insertBefore(start, new JumpInsnNode(Opcodes.IF_ICMPGE, failLabel));

            methodNode.instructions.insertBefore(start, new IntInsnNode(Opcodes.BIPUSH, 126));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ALOAD, 3));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.ICONST_5));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.IADD));
            methodNode.instructions.insertBefore(start,
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C"));
            methodNode.instructions.insertBefore(start, new JumpInsnNode(Opcodes.IF_ICMPNE, failLabel));

            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ALOAD, 3));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.ICONST_1));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.IADD));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ILOAD, 10));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.ICONST_5));
            methodNode.instructions.insertBefore(start, new InsnNode(Opcodes.IADD));
            methodNode.instructions.insertBefore(start, new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
                    "java/lang/String", "substring", "(II)Ljava/lang/String;"));
            methodNode.instructions.insertBefore(start, new MethodInsnNode(Opcodes.INVOKESTATIC,
                    "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I"));
            methodNode.instructions.insertBefore(start, new VarInsnNode(Opcodes.ISTORE, 4));
            methodNode.instructions.insertBefore(start, new IincInsnNode(10, 5));

            methodNode.instructions.insertBefore(start, new JumpInsnNode(Opcodes.GOTO, finishLabel));

            methodNode.instructions.insertBefore(start, failLabel);
        }
    }
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitStoreResumed(MethodVisitor mv, boolean value) {
    mv.visitInsn(value ? Opcodes.ICONST_1 : Opcodes.ICONST_0);
    mv.visitVarInsn(Opcodes.ISTORE, lvarResumed);
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private void emitPreemptionPoint(MethodVisitor mv, int type) {
    mv.visitVarInsn(Opcodes.ALOAD, lvarStack);
    switch (type) {
    case 0:/*from   w  ww. j av  a  2s. c  o m*/
        mv.visitInsn(Opcodes.ICONST_0);
        break;
    case 1:
        mv.visitInsn(Opcodes.ICONST_1);
        break;
    case 2:
        mv.visitInsn(Opcodes.ICONST_2);
        break;
    case 3:
        mv.visitInsn(Opcodes.ICONST_3);
        break;
    default:
        throw new AssertionError("Unsupported type: " + type);
    }
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, STACK_NAME, "preemptionPoint", "(I)V");
}

From source file:com.android.tools.layoutlib.create.StubMethodAdapter.java

License:Apache License

private void generateInvoke() {
    /* Generates the code:
     *  OverrideMethod.invoke("signature", mIsNative ? true : false, null or this);
     *///from  w w w .  j ava  2 s .  c  o m
    mParentVisitor.visitLdcInsn(mInvokeSignature);
    // push true or false
    mParentVisitor.visitInsn(mIsNative ? Opcodes.ICONST_1 : Opcodes.ICONST_0);
    // push null or this
    if (mIsStatic) {
        mParentVisitor.visitInsn(Opcodes.ACONST_NULL);
    } else {
        mParentVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    }

    int sort = mReturnType != null ? mReturnType.getSort() : Type.VOID;
    switch (sort) {
    case Type.VOID:
        mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/android/tools/layoutlib/create/OverrideMethod", "invokeV",
                "(Ljava/lang/String;ZLjava/lang/Object;)V");
        mParentVisitor.visitInsn(Opcodes.RETURN);
        break;
    case Type.BOOLEAN:
    case Type.CHAR:
    case Type.BYTE:
    case Type.SHORT:
    case Type.INT:
        mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/android/tools/layoutlib/create/OverrideMethod", "invokeI",
                "(Ljava/lang/String;ZLjava/lang/Object;)I");
        switch (sort) {
        case Type.BOOLEAN:
            Label l1 = new Label();
            mParentVisitor.visitJumpInsn(Opcodes.IFEQ, l1);
            mParentVisitor.visitInsn(Opcodes.ICONST_1);
            mParentVisitor.visitInsn(Opcodes.IRETURN);
            mParentVisitor.visitLabel(l1);
            mParentVisitor.visitInsn(Opcodes.ICONST_0);
            break;
        case Type.CHAR:
            mParentVisitor.visitInsn(Opcodes.I2C);
            break;
        case Type.BYTE:
            mParentVisitor.visitInsn(Opcodes.I2B);
            break;
        case Type.SHORT:
            mParentVisitor.visitInsn(Opcodes.I2S);
            break;
        }
        mParentVisitor.visitInsn(Opcodes.IRETURN);
        break;
    case Type.LONG:
        mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/android/tools/layoutlib/create/OverrideMethod", "invokeL",
                "(Ljava/lang/String;ZLjava/lang/Object;)J");
        mParentVisitor.visitInsn(Opcodes.LRETURN);
        break;
    case Type.FLOAT:
        mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/android/tools/layoutlib/create/OverrideMethod", "invokeF",
                "(Ljava/lang/String;ZLjava/lang/Object;)F");
        mParentVisitor.visitInsn(Opcodes.FRETURN);
        break;
    case Type.DOUBLE:
        mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/android/tools/layoutlib/create/OverrideMethod", "invokeD",
                "(Ljava/lang/String;ZLjava/lang/Object;)D");
        mParentVisitor.visitInsn(Opcodes.DRETURN);
        break;
    case Type.ARRAY:
    case Type.OBJECT:
        mParentVisitor.visitMethodInsn(Opcodes.INVOKESTATIC,
                "com/android/tools/layoutlib/create/OverrideMethod", "invokeA",
                "(Ljava/lang/String;ZLjava/lang/Object;)Ljava/lang/Object;");
        mParentVisitor.visitTypeInsn(Opcodes.CHECKCAST, mReturnType.getInternalName());
        mParentVisitor.visitInsn(Opcodes.ARETURN);
        break;
    }

}

From source file:com.android.tools.lint.checks.AllowAllHostnameVerifierDetector.java

License:Apache License

@SuppressWarnings("rawtypes")
@Override/*www  . ja  v  a2 s .  c o m*/
public void checkClass(@NonNull final ClassContext context, @NonNull ClassNode classNode) {
    if (!classNode.interfaces.contains("javax/net/ssl/HostnameVerifier")) {
        return;
    }
    List methodList = classNode.methods;
    for (Object m : methodList) {
        MethodNode method = (MethodNode) m;
        if ("verify".equals(method.name)) {
            InsnList nodes = method.instructions;
            boolean emptyMethod = true; // Stays true if method has no instructions
                                        // other than ICONST_1 or IRETURN.
            boolean containsIconst1 = false;

            for (int i = 0, n = nodes.size(); i < n; i++) {
                // Future work: Improve this check to be less sensitive to irrelevant
                // instructions/statements/invocations (e.g. System.out.println).
                AbstractInsnNode instruction = nodes.get(i);
                int type = instruction.getType();
                if (type != AbstractInsnNode.LABEL && type != AbstractInsnNode.LINE
                        && !(type == AbstractInsnNode.INSN && (instruction.getOpcode() == Opcodes.ICONST_1
                                || instruction.getOpcode() == Opcodes.IRETURN))) {
                    emptyMethod = false;
                    break;
                } else if (type == AbstractInsnNode.INSN && instruction.getOpcode() == Opcodes.ICONST_1) {
                    containsIconst1 = true;
                }
            }
            if (containsIconst1 && emptyMethod) {
                Location location = context.getLocation(method, classNode);
                context.report(ISSUE, location,
                        method.name + " always returns true, which "
                                + "could cause insecure network traffic due to trusting TLS/SSL "
                                + "server certificates for wrong hostnames");
            }
        }
    }
}

From source file:com.android.tools.lint.checks.ApiDetector.java

License:Apache License

@SuppressWarnings("rawtypes") // ASM API
private static void checkSwitchBlock(ClassContext context, ClassNode classNode, FieldInsnNode field,
        MethodNode method, String name, String owner, int api, int minSdk) {
    // Switch statements on enums are tricky. The compiler will generate a method
    // which returns an array of the enum constants, indexed by their ordinal() values.
    // However, we only want to complain if the code is actually referencing one of
    // the non-available enum fields.
    ///*from  w  ww . jav a2  s. co m*/
    // For the android.graphics.PorterDuff.Mode enum for example, the first few items
    // in the array are populated like this:
    //
    //   L0
    //    ALOAD 0
    //    GETSTATIC android/graphics/PorterDuff$Mode.ADD : Landroid/graphics/PorterDuff$Mode;
    //    INVOKEVIRTUAL android/graphics/PorterDuff$Mode.ordinal ()I
    //    ICONST_1
    //    IASTORE
    //   L1
    //    GOTO L3
    //   L2
    //   FRAME FULL [[I] [java/lang/NoSuchFieldError]
    //    POP
    //   L3
    //   FRAME SAME
    //    ALOAD 0
    //    GETSTATIC android/graphics/PorterDuff$Mode.CLEAR : Landroid/graphics/PorterDuff$Mode;
    //    INVOKEVIRTUAL android/graphics/PorterDuff$Mode.ordinal ()I
    //    ICONST_2
    //    IASTORE
    //    ...
    // So if we for example find that the "ADD" field isn't accessible, since it requires
    // API 11, we need to
    //   (1) First find out what its ordinal number is. We can look at the following
    //       instructions to discover this; it's the "ICONST_1" and "IASTORE" instructions.
    //       (After ICONST_5 it moves on to BIPUSH 6, BIPUSH 7, etc.)
    //   (2) Find the corresponding *usage* of this switch method. For the above enum,
    //       the switch ordinal lookup method will be called
    //         "$SWITCH_TABLE$android$graphics$PorterDuff$Mode" with desc "()[I".
    //       This means we will be looking for an invocation in some other method which looks
    //       like this:
    //         INVOKESTATIC (current class).$SWITCH_TABLE$android$graphics$PorterDuff$Mode ()[I
    //       (obviously, it can be invoked more than once)
    //       Note that it can be used more than once in this class and all sites should be
    //       checked!
    //   (3) Look up the corresponding table switch, which should look something like this:
    //        INVOKESTATIC (current class).$SWITCH_TABLE$android$graphics$PorterDuff$Mode ()[I
    //        ALOAD 0
    //        INVOKEVIRTUAL android/graphics/PorterDuff$Mode.ordinal ()I
    //        IALOAD
    //        LOOKUPSWITCH
    //          2: L1
    //          11: L2
    //          default: L3
    //       Here we need to see if the LOOKUPSWITCH instruction is referencing our target
    //       case. Above we were looking for the "ADD" case which had ordinal 1. Since this
    //       isn't explicitly referenced, we can ignore this field reference.
    AbstractInsnNode next = field.getNext();
    if (next == null || next.getOpcode() != Opcodes.INVOKEVIRTUAL) {
        return;
    }
    next = next.getNext();
    if (next == null) {
        return;
    }
    int ordinal;
    switch (next.getOpcode()) {
    case Opcodes.ICONST_0:
        ordinal = 0;
        break;
    case Opcodes.ICONST_1:
        ordinal = 1;
        break;
    case Opcodes.ICONST_2:
        ordinal = 2;
        break;
    case Opcodes.ICONST_3:
        ordinal = 3;
        break;
    case Opcodes.ICONST_4:
        ordinal = 4;
        break;
    case Opcodes.ICONST_5:
        ordinal = 5;
        break;
    case Opcodes.BIPUSH: {
        IntInsnNode iin = (IntInsnNode) next;
        ordinal = iin.operand;
        break;
    }
    default:
        return;
    }

    // Find usages of this call site
    List methodList = classNode.methods;
    for (Object m : methodList) {
        InsnList nodes = ((MethodNode) m).instructions;
        for (int i = 0, n = nodes.size(); i < n; i++) {
            AbstractInsnNode instruction = nodes.get(i);
            if (instruction.getOpcode() != Opcodes.INVOKESTATIC) {
                continue;
            }
            MethodInsnNode node = (MethodInsnNode) instruction;
            if (node.name.equals(method.name) && node.desc.equals(method.desc)
                    && node.owner.equals(classNode.name)) {
                // Find lookup switch
                AbstractInsnNode target = getNextInstruction(node);
                while (target != null) {
                    if (target.getOpcode() == Opcodes.LOOKUPSWITCH) {
                        LookupSwitchInsnNode lookup = (LookupSwitchInsnNode) target;
                        @SuppressWarnings("unchecked") // ASM API
                        List<Integer> keys = lookup.keys;
                        if (keys != null && keys.contains(ordinal)) {
                            String fqcn = ClassContext.getFqcn(owner) + '#' + name;
                            String message = String.format(
                                    "Enum value requires API level %1$d " + "(current min is %2$d): %3$s", api,
                                    minSdk, fqcn);
                            report(context, message, lookup, (MethodNode) m, name, null,
                                    SearchHints.create(FORWARD).matchJavaSymbol());

                            // Break out of the inner target search only; the switch
                            // statement could be used in other places in this class as
                            // well and we want to report all problematic usages.
                            break;
                        }
                    }
                    target = getNextInstruction(target);
                }
            }
        }
    }
}

From source file:com.android.tools.lint.checks.SecureRandomGeneratorDetector.java

License:Apache License

@Override
public void checkCall(@NonNull ClassContext context, @NonNull ClassNode classNode, @NonNull MethodNode method,
        @NonNull MethodInsnNode call) {/*from   w  w  w  . j  av  a 2s . co m*/
    if (mIgnore) {
        return;
    }

    String owner = call.owner;
    String name = call.name;

    // Look for the workaround code: if we see a Class.forName on the harmony NativeCrypto,
    // we'll consider that a sign.

    if (name.equals(FOR_NAME)) {
        if (call.getOpcode() != Opcodes.INVOKESTATIC || !owner.equals(JAVA_LANG_CLASS)) {
            return;
        }
        AbstractInsnNode prev = LintUtils.getPrevInstruction(call);
        if (prev instanceof LdcInsnNode) {
            Object cst = ((LdcInsnNode) prev).cst;
            //noinspection SpellCheckingInspection
            if (cst instanceof String && "org.apache.harmony.xnet.provider.jsse.NativeCrypto".equals(cst)) {
                mIgnore = true;
            }
        }
        return;
    }

    // Look for calls that probably require a properly initialized random number generator.
    assert owner.equals(JAVAX_CRYPTO_KEY_GENERATOR) || owner.equals(JAVA_SECURITY_KEY_PAIR_GENERATOR)
            || owner.equals(JAVAX_CRYPTO_KEY_AGREEMENT) || owner.equals(OWNER_SECURE_RANDOM)
            || owner.equals(JAVAX_CRYPTO_CIPHER) || owner.equals(JAVAX_CRYPTO_SIGNATURE)
            || owner.equals(JAVAX_NET_SSL_SSLENGINE) : owner;
    boolean warn = false;

    if (owner.equals(JAVAX_CRYPTO_SIGNATURE)) {
        warn = name.equals(INIT_SIGN);
    } else if (owner.equals(JAVAX_CRYPTO_CIPHER)) {
        if (name.equals(INIT)) {
            int arity = getDescArity(call.desc);
            AbstractInsnNode node = call;
            for (int i = 0; i < arity; i++) {
                node = LintUtils.getPrevInstruction(node);
                if (node == null) {
                    break;
                }
            }
            if (node != null) {
                int opcode = node.getOpcode();
                if (opcode == Opcodes.ICONST_3 || // Cipher.WRAP_MODE
                        opcode == Opcodes.ICONST_1) { // Cipher.ENCRYPT_MODE
                    warn = true;
                }
            }
        }
    } else if (name.equals(GET_INSTANCE) || name.equals(CONSTRUCTOR_NAME) || name.equals(WRAP)
            || name.equals(UNWRAP)) { // For SSLEngine
        warn = true;
    }

    if (warn) {
        if (mLocation != null) {
            return;
        }
        if (context.getMainProject().getMinSdk() > 18) {
            // Fix no longer needed
            mIgnore = true;
            return;
        }

        if (context.getDriver().isSuppressed(ISSUE, classNode, method, call)) {
            mIgnore = true;
        } else {
            mLocation = context.getLocation(call);
        }
    }
}

From source file:com.codename1.tools.translator.BytecodeMethod.java

License:Open Source License

boolean optimize() {
    int instructionCount = instructions.size();

    // optimize away a method that only contains the void return instruction e.g. blank constructors etc.
    if (instructionCount < 6) {
        int realCount = instructionCount;
        Instruction actual = null;/*from   w  ww  .ja va 2 s.  c  o m*/
        for (int iter = 0; iter < instructionCount; iter++) {
            Instruction current = instructions.get(iter);
            if (current instanceof LabelInstruction) {
                realCount--;
                continue;
            }
            if (current instanceof LineNumber) {
                realCount--;
                continue;
            }
            actual = current;
        }

        if (realCount == 1 && actual != null && actual.getOpcode() == Opcodes.RETURN) {
            return false;
        }
    }

    boolean astoreCalls = false;
    boolean hasInstructions = false;

    boolean hasTryCatch = false;
    for (int iter = 0; iter < instructionCount - 1; iter++) {
        Instruction current = instructions.get(iter);
        if (current instanceof TryCatch) {
            hasTryCatch = true;
        }
        current.setMethod(this);
        if (current.isOptimized()) {
            continue;
        }
        int currentOpcode = current.getOpcode();
        switch (currentOpcode) {
        case Opcodes.CHECKCAST: {
            // Remove the check cast for now as it gets in the way of other optimizations
            instructions.remove(iter);
            iter--;
            instructionCount--;
            break;
        }
        }
    }

    for (int iter = 0; iter < instructionCount - 1; iter++) {
        Instruction current = instructions.get(iter);
        if (current.isOptimized()) {
            // This instruction has already been optimized
            // we should skip it and proceed to the next one
            continue;
        }
        Instruction next = instructions.get(iter + 1);

        int currentOpcode = current.getOpcode();
        int nextOpcode = next.getOpcode();

        if (ArithmeticExpression.isArithmeticOp(current)) {
            int addedIndex = ArithmeticExpression.tryReduce(instructions, iter);
            if (addedIndex >= 0) {
                iter = addedIndex;
                instructionCount = instructions.size();
                continue;
            }
        }

        if (current instanceof Field) {
            int newIter = Field.tryReduce(instructions, iter);
            if (newIter >= 0) {
                iter = newIter;
                instructionCount = instructions.size();
                continue;
            }
        }

        switch (currentOpcode) {

        case Opcodes.ARRAYLENGTH: {
            if (!dependentClasses.contains("java_lang_NullPointerException")) {
                dependentClasses.add("java_lang_NullPointerException");
            }
            int newIter = ArrayLengthExpression.tryReduce(instructions, iter);
            if (newIter >= 0) {
                instructionCount = instructions.size();
                iter = newIter;
                continue;
            }
            break;
        }

        case Opcodes.DUP: {
            int newIter = DupExpression.tryReduce(instructions, iter);
            if (newIter >= 0) {
                iter = newIter;
                instructionCount = instructions.size();
                continue;
            }
            break;
        }

        case Opcodes.POP: {
            if (iter > 0) {
                Instruction prev = instructions.get(iter - 1);
                if (prev instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) prev;
                    if (inv.methodHasReturnValue()) {
                        inv.setNoReturn(true);
                        instructions.remove(iter);
                        iter--;
                        instructionCount--;
                        continue;
                    }
                }
            }
            break;
        }

        case Opcodes.ASTORE:
        case Opcodes.ISTORE:
        case Opcodes.DSTORE:
        case Opcodes.LSTORE:
        case Opcodes.FSTORE: {
            if (iter > 0 && current instanceof VarOp) {
                VarOp currentVarOp = (VarOp) current;
                Instruction prev = instructions.get(iter - 1);
                if (prev instanceof AssignableExpression) {
                    AssignableExpression expr = (AssignableExpression) prev;
                    StringBuilder sb = new StringBuilder();
                    if (currentVarOp.assignFrom(expr, sb)) {
                        instructions.remove(iter - 1);
                        instructions.remove(iter - 1);
                        instructions.add(iter - 1,
                                new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
                        iter = iter - 1;
                        instructionCount = instructions.size();
                        continue;
                    }

                } else if (prev instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) prev;
                    StringBuilder sb = new StringBuilder();
                    if (currentVarOp.assignFrom(inv, sb)) {
                        instructions.remove(iter - 1);
                        instructions.remove(iter - 1);
                        instructions.add(iter - 1,
                                new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
                        iter = iter - 1;
                        instructionCount = instructions.size();
                        continue;
                    }
                }
            }

            break;
        }

        case Opcodes.IRETURN:
        case Opcodes.FRETURN:
        case Opcodes.ARETURN:
        case Opcodes.LRETURN:
        case Opcodes.DRETURN: {
            if (iter > 0 && current instanceof BasicInstruction) {
                Instruction prev = instructions.get(iter - 1);
                if (prev instanceof AssignableExpression) {
                    AssignableExpression expr = (AssignableExpression) prev;
                    StringBuilder sb = new StringBuilder();
                    if (expr.assignTo(null, sb)) {
                        instructions.remove(iter - 1);
                        instructions.remove(iter - 1);
                        String exprString = sb.toString().trim();
                        String retVal = exprString;
                        sb.setLength(0);
                        if (!prev.isConstant()) {
                            sb.append("\n{\n    ");
                            switch (currentOpcode) {
                            case Opcodes.IRETURN:
                                sb.append("JAVA_INT");
                                break;
                            case Opcodes.FRETURN:
                                sb.append("JAVA_FLOAT");
                                break;
                            case Opcodes.ARETURN:
                                sb.append("JAVA_OBJECT");
                                break;
                            case Opcodes.LRETURN:
                                sb.append("JAVA_LONG");
                                break;
                            case Opcodes.DRETURN:
                                sb.append("JAVA_DOUBLE");
                                break;
                            }
                            sb.append(" ___returnValue=").append(exprString).append(";\n");
                            retVal = "___returnValue";
                        }
                        if (synchronizedMethod) {
                            if (staticMethod) {
                                sb.append("    monitorExit(threadStateData, (JAVA_OBJECT)&class__");
                                sb.append(getClsName());
                                sb.append(");\n");
                            } else {
                                sb.append("    monitorExit(threadStateData, __cn1ThisObject);\n");
                            }
                        }
                        if (hasTryCatch) {
                            sb.append(
                                    "    releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); return ")
                                    .append(retVal).append(";\n");
                        } else {
                            sb.append("    releaseForReturn(threadStateData, cn1LocalsBeginInThread); return ")
                                    .append(retVal).append(";\n");
                        }
                        if (!prev.isConstant()) {
                            sb.append("}\n");
                        }

                        instructions.add(iter - 1,
                                new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
                        iter--;
                        instructionCount = instructions.size();
                        continue;

                    }
                } else if (prev instanceof CustomInvoke) {

                    CustomInvoke expr = (CustomInvoke) prev;
                    String returnType = expr.getReturnValue();
                    if (returnType != null && !"JAVA_OBJECT".equals(returnType)) {
                        // We can't safely return a JAVA_OBJECT directly because it needs to be added 
                        // to the stack for the GC
                        StringBuilder sb = new StringBuilder();
                        if (expr.appendExpression(sb)) {
                            instructions.remove(iter - 1);
                            instructions.remove(iter - 1);
                            String exprString = sb.toString().trim();
                            String retVal = exprString;
                            sb.setLength(0);
                            if (!expr.isConstant()) {

                                sb.append("\n{\n    ");
                                switch (currentOpcode) {
                                case Opcodes.IRETURN:
                                    sb.append("JAVA_INT");
                                    break;
                                case Opcodes.FRETURN:
                                    sb.append("JAVA_FLOAT");
                                    break;
                                case Opcodes.ARETURN:
                                    sb.append("JAVA_OBJECT");
                                    break;
                                case Opcodes.LRETURN:
                                    sb.append("JAVA_LONG");
                                    break;
                                case Opcodes.DRETURN:
                                    sb.append("JAVA_DOUBLE");
                                    break;
                                }

                                sb.append(" ___returnValue=").append(exprString).append(";\n");
                                retVal = "___returnValue";
                            }
                            if (synchronizedMethod) {
                                if (staticMethod) {
                                    sb.append("    monitorExit(threadStateData, (JAVA_OBJECT)&class__");
                                    sb.append(getClsName());
                                    sb.append(");\n");
                                } else {
                                    sb.append("    monitorExit(threadStateData, __cn1ThisObject);\n");
                                }
                            }
                            if (hasTryCatch) {
                                sb.append(
                                        "    releaseForReturnInException(threadStateData, cn1LocalsBeginInThread, methodBlockOffset); return ")
                                        .append(retVal).append(";\n");
                            } else {
                                sb.append(
                                        "    releaseForReturn(threadStateData, cn1LocalsBeginInThread); return ")
                                        .append(retVal).append(";\n");
                            }
                            if (!expr.isConstant()) {
                                sb.append("}\n");
                            }

                            instructions.add(iter - 1,
                                    new CustomIntruction(sb.toString(), sb.toString(), dependentClasses));
                            iter--;
                            instructionCount = instructions.size();
                            continue;

                        }
                    }
                }
            }
            break;
        }

        case Opcodes.BASTORE:
        case Opcodes.SASTORE:
        case Opcodes.CASTORE:
        case Opcodes.AASTORE:
        case Opcodes.IASTORE:
        case Opcodes.DASTORE:
        case Opcodes.LASTORE:
        case Opcodes.FASTORE: {
            if (iter > 2 && current instanceof BasicInstruction) {
                StringBuilder devNull = new StringBuilder();
                String arrayLiteral = null;
                String indexLiteral = null;
                String valueLiteral = null;
                Instruction prev3 = instructions.get(iter - 3);
                if (prev3 instanceof AssignableExpression) {
                    if (((AssignableExpression) prev3).assignTo(null, devNull)) {
                        arrayLiteral = devNull.toString().trim();

                    }
                }
                devNull.setLength(0);
                Instruction prev2 = instructions.get(iter - 2);
                if (prev2 instanceof AssignableExpression) {
                    if (((AssignableExpression) prev2).assignTo(null, devNull)) {
                        indexLiteral = devNull.toString().trim();
                    }
                }
                devNull.setLength(0);
                Instruction prev1 = instructions.get(iter - 1);

                if (prev1 instanceof AssignableExpression) {
                    if (((AssignableExpression) prev1).assignTo(null, devNull)) {
                        valueLiteral = devNull.toString().trim();
                    }
                } else if (prev1 instanceof CustomInvoke) {
                    devNull.setLength(0);
                    if (((CustomInvoke) prev1).appendExpression(devNull)) {
                        valueLiteral = devNull.toString().trim();
                    }
                }

                if (arrayLiteral != null && indexLiteral != null && valueLiteral != null) {
                    String elementType = null;
                    switch (current.getOpcode()) {
                    case Opcodes.AASTORE:
                        elementType = "OBJECT";
                        break;
                    case Opcodes.IASTORE:
                        elementType = "INT";
                        break;
                    case Opcodes.DASTORE:
                        elementType = "DOUBLE";
                        break;

                    case Opcodes.LASTORE:
                        elementType = "LONG";
                        break;
                    case Opcodes.FASTORE:
                        elementType = "FLOAT";
                        break;
                    case Opcodes.CASTORE:
                        elementType = "CHAR";
                        break;
                    case Opcodes.BASTORE:
                        elementType = "BYTE";
                        break;
                    case Opcodes.SASTORE:
                        elementType = "SHORT";
                        break;

                    }
                    if (elementType == null) {
                        break;
                    }

                    instructions.remove(iter - 3);
                    instructions.remove(iter - 3);
                    instructions.remove(iter - 3);
                    instructions.remove(iter - 3);
                    String code = "    CN1_SET_ARRAY_ELEMENT_" + elementType + "(" + arrayLiteral + ", "
                            + indexLiteral + ", " + valueLiteral + ");\n";
                    instructions.add(iter - 3, new CustomIntruction(code, code, dependentClasses));
                    iter = iter - 3;
                    instructionCount = instructions.size();
                    continue;
                }
            }

            break;
        }

        case Opcodes.FALOAD:
        case Opcodes.BALOAD:
        case Opcodes.IALOAD:
        case Opcodes.LALOAD:
        case Opcodes.DALOAD:
        case Opcodes.AALOAD:
        case Opcodes.SALOAD:
        case Opcodes.CALOAD: {
            int newIter = ArrayLoadExpression.tryReduce(instructions, iter);
            if (newIter >= 0) {
                iter = newIter;
                instructionCount = instructions.size();
                continue;
            }
            break;
        }

        /* Try to optimize if statements that just use constants
           and local variables so that they don't need the intermediate
           push and pop from the stack.
        */
        case Opcodes.IF_ACMPEQ:
        case Opcodes.IF_ACMPNE:
        case Opcodes.IF_ICMPLE:
        case Opcodes.IF_ICMPLT:
        case Opcodes.IF_ICMPNE:
        case Opcodes.IF_ICMPGT:
        case Opcodes.IF_ICMPEQ:
        case Opcodes.IF_ICMPGE: {

            if (iter > 1) {
                Instruction leftArg = instructions.get(iter - 2);
                Instruction rightArg = instructions.get(iter - 1);

                String leftLiteral = null;
                String rightLiteral = null;

                if (leftArg instanceof AssignableExpression) {
                    StringBuilder sb = new StringBuilder();
                    if (((AssignableExpression) leftArg).assignTo(null, sb)) {
                        leftLiteral = sb.toString().trim();
                    }
                } else if (leftArg instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) leftArg;
                    StringBuilder sb = new StringBuilder();
                    if (!"JAVA_OBJECT".equals(inv.getReturnValue()) && inv.appendExpression(sb)) {
                        leftLiteral = sb.toString().trim();
                    }
                }
                if (rightArg instanceof AssignableExpression) {
                    StringBuilder sb = new StringBuilder();
                    if (((AssignableExpression) rightArg).assignTo(null, sb)) {
                        rightLiteral = sb.toString().trim();
                    }
                } else if (rightArg instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) rightArg;
                    StringBuilder sb = new StringBuilder();
                    if (!"JAVA_OBJECT".equals(inv.getReturnValue()) && inv.appendExpression(sb)) {
                        rightLiteral = sb.toString().trim();
                    }
                }

                if (rightLiteral != null && leftLiteral != null) {
                    Jump jmp = (Jump) current;
                    instructions.remove(iter - 2);
                    instructions.remove(iter - 2);
                    instructions.remove(iter - 2);
                    //instructions.remove(iter-2);
                    iter -= 2;
                    //instructionCount -= 2;
                    StringBuilder sb = new StringBuilder();
                    String operator = null;
                    String opName = null;
                    switch (currentOpcode) {
                    case Opcodes.IF_ICMPLE:
                        operator = "<=";
                        opName = "IF_ICMPLE";
                        break;
                    case Opcodes.IF_ICMPLT:
                        operator = "<";
                        opName = "IF_IMPLT";
                        break;
                    case Opcodes.IF_ICMPNE:
                        operator = "!=";
                        opName = "IF_ICMPNE";
                        break;
                    case Opcodes.IF_ICMPGT:
                        operator = ">";
                        opName = "IF_ICMPGT";
                        break;
                    case Opcodes.IF_ICMPGE:
                        operator = ">=";
                        opName = "IF_ICMPGE";
                        break;
                    case Opcodes.IF_ICMPEQ:
                        operator = "==";
                        opName = "IF_ICMPEQ";
                        break;
                    case Opcodes.IF_ACMPEQ:
                        operator = "==";
                        opName = "IF_ACMPEQ";
                        break;
                    case Opcodes.IF_ACMPNE:
                        operator = "!=";
                        opName = "IF_ACMPNE";
                        break;
                    default:
                        throw new RuntimeException(
                                "Invalid operator during optimization of integer comparison");
                    }

                    sb.append("if (").append(leftLiteral).append(operator).append(rightLiteral).append(") /* ")
                            .append(opName).append(" CustomJump */ ");
                    CustomJump newJump = CustomJump.create(jmp, sb.toString());
                    //jmp.setCustomCompareCode(sb.toString());
                    newJump.setOptimized(true);
                    instructions.add(iter, newJump);
                    instructionCount = instructions.size();

                }

            }
            break;
        }
        case Opcodes.IFNONNULL:
        case Opcodes.IFNULL:

        case Opcodes.IFLE:
        case Opcodes.IFLT:
        case Opcodes.IFNE:
        case Opcodes.IFGT:
        case Opcodes.IFEQ:
        case Opcodes.IFGE: {
            String rightArg = "0";
            if (currentOpcode == Opcodes.IFNONNULL || currentOpcode == Opcodes.IFNULL) {
                rightArg = "JAVA_NULL";
            }
            if (iter > 0) {
                Instruction leftArg = instructions.get(iter - 1);

                String leftLiteral = null;

                if (leftArg instanceof AssignableExpression) {
                    StringBuilder sb = new StringBuilder();
                    if (((AssignableExpression) leftArg).assignTo(null, sb)) {
                        leftLiteral = sb.toString().trim();
                    }
                } else if (leftArg instanceof CustomInvoke) {
                    CustomInvoke inv = (CustomInvoke) leftArg;
                    StringBuilder sb = new StringBuilder();
                    if (inv.appendExpression(sb)) {
                        leftLiteral = sb.toString().trim();
                    }
                }

                if (leftLiteral != null) {
                    Jump jmp = (Jump) current;
                    instructions.remove(iter - 1);
                    instructions.remove(iter - 1);
                    //instructions.remove(iter-2);
                    iter -= 1;
                    //instructionCount -= 2;
                    StringBuilder sb = new StringBuilder();
                    String operator = null;
                    String opName = null;
                    switch (currentOpcode) {
                    case Opcodes.IFLE:
                        operator = "<=";
                        opName = "IFLE";
                        break;
                    case Opcodes.IFLT:
                        operator = "<";
                        opName = "IFLT";
                        break;
                    case Opcodes.IFNE:
                        operator = "!=";
                        opName = "IFNE";
                        break;
                    case Opcodes.IFGT:
                        operator = ">";
                        opName = "IFGT";
                        break;
                    case Opcodes.IFGE:
                        operator = ">=";
                        opName = "IFGE";
                        break;
                    case Opcodes.IFEQ:
                        operator = "==";
                        opName = "IFEQ";
                        break;
                    case Opcodes.IFNULL:
                        operator = "==";
                        opName = "IFNULL";
                        break;
                    case Opcodes.IFNONNULL:
                        operator = "!=";
                        opName = "IFNONNULL";
                        break;
                    default:
                        throw new RuntimeException(
                                "Invalid operator during optimization of integer comparison");
                    }

                    sb.append("if (").append(leftLiteral).append(operator).append(rightArg).append(") /* ")
                            .append(opName).append(" CustomJump */ ");
                    CustomJump newJump = CustomJump.create(jmp, sb.toString());
                    //jmp.setCustomCompareCode(sb.toString());
                    newJump.setOptimized(true);
                    instructions.add(iter, newJump);
                    instructionCount = instructions.size();

                }

            }
            break;
        }

        case Opcodes.INVOKEVIRTUAL:
        case Opcodes.INVOKESTATIC:
        case Opcodes.INVOKESPECIAL:
        case Opcodes.INVOKEINTERFACE: {
            if (current instanceof Invoke) {
                Invoke inv = (Invoke) current;
                List<ByteCodeMethodArg> invocationArgs = inv.getArgs();
                int numArgs = invocationArgs.size();

                //if (current.getOpcode() != Opcodes.INVOKESTATIC) {
                //    numArgs++;
                //}
                if (iter >= numArgs) {
                    String[] argLiterals = new String[numArgs];
                    StringBuilder devNull = new StringBuilder();
                    for (int i = 0; i < numArgs; i++) {
                        devNull.setLength(0);
                        Instruction instr = instructions.get(iter - numArgs + i);
                        if (instr instanceof AssignableExpression
                                && ((AssignableExpression) instr).assignTo(null, devNull)) {
                            argLiterals[i] = devNull.toString().trim();
                        } else if (instr instanceof CustomInvoke) {
                            CustomInvoke cinv = (CustomInvoke) instr;
                            devNull.setLength(0);
                            if (!"JAVA_OBJECT".equals(cinv.getReturnValue())
                                    && cinv.appendExpression(devNull)) {
                                // We can't add invocations that return objects directly
                                // because they need to be added to the stack for GC
                                argLiterals[i] = devNull.toString().trim();
                            }
                        } else if (instr instanceof ArithmeticExpression) {
                            argLiterals[i] = ((ArithmeticExpression) instr).getExpressionAsString().trim();
                        } else if (instr instanceof VarOp) {
                            VarOp var = (VarOp) instr;
                            switch (instr.getOpcode()) {
                            case Opcodes.ALOAD: {
                                if (!isStatic() && var.getIndex() == 0) {
                                    argLiterals[i] = "__cn1ThisObject";
                                } else {
                                    argLiterals[i] = "locals[" + var.getIndex() + "].data.o";
                                }
                                break;
                            }
                            case Opcodes.ILOAD: {
                                argLiterals[i] = "ilocals_" + var.getIndex() + "_";
                                break;
                            }
                            case Opcodes.ACONST_NULL: {
                                argLiterals[i] = "JAVA_NULL";
                                break;
                            }
                            case Opcodes.DLOAD: {
                                argLiterals[i] = "dlocals_" + var.getIndex() + "_";
                                break;
                            }
                            case Opcodes.FLOAD: {
                                argLiterals[i] = "flocals_" + var.getIndex() + "_";
                                break;
                            }
                            case Opcodes.LLOAD: {
                                argLiterals[i] = "llocals_" + var.getIndex() + "_";
                                break;
                            }
                            case Opcodes.ICONST_0: {
                                argLiterals[i] = "0";
                                break;
                            }
                            case Opcodes.ICONST_1: {
                                argLiterals[i] = "1";
                                break;
                            }
                            case Opcodes.ICONST_2: {
                                argLiterals[i] = "2";
                                break;
                            }
                            case Opcodes.ICONST_3: {
                                argLiterals[i] = "3";
                                break;
                            }
                            case Opcodes.ICONST_4: {
                                argLiterals[i] = "4";
                                break;
                            }
                            case Opcodes.ICONST_5: {
                                argLiterals[i] = "5";
                                break;
                            }
                            case Opcodes.ICONST_M1: {
                                argLiterals[i] = "-1";
                                break;
                            }
                            case Opcodes.LCONST_0: {
                                argLiterals[i] = "(JAVA_LONG)0";
                                break;
                            }
                            case Opcodes.LCONST_1: {
                                argLiterals[i] = "(JAVA_LONG)1";
                                break;
                            }
                            case Opcodes.BIPUSH:
                            case Opcodes.SIPUSH: {
                                argLiterals[i] = String.valueOf(var.getIndex());

                                break;
                            }
                            }
                        } else {
                            switch (instr.getOpcode()) {

                            case Opcodes.ACONST_NULL: {
                                argLiterals[i] = "JAVA_NULL";
                                break;
                            }

                            case Opcodes.ICONST_0: {
                                argLiterals[i] = "0";
                                break;
                            }
                            case Opcodes.ICONST_1: {
                                argLiterals[i] = "1";
                                break;
                            }
                            case Opcodes.ICONST_2: {
                                argLiterals[i] = "2";
                                break;
                            }
                            case Opcodes.ICONST_3: {
                                argLiterals[i] = "3";
                                break;
                            }
                            case Opcodes.ICONST_4: {
                                argLiterals[i] = "4";
                                break;
                            }
                            case Opcodes.ICONST_5: {
                                argLiterals[i] = "5";
                                break;
                            }
                            case Opcodes.ICONST_M1: {
                                argLiterals[i] = "-1";
                                break;
                            }
                            case Opcodes.LCONST_0: {
                                argLiterals[i] = "(JAVA_LONG)0";
                                break;
                            }
                            case Opcodes.LCONST_1: {
                                argLiterals[i] = "(JAVA_LONG)1";
                                break;
                            }
                            case Opcodes.BIPUSH: {
                                if (instr instanceof BasicInstruction) {
                                    argLiterals[i] = String.valueOf(((BasicInstruction) instr).getValue());
                                }
                                break;
                            }
                            case Opcodes.LDC: {
                                if (instr instanceof Ldc) {
                                    Ldc ldc = (Ldc) instr;
                                    argLiterals[i] = ldc.getValueAsString();

                                }
                                break;
                            }

                            }

                        }
                    }

                    // Check to make sure that we have all the args as literals.
                    boolean missingLiteral = false;
                    for (String lit : argLiterals) {
                        if (lit == null) {
                            missingLiteral = true;
                            break;
                        }
                    }

                    // We have all of the arguments as literals.  Let's
                    // add them to our invoke instruction.
                    if (!missingLiteral) {
                        CustomInvoke newInvoke = CustomInvoke.create(inv);
                        instructions.remove(iter);
                        instructions.add(iter, newInvoke);
                        int newIter = iter;
                        for (int i = 0; i < numArgs; i++) {
                            instructions.remove(iter - numArgs);
                            newIter--;
                            newInvoke.setLiteralArg(i, argLiterals[i]);
                        }
                        if (inv.getOpcode() != Opcodes.INVOKESTATIC) {
                            Instruction ldTarget = instructions.get(iter - numArgs - 1);
                            if (ldTarget instanceof AssignableExpression) {
                                StringBuilder targetExprStr = new StringBuilder();
                                if (((AssignableExpression) ldTarget).assignTo(null, targetExprStr)) {
                                    newInvoke.setTargetObjectLiteral(targetExprStr.toString().trim());
                                    instructions.remove(iter - numArgs - 1);
                                    newIter--;

                                }

                            } else if (ldTarget instanceof CustomInvoke) {
                                // WE Can't pass a custom invoke as the target directly
                                // because it the return value needs to be added to the 
                                // stack for the GC
                            } else {
                                switch (ldTarget.getOpcode()) {
                                case Opcodes.ALOAD: {
                                    VarOp v = (VarOp) ldTarget;
                                    if (isStatic() && v.getIndex() == 0) {
                                        newInvoke.setTargetObjectLiteral("__cn1ThisObject");
                                    } else {
                                        newInvoke.setTargetObjectLiteral("locals[" + v.getIndex() + "].data.o");
                                    }
                                    instructions.remove(iter - numArgs - 1);
                                    newIter--;
                                    break;
                                }
                                }
                            }
                        }

                        newInvoke.setOptimized(true);
                        //iter = 0;
                        instructionCount = instructions.size();
                        iter = newIter;

                    }
                }
            }
            break;
        }

        }
        astoreCalls = astoreCalls || currentOpcode == Opcodes.ASTORE || currentOpcode == Opcodes.ISTORE
                || currentOpcode == Opcodes.LSTORE || currentOpcode == Opcodes.DSTORE
                || currentOpcode == Opcodes.FSTORE;

        hasInstructions = hasInstructions | current.getOpcode() != -1;
    }
    return hasInstructions;
}