Example usage for org.objectweb.asm Opcodes AASTORE

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

Introduction

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

Prototype

int AASTORE

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

Click Source Link

Usage

From source file:com.google.test.metric.asm.MethodVisitorBuilder.java

License:Apache License

public void visitInsn(final int opcode) {
    switch (opcode) {
    case Opcodes.ACONST_NULL:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Load(lineNumber, new Constant(null, JavaType.OBJECT)));
            }// w w w  .  j  av a2s . c o  m
        });
        break;
    case Opcodes.ICONST_M1:
    case Opcodes.ICONST_0:
    case Opcodes.ICONST_1:
    case Opcodes.ICONST_2:
    case Opcodes.ICONST_3:
    case Opcodes.ICONST_4:
    case Opcodes.ICONST_5:
        loadConstant(opcode - Opcodes.ICONST_M1 - 1, JavaType.INT);
        break;
    case Opcodes.LCONST_0:
    case Opcodes.LCONST_1:
        loadConstant(opcode - Opcodes.LCONST_0, JavaType.LONG);
        break;
    case Opcodes.FCONST_0:
    case Opcodes.FCONST_1:
    case Opcodes.FCONST_2:
        loadConstant(opcode - Opcodes.FCONST_0, JavaType.FLOAT);
        break;
    case Opcodes.DCONST_0:
    case Opcodes.DCONST_1:
        loadConstant(opcode - Opcodes.DCONST_0, JavaType.DOUBLE);
        break;
    case Opcodes.IALOAD:
        recordArrayLoad(JavaType.INT);
        break;
    case Opcodes.LALOAD:
        recordArrayLoad(JavaType.LONG);
        break;
    case Opcodes.FALOAD:
        recordArrayLoad(JavaType.FLOAT);
        break;
    case Opcodes.DALOAD:
        recordArrayLoad(JavaType.DOUBLE);
        break;
    case Opcodes.AALOAD:
        recordArrayLoad(JavaType.OBJECT);
        break;
    case Opcodes.BALOAD:
        recordArrayLoad(JavaType.BYTE);
        break;
    case Opcodes.CALOAD:
        recordArrayLoad(JavaType.CHAR);
        break;
    case Opcodes.SALOAD:
        recordArrayLoad(JavaType.SHORT);
        break;

    case Opcodes.IASTORE:
        recordArrayStore(JavaType.INT);
        break;
    case Opcodes.LASTORE:
        recordArrayStore(JavaType.LONG);
        break;
    case Opcodes.FASTORE:
        recordArrayStore(JavaType.FLOAT);
        break;
    case Opcodes.DASTORE:
        recordArrayStore(JavaType.DOUBLE);
        break;
    case Opcodes.AASTORE:
        recordArrayStore(JavaType.OBJECT);
        break;
    case Opcodes.BASTORE:
        recordArrayStore(JavaType.BYTE);
        break;
    case Opcodes.CASTORE:
        recordArrayStore(JavaType.CHAR);
        break;
    case Opcodes.SASTORE:
        recordArrayStore(JavaType.SHORT);
        break;
    case Opcodes.POP:
    case Opcodes.POP2:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Pop(lineNumber, opcode - Opcodes.POP + 1));
            }
        });
        break;
    case Opcodes.DUP:
    case Opcodes.DUP_X1:
    case Opcodes.DUP_X2:
        recorder.add(new Runnable() {
            public void run() {
                int offset = opcode - Opcodes.DUP;
                block.addOp(new Duplicate(lineNumber, offset));
            }
        });
        break;
    case Opcodes.DUP2:
    case Opcodes.DUP2_X1:
    case Opcodes.DUP2_X2:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Duplicate2(lineNumber, opcode - Opcodes.DUP2));
            }
        });
        break;
    case Opcodes.SWAP:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Swap(lineNumber));
            }
        });
        break;
    case Opcodes.IRETURN:
        _return(JavaType.INT);
        break;
    case Opcodes.FRETURN:
        _return(JavaType.FLOAT);
        break;
    case Opcodes.ARETURN:
        _return(JavaType.OBJECT);
        break;
    case Opcodes.LRETURN:
        _return(JavaType.LONG);
        break;
    case Opcodes.DRETURN:
        _return(JavaType.DOUBLE);
        break;
    case Opcodes.ATHROW:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Throw(lineNumber));
            }
        });
        break;
    case Opcodes.RETURN:
        _return(JavaType.VOID);
        break;
    case Opcodes.LCMP:
        operation("cmp", JavaType.LONG, JavaType.LONG, JavaType.INT);
        break;
    case Opcodes.FCMPL:
        operation("cmpl", JavaType.FLOAT, JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.FCMPG:
        operation("cmpg", JavaType.FLOAT, JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.DCMPL:
        operation("cmpl", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.DCMPG:
        operation("cmpg", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.LSHL:
        operation("shl", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LSHR:
        operation("shr", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LUSHR:
        operation("ushr", JavaType.LONG, JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.LADD:
        operation("add", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LSUB:
        operation("sub", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LDIV:
        operation("div", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LREM:
        operation("rem", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LAND:
        operation("and", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LOR:
        operation("or", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LXOR:
        operation("xor", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.LMUL:
        operation("mul", JavaType.LONG, JavaType.LONG, JavaType.LONG);
        break;
    case Opcodes.FADD:
        operation("add", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FSUB:
        operation("sub", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FMUL:
        operation("mul", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FREM:
        operation("rem", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.FDIV:
        operation("div", JavaType.FLOAT, JavaType.FLOAT, JavaType.FLOAT);
        break;
    case Opcodes.ISHL:
        operation("shl", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.ISHR:
        operation("shr", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IUSHR:
        operation("ushr", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IADD:
        operation("add", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.ISUB:
        operation("sub", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IMUL:
        operation("mul", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IDIV:
        operation("div", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IREM:
        operation("rem", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IAND:
        operation("and", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IOR:
        operation("or", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.IXOR:
        operation("xor", JavaType.INT, JavaType.INT, JavaType.INT);
        break;
    case Opcodes.DSUB:
        operation("sub", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DADD:
        operation("add", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DMUL:
        operation("mul", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DDIV:
        operation("div", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.DREM:
        operation("rem", JavaType.DOUBLE, JavaType.DOUBLE, JavaType.DOUBLE);
        break;
    case Opcodes.L2I:
        convert(JavaType.LONG, JavaType.INT);
        break;
    case Opcodes.L2F:
        convert(JavaType.LONG, JavaType.FLOAT);
        break;
    case Opcodes.L2D:
        convert(JavaType.LONG, JavaType.DOUBLE);
        break;
    case Opcodes.LNEG:
        operation("neg", JavaType.LONG, null, JavaType.LONG);
        break;
    case Opcodes.F2I:
        convert(JavaType.FLOAT, JavaType.INT);
        break;
    case Opcodes.F2L:
        convert(JavaType.FLOAT, JavaType.LONG);
        break;
    case Opcodes.FNEG:
        operation("neg", JavaType.FLOAT, null, JavaType.FLOAT);
        break;
    case Opcodes.F2D:
        convert(JavaType.FLOAT, JavaType.DOUBLE);
        break;
    case Opcodes.D2I:
        convert(JavaType.DOUBLE, JavaType.INT);
        break;
    case Opcodes.D2L:
        convert(JavaType.DOUBLE, JavaType.LONG);
        break;
    case Opcodes.D2F:
        convert(JavaType.DOUBLE, JavaType.FLOAT);
        break;
    case Opcodes.DNEG:
        operation("neg", JavaType.DOUBLE, null, JavaType.DOUBLE);
        break;
    case Opcodes.I2L:
        convert(JavaType.INT, JavaType.LONG);
        break;
    case Opcodes.I2F:
        convert(JavaType.INT, JavaType.FLOAT);
        break;
    case Opcodes.I2D:
        convert(JavaType.INT, JavaType.DOUBLE);
        break;
    case Opcodes.I2B:
        convert(JavaType.INT, JavaType.BYTE);
        break;
    case Opcodes.I2C:
        convert(JavaType.INT, JavaType.CHAR);
        break;
    case Opcodes.I2S:
        convert(JavaType.INT, JavaType.SHORT);
        break;
    case Opcodes.INEG:
        operation("neg", JavaType.INT, null, JavaType.INT);
        break;
    case Opcodes.ARRAYLENGTH:
        operation("arraylength", JavaType.OBJECT.toArray(), null, JavaType.INT);
        break;
    case Opcodes.MONITORENTER:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new MonitorEnter(lineNumber));
            }
        });
        break;
    case Opcodes.MONITOREXIT:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new MonitorExit(lineNumber));
            }
        });
        break;
    case Opcodes.NOP:
        recorder.add(new Runnable() {
            public void run() {
                block.addOp(new Transform(lineNumber, "NOP", null, null, null));
            }
        });
    }
}

From source file:com.liferay.alloy.mvc.jsonwebservice.AlloyControllerInvokerManager.java

License:Open Source License

protected byte[] generateAlloyControllerInvokerClassData(Class<?> controllerClass,
        String alloyControllerInvokerClassName) throws NoClassNecessaryException {

    boolean jsonWebServiceMethodsPresent = false;

    ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

    String alloyControllerInvokerClassBinaryName = getClassBinaryName(alloyControllerInvokerClassName);

    String baseAlloyControllerInvokerClassBinaryName = getClassBinaryName(
            BaseAlloyControllerInvokerImpl.class.getName());

    classWriter.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER,
            alloyControllerInvokerClassBinaryName, null, baseAlloyControllerInvokerClassBinaryName, null);

    MethodVisitor methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);

    methodVisitor.visitCode();//from w  ww.ja va  2 s  .com
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, baseAlloyControllerInvokerClassBinaryName, "<init>",
            "()V");
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(1, 1);
    methodVisitor.visitEnd();

    Method[] methods = controllerClass.getDeclaredMethods();

    for (Method method : methods) {
        if (!Modifier.isPublic(method.getModifiers())) {
            continue;
        }

        JSONWebServiceMethod jsonWebServiceMethod = method.getAnnotation(JSONWebServiceMethod.class);

        if (jsonWebServiceMethod == null) {
            continue;
        }

        jsonWebServiceMethodsPresent = true;

        Class<?>[] parameterTypes = jsonWebServiceMethod.parameterTypes();

        StringBundler sb = new StringBundler(parameterTypes.length + 3);

        sb.append(StringPool.OPEN_PARENTHESIS);

        for (Class<?> parameterType : parameterTypes) {
            sb.append(Type.getDescriptor(parameterType));
        }

        sb.append(StringPool.CLOSE_PARENTHESIS);
        sb.append(Type.getDescriptor(JSONSerializable.class));

        String methodDescriptor = sb.toString();

        methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), methodDescriptor, null,
                new String[] { getClassBinaryName(Exception.class.getName()) });

        methodVisitor.visitCode();

        for (int i = 0; i < parameterTypes.length; i++) {
            String parameterName = jsonWebServiceMethod.parameterNames()[i];
            Class<?> parameterType = parameterTypes[i];

            methodVisitor.visitLocalVariable(parameterName, Type.getDescriptor(parameterType), null,
                    new Label(), new Label(), i + 1);
        }

        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitLdcInsn(jsonWebServiceMethod.lifecycle());

        methodVisitor.visitIntInsn(Opcodes.BIPUSH, parameterTypes.length * 2 + 2);
        methodVisitor.visitTypeInsn(Opcodes.ANEWARRAY, getClassBinaryName(Object.class.getName()));

        methodVisitor.visitInsn(Opcodes.DUP);
        methodVisitor.visitInsn(Opcodes.ICONST_0);
        methodVisitor.visitLdcInsn("action");
        methodVisitor.visitInsn(Opcodes.AASTORE);

        methodVisitor.visitInsn(Opcodes.DUP);
        methodVisitor.visitInsn(Opcodes.ICONST_1);
        methodVisitor.visitLdcInsn(method.getName());
        methodVisitor.visitInsn(Opcodes.AASTORE);

        for (int i = 0; i < parameterTypes.length; i++) {
            String parameterName = jsonWebServiceMethod.parameterNames()[i];

            methodVisitor.visitInsn(Opcodes.DUP);
            methodVisitor.visitIntInsn(Opcodes.BIPUSH, (i + 1) * 2);
            methodVisitor.visitLdcInsn(parameterName);
            methodVisitor.visitInsn(Opcodes.AASTORE);

            methodVisitor.visitInsn(Opcodes.DUP);
            methodVisitor.visitIntInsn(Opcodes.BIPUSH, (i + 1) * 2 + 1);
            methodVisitor.visitVarInsn(Opcodes.ALOAD, i + 1);
            methodVisitor.visitInsn(Opcodes.AASTORE);
        }

        sb = new StringBundler(5);

        sb.append(StringPool.OPEN_PARENTHESIS);
        sb.append(Type.getDescriptor(String.class));
        sb.append(Type.getDescriptor(Object[].class));
        sb.append(StringPool.CLOSE_PARENTHESIS);
        sb.append(Type.getDescriptor(JSONSerializable.class));

        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, alloyControllerInvokerClassBinaryName,
                "invokeAlloyController", sb.toString());

        methodVisitor.visitInsn(Opcodes.ARETURN);

        methodVisitor.visitMaxs(-1, -1);
        methodVisitor.visitEnd();
    }

    classWriter.visitEnd();

    if (!jsonWebServiceMethodsPresent) {
        throw new NoClassNecessaryException();
    }

    return classWriter.toByteArray();
}

From source file:com.liferay.portal.nio.intraband.proxy.IntrabandProxyUtilTest.java

License:Open Source License

@Test
public void testRewriteGetProxyMethodSignaturesMethodNode() {
    class TestClass {

        @SuppressWarnings("unused")
        public final String[] PROXY_METHOD_SIGNATURES = _getProxyMethodSignatures();

        private String[] _getProxyMethodSignatures() {
            return new String[0];
        }//  ww w.ja  va  2  s .  c  o  m
    }

    ClassNode classNode = _loadClass(TestClass.class);

    String[] proxyMethodSignatures = { "testSignature1", "testSignature2", "testSignature3" };

    IntrabandProxyUtil.rewriteGetProxyMethodSignaturesMethodNode(classNode, proxyMethodSignatures);

    MethodNode methodNode = ASMUtil.findMethodNode(classNode.methods, "_getProxyMethodSignatures",
            Type.getType(String[].class));

    InsnList insnList = methodNode.instructions;

    Iterator<AbstractInsnNode> iterator = insnList.iterator();

    _assertInsnNode(iterator.next(), Opcodes.ICONST_3);

    _assertTypeInsnNode(iterator.next(), Opcodes.ANEWARRAY, String.class);

    for (int i = 0; i < proxyMethodSignatures.length; i++) {
        _assertInsnNode(iterator.next(), Opcodes.DUP);
        _assertInsnNode(iterator.next(), Opcodes.ICONST_0 + i);
        _assertLdcInsnNode(iterator.next(), Opcodes.LDC, proxyMethodSignatures[i]);
        _assertInsnNode(iterator.next(), Opcodes.AASTORE);
    }

    _assertInsnNode(iterator.next(), Opcodes.ARETURN);

    Assert.assertFalse(iterator.hasNext());
}

From source file:com.liferay.portal.upgrade.test.BaseBuildAutoUpgradeTestCase.java

License:Open Source License

private void _initTableColumns(MethodVisitor methodVisitor, Object[][] tableColumns) {

    methodVisitor.visitCode();/*w  w w .  j a v  a 2s .c o  m*/

    methodVisitor.visitInsn(Opcodes.ICONST_0 + tableColumns.length);
    methodVisitor.visitTypeInsn(Opcodes.ANEWARRAY, Type.getDescriptor(Object[].class));
    methodVisitor.visitInsn(Opcodes.DUP);

    for (int i = 0; i < tableColumns.length; i++) {
        Object[] tableColumn = tableColumns[i];

        methodVisitor.visitInsn(Opcodes.ICONST_0 + i);
        methodVisitor.visitInsn(Opcodes.ICONST_2);
        methodVisitor.visitTypeInsn(Opcodes.ANEWARRAY, Type.getInternalName(Object.class));
        methodVisitor.visitInsn(Opcodes.DUP);
        methodVisitor.visitInsn(Opcodes.ICONST_0);
        methodVisitor.visitLdcInsn(tableColumn[0]);
        methodVisitor.visitInsn(Opcodes.AASTORE);
        methodVisitor.visitInsn(Opcodes.DUP);
        methodVisitor.visitInsn(Opcodes.ICONST_1);
        methodVisitor.visitIntInsn(Opcodes.BIPUSH, (Integer) tableColumn[1]);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(Integer.class), "valueOf",
                Type.getMethodDescriptor(Type.getType(Integer.class), Type.INT_TYPE), false);

        methodVisitor.visitInsn(Opcodes.AASTORE);
        methodVisitor.visitInsn(Opcodes.AASTORE);

        if (i < (tableColumns.length - 1)) {
            methodVisitor.visitInsn(Opcodes.DUP);
        }
    }

    methodVisitor.visitFieldInsn(Opcodes.PUTSTATIC,
            Type.getInternalName(BuildAutoUpgradeTestEntityModelImpl.class), "TABLE_COLUMNS",
            Type.getDescriptor(Object[][].class));
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(0, 0);
    methodVisitor.visitEnd();
}

From source file:com.mebigfatguy.junitflood.jvm.OperandStack.java

License:Apache License

public void performInsn(int opcode) {
    switch (opcode) {
    case Opcodes.NOP:
        break;//w w  w . j ava 2s.  c  o m

    case Opcodes.ACONST_NULL: {
        Operand op = new Operand();
        op.setNull(true);
        push(op);
    }
        break;

    case Opcodes.ICONST_M1: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(-1));
        push(op);
    }
        break;

    case Opcodes.ICONST_0: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(0));
        push(op);
    }
        break;

    case Opcodes.ICONST_1: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(1));
        push(op);
    }
        break;

    case Opcodes.ICONST_2: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(2));
        push(op);
    }
        break;

    case Opcodes.ICONST_3: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(3));
        push(op);
    }
        break;

    case Opcodes.ICONST_4: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(4));
        push(op);
    }
        break;

    case Opcodes.ICONST_5: {
        Operand op = new Operand();
        op.setConstant(Integer.valueOf(5));
        push(op);
    }
        break;

    case Opcodes.LCONST_0: {
        Operand op = new Operand();
        op.setConstant(Long.valueOf(0));
        push(op);
    }
        break;

    case Opcodes.LCONST_1: {
        Operand op = new Operand();
        op.setConstant(Long.valueOf(1));
        push(op);
    }
        break;

    case Opcodes.FCONST_0: {
        Operand op = new Operand();
        op.setConstant(Float.valueOf(0));
        push(op);
    }
        break;

    case Opcodes.FCONST_1: {
        Operand op = new Operand();
        op.setConstant(Float.valueOf(1));
        push(op);
    }
        break;

    case Opcodes.FCONST_2: {
        Operand op = new Operand();
        op.setConstant(Float.valueOf(2));
        push(op);
    }
        break;

    case Opcodes.DCONST_0: {
        Operand op = new Operand();
        op.setConstant(Double.valueOf(0));
        push(op);
    }
        break;

    case Opcodes.DCONST_1: {
        Operand op = new Operand();
        op.setConstant(Double.valueOf(1));
        push(op);
    }
        break;

    case Opcodes.IALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.AALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("Ljava/lang/Object;");
        push(op);
    }
        break;

    case Opcodes.BALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("B");
        push(op);
    }
        break;

    case Opcodes.CALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("C");
        push(op);
    }
        break;

    case Opcodes.SALOAD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("S");
        push(op);
    }
        break;

    case Opcodes.IASTORE:
    case Opcodes.LASTORE:
    case Opcodes.FASTORE:
    case Opcodes.DASTORE:
    case Opcodes.AASTORE:
    case Opcodes.BASTORE:
    case Opcodes.CASTORE:
    case Opcodes.SASTORE:
        if (stack.size() < 2) {
            stack.clear();
        } else {
            Operand value = stack.remove(stack.size() - 1);
            Operand reg = stack.remove(stack.size() - 1);
            registers.put(Integer.valueOf(reg.getRegister()), value);
        }
        break;

    case Opcodes.POP:
        pop();
        break;

    case Opcodes.POP2:
        pop2();
        break;

    case Opcodes.DUP:
        if (!stack.isEmpty()) {
            Operand op = stack.get(stack.size() - 1);
            push(op);
        }
        break;

    case Opcodes.DUP_X1:
        if (stack.size() >= 2) {
            Operand op = stack.get(stack.size() - 1);
            stack.add(stack.size() - 2, op);
        }
        break;

    case Opcodes.DUP_X2:
        if (stack.size() >= 2) {
            Operand op = stack.get(stack.size() - 2);
            String sig = op.getSignature();
            op = stack.get(stack.size() - 1);
            if ("J".equals(sig) || "D".equals(sig)) {
                stack.add(stack.size() - 2, op);
            } else if (stack.size() >= 3) {
                stack.add(stack.size() - 3, op);
            }
        }
        break;

    case Opcodes.DUP2:
        if (stack.size() >= 2) {
            stack.add(stack.get(stack.size() - 2));
            stack.add(stack.get(stack.size() - 2));
        }
        break;

    case Opcodes.DUP2_X1:
        if (stack.size() >= 1) {
            Operand op = stack.get(stack.size() - 1);
            String sig = op.getSignature();
            if ("J".equals(sig) || "D".equals(sig)) {
                if (stack.size() >= 3) {
                    stack.add(stack.size() - 3, op);
                    op = stack.get(stack.size() - 2);
                    stack.add(stack.size() - 4, op);
                }
            } else {
                if (stack.size() >= 2) {
                    stack.add(stack.size() - 2, op);
                }
            }
        }
        break;

    case Opcodes.DUP2_X2:
        if (stack.size() >= 1) {
            Operand op = stack.get(stack.size() - 1);
            String sig = op.getSignature();
            if ("J".equals(sig) || "D".equals(sig)) {
                if (stack.size() >= 2) {
                    op = stack.get(stack.size() - 2);
                    sig = op.getSignature();
                    if ("J".equals(sig) || "D".equals(sig)) {
                        op = stack.get(stack.size() - 1);
                        stack.add(stack.size() - 2, op);
                    } else {
                        if (stack.size() >= 3) {
                            op = stack.get(stack.size() - 1);
                            stack.add(stack.size() - 3, op);
                        }
                    }
                }
            } else {
                if (stack.size() >= 3) {
                    op = stack.get(stack.size() - 3);
                    sig = op.getSignature();
                    if ("J".equals(sig) || "D".equals(sig)) {
                        op = stack.get(stack.size() - 2);
                        stack.add(stack.size() - 3, op);
                        op = stack.get(stack.size() - 1);
                        stack.add(stack.size() - 3, op);
                    } else {
                        if (stack.size() >= 4) {
                            op = stack.get(stack.size() - 2);
                            stack.add(stack.size() - 4, op);
                            op = stack.get(stack.size() - 1);
                            stack.add(stack.size() - 4, op);
                        }
                    }
                }
            }
        }
        break;

    case Opcodes.SWAP:
        if (stack.size() >= 2) {
            Operand op = stack.remove(stack.size() - 1);
            stack.add(stack.size() - 1, op);
        }
        break;

    case Opcodes.IADD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LADD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FADD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DADD: {
        pop2();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.ISUB: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LSUB: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FSUB: {
        pop2();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DSUB: {
        pop2();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.IMUL: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LMUL: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FMUL: {
        pop2();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DMUL: {
        pop2();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.IDIV: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LDIV: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FDIV: {
        pop2();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DDIV: {
        pop2();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.IREM: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LREM: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FREM: {
        pop2();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DREM: {
        pop2();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.INEG: {
        pop();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LNEG: {
        pop();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.FNEG: {
        pop();
        Operand op = new Operand();
        op.setSignature("F");
        push(op);
    }
        break;

    case Opcodes.DNEG: {
        pop();
        Operand op = new Operand();
        op.setSignature("D");
        push(op);
    }
        break;

    case Opcodes.ISHL: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LSHL: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.ISHR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LSHR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.IUSHR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LUSHR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.IAND: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LAND: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.IOR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LOR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.IXOR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.LXOR: {
        pop2();
        Operand op = new Operand();
        op.setSignature("J");
        push(op);
    }
        break;

    case Opcodes.I2L: {
        Operand lop = new Operand();
        lop.setSignature("J");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                lop.setConstant(Long.valueOf(((Integer) o).longValue()));
            }
        }
        push(lop);
    }
        break;

    case Opcodes.I2F: {
        Operand fop = new Operand();
        fop.setSignature("F");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                fop.setConstant(Float.valueOf(((Integer) o).floatValue()));
            }
        }
        push(fop);
    }
        break;

    case Opcodes.I2D: {
        Operand dop = new Operand();
        dop.setSignature("D");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                dop.setConstant(Double.valueOf(((Integer) o).doubleValue()));
            }
        }
        push(dop);
    }
        break;

    case Opcodes.L2I: {
        Operand iop = new Operand();
        iop.setSignature("I");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                iop.setConstant(Integer.valueOf(((Long) o).intValue()));
            }
        }
        push(iop);
    }
        break;

    case Opcodes.L2F: {
        Operand fop = new Operand();
        fop.setSignature("F");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                fop.setConstant(Float.valueOf(((Long) o).floatValue()));
            }
        }
        push(fop);
    }
        break;

    case Opcodes.L2D: {
        Operand dop = new Operand();
        dop.setSignature("D");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                dop.setConstant(Double.valueOf(((Long) o).doubleValue()));
            }
        }
        push(dop);
    }
        break;

    case Opcodes.F2I: {
        Operand iop = new Operand();
        iop.setSignature("I");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                iop.setConstant(Integer.valueOf(((Float) o).intValue()));
            }
        }
        push(iop);
    }
        break;

    case Opcodes.F2L: {
        Operand lop = new Operand();
        lop.setSignature("J");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                lop.setConstant(Long.valueOf(((Float) o).longValue()));
            }
        }
        push(lop);
    }
        break;

    case Opcodes.F2D: {
        Operand dop = new Operand();
        dop.setSignature("D");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                dop.setConstant(Double.valueOf(((Float) o).doubleValue()));
            }
        }
        push(dop);
    }
        break;

    case Opcodes.D2I: {
        Operand iop = new Operand();
        iop.setSignature("I");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                iop.setConstant(Integer.valueOf(((Double) o).intValue()));
            }
        }
        push(iop);
    }
        break;

    case Opcodes.D2L: {
        Operand lop = new Operand();
        lop.setSignature("J");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                lop.setConstant(Long.valueOf(((Double) o).longValue()));
            }
        }
        push(lop);
    }
        break;

    case Opcodes.D2F: {
        Operand fop = new Operand();
        fop.setSignature("F");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                fop.setConstant(Float.valueOf(((Double) o).floatValue()));
            }
        }
        push(fop);
    }
        break;

    case Opcodes.I2B: {
        Operand bop = new Operand();
        bop.setSignature("B");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                bop.setConstant(Byte.valueOf(((Integer) o).byteValue()));
            }
        }
        push(bop);
    }
        break;

    case Opcodes.I2C: {
        Operand cop = new Operand();
        cop.setSignature("C");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                cop.setConstant(Character.valueOf((char) ((Integer) o).intValue()));
            }
        }
        push(cop);
    }
        break;

    case Opcodes.I2S: {
        Operand sop = new Operand();
        sop.setSignature("S");
        if (!stack.isEmpty()) {
            Operand op = stack.remove(stack.size() - 1);
            Object o = op.getConstant();
            if (o != null) {
                sop.setConstant(Short.valueOf((short) ((Integer) o).intValue()));
            }
        }
        push(sop);
    }
        break;

    case Opcodes.LCMP:
    case Opcodes.FCMPL:
    case Opcodes.FCMPG:
    case Opcodes.DCMPL:
    case Opcodes.DCMPG: {
        pop2();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
    case Opcodes.FRETURN:
    case Opcodes.DRETURN:
    case Opcodes.ARETURN:
        pop();
        break;

    case Opcodes.RETURN:
        //nop
        break;

    case Opcodes.ARRAYLENGTH: {
        pop();
        Operand op = new Operand();
        op.setSignature("I");
        push(op);
    }
        break;

    case Opcodes.ATHROW:
    case Opcodes.MONITORENTER:
    case Opcodes.MONITOREXIT:
        pop();
        break;
    }
}

From source file:com.microsoft.applicationinsights.agent.internal.agent.redis.JedisMethodVisitor.java

License:Open Source License

protected void storeTopStackValueIntoArray() {
    mv.visitInsn(Opcodes.AASTORE);
}

From source file:com.mogujie.instantrun.IncrementalTool.java

License:Apache License

public static byte[] getPatchFileContents(ImmutableList<String> patchFileContents,
        ImmutableList<Integer> patchIndexContents) {
    if (patchFileContents.size() != patchIndexContents.size()) {
        throw new GradleException("patchFileContents's size is " + patchFileContents.size()
                + ", but patchIndexContents's size is " + patchIndexContents.size()
                + ", please check the changed classes.");
    }// w w  w  .j  a  v  a 2s .co m
    ClassWriter cw = new ClassWriter(0);
    MethodVisitor mv;

    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, IncrementalVisitor.APP_PATCHES_LOADER_IMPL,
            null, IncrementalVisitor.ABSTRACT_PATCHES_LOADER_IMPL, null);

    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, IncrementalVisitor.ABSTRACT_PATCHES_LOADER_IMPL, "<init>",
                "()V", false);
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getPatchedClasses", "()[Ljava/lang/String;", null, null);
        mv.visitCode();

        mv.visitIntInsn(Opcodes.SIPUSH, patchFileContents.size());
        mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
        for (int index = 0; index < patchFileContents.size(); index++) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitIntInsn(Opcodes.SIPUSH, index);
            mv.visitLdcInsn(patchFileContents.get(index));
            mv.visitInsn(Opcodes.AASTORE);
        }
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(4, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getPatchedClassIndexes", "()[I", null, null);
        mv.visitCode();

        mv.visitIntInsn(Opcodes.SIPUSH, patchIndexContents.size());
        mv.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT);
        for (int index = 0; index < patchIndexContents.size(); index++) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitIntInsn(Opcodes.SIPUSH, index);
            mv.visitLdcInsn(patchIndexContents.get(index));
            mv.visitInsn(Opcodes.IASTORE);
        }
        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(4, 1);
        mv.visitEnd();
    }
    cw.visitEnd();

    return cw.toByteArray();

}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Generates instructions to save the operand stack to an object array.
 * @param arrayStackVar variable that the object array containing operand stack is stored
 * @param tempObjectVar variable to use for temporary objects
 * @param frame execution frame at the instruction where the operand stack is to be saved
 * @return instructions to save the operand stack in to an array and save it to the local variables table
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if variables have the same index, or if variables have been released, or if variables are of wrong
 * type/*w  w  w.j a va  2 s .c  o m*/
 */
public static InsnList saveOperandStack(Variable arrayStackVar, Variable tempObjectVar,
        Frame<BasicValue> frame) {
    Validate.notNull(arrayStackVar);
    Validate.notNull(tempObjectVar);
    Validate.notNull(frame);
    Validate.isTrue(arrayStackVar.getType().equals(Type.getType(Object[].class)));
    Validate.isTrue(tempObjectVar.getType().equals(Type.getType(Object.class)));
    validateLocalIndicies(arrayStackVar.getIndex(), tempObjectVar.getIndex());

    InsnList ret = new InsnList();

    // Create stack storage array and save it in local vars table
    ret.add(new LdcInsnNode(frame.getStackSize()));
    ret.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));
    ret.add(new VarInsnNode(Opcodes.ASTORE, arrayStackVar.getIndex()));

    // Save the stack
    for (int i = frame.getStackSize() - 1; i >= 0; i--) {
        BasicValue basicValue = frame.getStack(i);
        Type type = basicValue.getType();

        // If type is 'Lnull;', this means that the slot has been assigned null and that "there has been no merge yet that would 'raise'
        // the type toward some class or interface type" (from ASM mailing list). We know this slot will always contain null at this
        // point in the code so we can avoid saving it (but we still need to do a POP to get rid of it). When we load it back up, we can
        // simply push a null in to that slot, thereby keeping the same 'Lnull;' type.
        if ("Lnull;".equals(type.getDescriptor())) {
            ret.add(new InsnNode(Opcodes.POP));
            continue;
        }

        // Convert the item to an object (if not already an object) and stores it in local vars table. Item removed from stack.
        switch (type.getSort()) {
        case Type.BOOLEAN:
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf",
                    "(Z)Ljava/lang/Boolean;"));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.BYTE:
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;",
                    false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.SHORT:
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf",
                    "(S)Ljava/lang/Short;", false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.CHAR:
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf",
                    "(C)Ljava/lang/Character;", false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.INT:
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf",
                    "(I)Ljava/lang/Integer;", false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.FLOAT:
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf",
                    "(F)Ljava/lang/Float;", false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.LONG:
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;",
                    false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.DOUBLE:
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf",
                    "(D)Ljava/lang/Double;", false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.ARRAY:
        case Type.OBJECT:
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.METHOD:
        case Type.VOID:
        default:
            throw new IllegalArgumentException();
        }

        // Store item in to stack storage array
        ret.add(new VarInsnNode(Opcodes.ALOAD, arrayStackVar.getIndex()));
        ret.add(new LdcInsnNode(i));
        ret.add(new VarInsnNode(Opcodes.ALOAD, tempObjectVar.getIndex()));
        ret.add(new InsnNode(Opcodes.AASTORE));
    }

    // Restore the stack
    for (int i = 0; i < frame.getStackSize(); i++) {
        BasicValue basicValue = frame.getStack(i);
        Type type = basicValue.getType();

        // If type is 'Lnull;', this means that the slot has been assigned null and that "there has been no merge yet that would 'raise'
        // the type toward some class or interface type" (from ASM mailing list). We know this slot will always contain null at this
        // point in the code so there's no specific value to load up from the array. Instead we push a null in to that slot, thereby
        // keeping the same 'Lnull;' type originally assigned to that slot (it doesn't make sense to do a CHECKCAST because 'null' is
        // not a real class and can never be a real class -- null is a reserved word in Java).
        if (type.getSort() == Type.OBJECT && "Lnull;".equals(type.getDescriptor())) {
            ret.add(new InsnNode(Opcodes.ACONST_NULL));
            continue;
        }

        // Load item from stack storage array
        ret.add(new VarInsnNode(Opcodes.ALOAD, arrayStackVar.getIndex()));
        ret.add(new LdcInsnNode(i));
        ret.add(new InsnNode(Opcodes.AALOAD));

        // Convert the item to an object (if not already an object) and stores it in local vars table. Item removed from stack.
        switch (type.getSort()) {
        case Type.BOOLEAN:
            ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Boolean"));
            ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z",
                    false));
            break;
        case Type.BYTE:
            ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Byte"));
            ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B", false));
            break;
        case Type.SHORT:
            ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Short"));
            ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S", false));
            break;
        case Type.CHAR:
            ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Character"));
            ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C",
                    false));
            break;
        case Type.INT:
            ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Integer"));
            ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I", false));
            break;
        case Type.FLOAT:
            ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Float"));
            ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F", false));
            break;
        case Type.LONG:
            ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Long"));
            ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J", false));
            break;
        case Type.DOUBLE:
            ret.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/lang/Double"));
            ret.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D", false));
            break;
        case Type.ARRAY:
        case Type.OBJECT:
            ret.add(new TypeInsnNode(Opcodes.CHECKCAST, basicValue.getType().getInternalName()));
            break;
        case Type.METHOD:
        case Type.VOID:
        default:
            throw new IllegalArgumentException();
        }
    }

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.asm.InstructionUtils.java

License:Open Source License

/**
 * Generates instructions to save the local variables table to an object array.
 *
 * @param arrayLocalsVar variable that the object array containing local variables table is stored
 * @param tempObjectVar variable to use for temporary objects
 * @param frame execution frame at the instruction where the local variables table is to be saved
 * @return instructions to save the local variables table in to an array
 * @throws NullPointerException if any argument is {@code null}
 * @throws IllegalArgumentException if variables have the same index, or if variables have been released, or if variables are of wrong
 * type//from w w  w  . j  a v a 2s  .  c  o m
 */
public static InsnList saveLocalVariableTable(Variable arrayLocalsVar, Variable tempObjectVar,
        Frame<BasicValue> frame) {
    Validate.notNull(arrayLocalsVar);
    Validate.notNull(tempObjectVar);
    Validate.notNull(frame);
    Validate.isTrue(arrayLocalsVar.getType().equals(Type.getType(Object[].class)));
    Validate.isTrue(tempObjectVar.getType().equals(Type.getType(Object.class)));
    validateLocalIndicies(arrayLocalsVar.getIndex(), tempObjectVar.getIndex());
    InsnList ret = new InsnList();

    // Create array and save it in local vars table
    ret.add(new LdcInsnNode(frame.getLocals()));
    ret.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));
    ret.add(new VarInsnNode(Opcodes.ASTORE, arrayLocalsVar.getIndex()));

    // Save the locals
    for (int i = 0; i < frame.getLocals(); i++) {
        BasicValue basicValue = frame.getLocal(i);
        Type type = basicValue.getType();

        // If type == null, basicValue is pointing to uninitialized var -- basicValue.toString() will return '.'. This means that this
        // slot contains nothing to save. So, skip this slot if we encounter it.
        if (type == null) {
            continue;
        }

        // If type is 'Lnull;', this means that the slot has been assigned null and that "there has been no merge yet that would 'raise'
        // the type toward some class or interface type" (from ASM mailing list). We know this slot will always contain null at this
        // point in the code so we can avoid saving it. When we load it back up, we can simply push a null in to that slot, thereby
        // keeping the same 'Lnull;' type.
        if ("Lnull;".equals(type.getDescriptor())) {
            continue;
        }

        // Convert the item to an object (if not already an object) and stores it in array.
        switch (type.getSort()) {
        case Type.BOOLEAN:
            ret.add(new VarInsnNode(Opcodes.ILOAD, i));
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf",
                    "(Z)Ljava/lang/Boolean;"));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.BYTE:
            ret.add(new VarInsnNode(Opcodes.ILOAD, i));
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;",
                    false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.SHORT:
            ret.add(new VarInsnNode(Opcodes.ILOAD, i));
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf",
                    "(S)Ljava/lang/Short;", false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.CHAR:
            ret.add(new VarInsnNode(Opcodes.ILOAD, i));
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf",
                    "(C)Ljava/lang/Character;", false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.INT:
            ret.add(new VarInsnNode(Opcodes.ILOAD, i));
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf",
                    "(I)Ljava/lang/Integer;", false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.FLOAT:
            ret.add(new VarInsnNode(Opcodes.FLOAD, i));
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf",
                    "(F)Ljava/lang/Float;", false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.LONG:
            ret.add(new VarInsnNode(Opcodes.LLOAD, i));
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;",
                    false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.DOUBLE:
            ret.add(new VarInsnNode(Opcodes.DLOAD, i));
            ret.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf",
                    "(D)Ljava/lang/Double;", false));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.ARRAY:
        case Type.OBJECT:
            ret.add(new VarInsnNode(Opcodes.ALOAD, i));
            ret.add(new VarInsnNode(Opcodes.ASTORE, tempObjectVar.getIndex()));
            break;
        case Type.METHOD:
        case Type.VOID:
        default:
            throw new IllegalStateException();
        }

        // Store item in to locals storage array
        ret.add(new VarInsnNode(Opcodes.ALOAD, arrayLocalsVar.getIndex()));
        ret.add(new LdcInsnNode(i));
        ret.add(new VarInsnNode(Opcodes.ALOAD, tempObjectVar.getIndex()));
        ret.add(new InsnNode(Opcodes.AASTORE));
    }

    return ret;
}

From source file:com.offbynull.coroutines.instrumenter.LocalsStateGenerators.java

License:Open Source License

/**
 * Generates instructions to save the local variables table.
 * @param markerType debug marker type//from   ww  w. java  2  s  .  c om
 * @param storageVars variables to store locals in to
 * @param frame execution frame at the instruction where the local variables table is to be saved
 * @return instructions to save the local variables table in to an array
 * @throws NullPointerException if any argument is {@code null}
 */
public static InsnList saveLocals(MarkerType markerType, StorageVariables storageVars,
        Frame<BasicValue> frame) {
    Validate.notNull(markerType);
    Validate.notNull(storageVars);
    Validate.notNull(frame);

    Variable intsVar = storageVars.getIntStorageVar();
    Variable floatsVar = storageVars.getFloatStorageVar();
    Variable longsVar = storageVars.getLongStorageVar();
    Variable doublesVar = storageVars.getDoubleStorageVar();
    Variable objectsVar = storageVars.getObjectStorageVar();

    int intsCounter = 0;
    int floatsCounter = 0;
    int longsCounter = 0;
    int doublesCounter = 0;
    int objectsCounter = 0;

    StorageSizes storageSizes = computeSizes(frame);

    InsnList ret = new InsnList();

    // Create storage arrays and save them in respective storage vars
    ret.add(merge(debugMarker(markerType, "Saving locals"), mergeIf(intsVar != null, () -> new Object[] {
            debugMarker(markerType, "Generating ints container (" + storageSizes.getIntsSize() + ")"),
            new LdcInsnNode(storageSizes.getIntsSize()), new IntInsnNode(Opcodes.NEWARRAY,
                    Opcodes.T_INT),
            new VarInsnNode(Opcodes.ASTORE, intsVar.getIndex()) }),
            mergeIf(floatsVar != null,
                    () -> new Object[] {
                            debugMarker(markerType,
                                    "Generating floats container (" + storageSizes.getFloatsSize() + ")"),
                            new LdcInsnNode(storageSizes.getFloatsSize()),
                            new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_FLOAT),
                            new VarInsnNode(Opcodes.ASTORE, floatsVar.getIndex()) }),
            mergeIf(longsVar != null, () -> new Object[] {
                    debugMarker(markerType, "Generating longs container (" + storageSizes.getLongsSize() + ")"),
                    new LdcInsnNode(storageSizes.getLongsSize()),
                    new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_LONG),
                    new VarInsnNode(Opcodes.ASTORE, longsVar.getIndex()) }),
            mergeIf(doublesVar != null, () -> new Object[] {
                    debugMarker(markerType,
                            "Generating doubles container (" + storageSizes.getDoublesSize() + ")"),
                    new LdcInsnNode(storageSizes.getDoublesSize()),
                    new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_DOUBLE),
                    new VarInsnNode(Opcodes.ASTORE, doublesVar.getIndex()) }),
            mergeIf(objectsVar != null,
                    () -> new Object[] {
                            debugMarker(markerType,
                                    "Generating objects container (" + storageSizes.getObjectsSize() + ")"),
                            new LdcInsnNode(storageSizes.getObjectsSize()),
                            new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"),
                            new VarInsnNode(Opcodes.ASTORE, objectsVar.getIndex()) })));

    // Save the locals
    for (int i = 0; i < frame.getLocals(); i++) {
        BasicValue basicValue = frame.getLocal(i);
        Type type = basicValue.getType();

        // If type == null, basicValue is pointing to uninitialized var -- basicValue.toString() will return '.'. This means that this
        // slot contains nothing to save. So, skip this slot if we encounter it.
        if (type == null) {
            ret.add(debugMarker(markerType, "Skipping uninitialized value at " + i));
            continue;
        }

        // If type is 'Lnull;', this means that the slot has been assigned null and that "there has been no merge yet that would 'raise'
        // the type toward some class or interface type" (from ASM mailing list). We know this slot will always contain null at this
        // point in the code so we can avoid saving it. When we load it back up, we can simply push a null in to that slot, thereby
        // keeping the same 'Lnull;' type.
        if ("Lnull;".equals(type.getDescriptor())) {
            ret.add(debugMarker(markerType, "Skipping null value at " + i));
            continue;
        }

        // Place item in to appropriate storage array
        switch (type.getSort()) {
        case Type.BOOLEAN:
        case Type.BYTE:
        case Type.SHORT:
        case Type.CHAR:
        case Type.INT:
            ret.add(debugMarker(markerType,
                    "Inserting int at LVT index " + i + " to storage index " + intsCounter));
            ret.add(new VarInsnNode(Opcodes.ALOAD, intsVar.getIndex())); // [int[]]
            ret.add(new LdcInsnNode(intsCounter)); // [int[], idx]
            ret.add(new VarInsnNode(Opcodes.ILOAD, i)); // [int[], idx, val]
            ret.add(new InsnNode(Opcodes.IASTORE)); // []
            intsCounter++;
            break;
        case Type.FLOAT:
            ret.add(debugMarker(markerType,
                    "Inserting float at LVT index " + i + " to storage index " + floatsCounter));
            ret.add(new VarInsnNode(Opcodes.ALOAD, floatsVar.getIndex())); // [float[]]
            ret.add(new LdcInsnNode(floatsCounter)); // [float[], idx]
            ret.add(new VarInsnNode(Opcodes.FLOAD, i)); // [float[], idx, val]
            ret.add(new InsnNode(Opcodes.FASTORE)); // []
            floatsCounter++;
            break;
        case Type.LONG:
            ret.add(debugMarker(markerType,
                    "Inserting long at LVT index " + i + " to storage index " + longsCounter));
            ret.add(new VarInsnNode(Opcodes.ALOAD, longsVar.getIndex())); // [long[]]
            ret.add(new LdcInsnNode(longsCounter)); // [long[], idx]
            ret.add(new VarInsnNode(Opcodes.LLOAD, i)); // [long[], idx, val]
            ret.add(new InsnNode(Opcodes.LASTORE)); // []
            longsCounter++;
            break;
        case Type.DOUBLE:
            ret.add(debugMarker(markerType,
                    "Inserting double at LVT index " + i + " to storage index " + doublesCounter));
            ret.add(new VarInsnNode(Opcodes.ALOAD, doublesVar.getIndex())); // [double[]]
            ret.add(new LdcInsnNode(doublesCounter)); // [double[], idx]
            ret.add(new VarInsnNode(Opcodes.DLOAD, i)); // [double[], idx, val]
            ret.add(new InsnNode(Opcodes.DASTORE)); // []
            doublesCounter++;
            break;
        case Type.ARRAY:
        case Type.OBJECT:
            ret.add(debugMarker(markerType,
                    "Inserting object at LVT index " + i + " to storage index " + objectsCounter));
            ret.add(new VarInsnNode(Opcodes.ALOAD, objectsVar.getIndex())); // [Object[]]
            ret.add(new LdcInsnNode(objectsCounter)); // [Object[], idx]
            ret.add(new VarInsnNode(Opcodes.ALOAD, i)); // [Object[], idx, val]
            ret.add(new InsnNode(Opcodes.AASTORE)); // []
            objectsCounter++;
            break;
        case Type.METHOD:
        case Type.VOID:
        default:
            throw new IllegalStateException();
        }
    }

    return ret;
}