Example usage for org.objectweb.asm Opcodes ARETURN

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

Introduction

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

Prototype

int ARETURN

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

Click Source Link

Usage

From source file:com.intellij.compiler.notNullVerification.ThrowOnNullMethodVisitor.java

License:Apache License

/**
 * Visits a zero operand instruction (ie return).
 * <p>//ww w . j  av a 2 s.  c o m
 * {@inheritDoc}
 */
public void visitInsn(final int opcode) {
    if (shouldInclude() && opcode == Opcodes.ARETURN) {
        if (isReturnNotNull) {
            mv.visitInsn(Opcodes.DUP);
            final Label skipLabel = new Label();
            mv.visitJumpInsn(Opcodes.IFNONNULL, skipLabel);
            generateThrow(ISE_CLASS_NAME,
                    "NotNull method " + classInfo.getName() + "." + methodName + " must not return null",
                    skipLabel);
        }
    }
    mv.visitInsn(opcode);
}

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.j  av  a 2s  .  c  om
    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];
        }/*from ww w  .  j a v  a2 s  .co  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.nio.intraband.proxy.IntrabandProxyUtilTest.java

License:Open Source License

private void _doTestCreateProxyMethodNode(Method method, int index, String skeletonId,
        String stubInternalName) {

    MethodNode proxyMethodNode = IntrabandProxyUtil.createProxyMethodNode(method, index, skeletonId,
            Type.getType(stubInternalName));

    _assertMethodNodeSignature(proxyMethodNode, method.getModifiers() & ~Modifier.ABSTRACT, method.getName(),
            Type.getMethodDescriptor(method), method.getExceptionTypes());

    InsnList insnList = proxyMethodNode.instructions;

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

    // NEW com/liferay/portal/kernel/io/Serializer

    _assertTypeInsnNode(iterator.next(), Opcodes.NEW, Serializer.class);

    // DUP//w w  w .j  a v  a  2 s.c om

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

    // INVOKESPECIAL com/liferay/portal/kernel/io/Serializer <init> ()V

    _assertMethodInsnNode(iterator.next(), Opcodes.INVOKESPECIAL, Type.getInternalName(Serializer.class),
            "<init>", Type.VOID_TYPE);

    // ASTORE argumentsSize

    Type methodType = Type.getType(method);

    int argumentsAndReturnSizes = methodType.getArgumentsAndReturnSizes();

    int argumentsSize = argumentsAndReturnSizes >> 2;

    _assertVarInsnNode(iterator.next(), Opcodes.ASTORE, argumentsSize);

    // ALOAD argumentsSize

    _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize);

    // LDC skeletonId

    _assertLdcInsnNode(iterator.next(), Opcodes.LDC, skeletonId);

    // INVOKEVIRTUAL com/liferay/portal/kernel/io/Serializer writeString
    // (Ljava/lang/String;)V

    _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Serializer.class),
            "writeString", Type.VOID_TYPE, Type.getType(String.class));

    // ALOAD argumentsSize

    _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize);

    // ALOAD 0

    _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, 0);

    // GETFIELD stubInternalName _id Ljava/lang/String;

    _assertFieldInsnNode(iterator.next(), Opcodes.GETFIELD, stubInternalName, "_id", String.class);

    // INVOKEVIRTUAL com/liferay/portal/kernel/io/Serializer writeString
    // (Ljava/lang/String;)V

    _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Serializer.class),
            "writeString", Type.VOID_TYPE, Type.getType(String.class));

    // ALOAD argumentsSize

    _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize);

    if (index <= 5) {

        // ICONST_index

        _assertInsnNode(iterator.next(), Opcodes.ICONST_0 + index);
    } else {

        // BIPUSH index

        _assertIntInsnNode(iterator.next(), Opcodes.BIPUSH, index);
    }

    // INVOKEVIRTUAL com/liferay/portal/kernel/io/Serializer writeInt (I)V

    _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL, Type.getInternalName(Serializer.class),
            "writeInt", Type.VOID_TYPE, Type.INT_TYPE);

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

    int offset = 1;

    for (int i = 0; i < parameterTypes.length; i++) {
        Class<?> parameterClass = parameterTypes[i];

        // ALOAD argumentsSize

        _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize);

        // xLOAD i

        Type parameterType = Type.getType(parameterClass);

        _assertVarInsnNode(iterator.next(), parameterType.getOpcode(Opcodes.ILOAD), offset);

        offset += parameterType.getSize();

        if (parameterClass.isPrimitive() || (parameterClass == String.class)) {

            String name = TextFormatter.format(parameterClass.getSimpleName(), TextFormatter.G);

            _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                    Type.getInternalName(Serializer.class), "write".concat(name), Type.VOID_TYPE,
                    parameterType);
        } else {
            _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                    Type.getInternalName(Serializer.class), "writeObject", Type.VOID_TYPE,
                    Type.getType(Serializable.class));
        }
    }

    // ALOAD 0

    _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, 0);

    // ALOAD argumentsSize

    _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize);

    Class<?> returnClass = method.getReturnType();

    Type returnType = Type.getType(returnClass);

    if (returnClass == void.class) {

        // INVOKESPECIAL stubInternalName _send
        // (Lcom/liferay/portal/kernel/io/Serializer;)V

        _assertMethodInsnNode(iterator.next(), Opcodes.INVOKESPECIAL, stubInternalName, "_send", Type.VOID_TYPE,
                Type.getType(Serializer.class));

        _assertInsnNode(iterator.next(), Opcodes.RETURN);
    } else {

        // INVOKESPECIAL stubInternalName _syncSend
        // (Lcom/liferay/portal/kernel/io/Serializer;)Ljava/io/Serializable;

        _assertMethodInsnNode(iterator.next(), Opcodes.INVOKESPECIAL, stubInternalName, "_syncSend",
                Type.getType(Serializable.class), Type.getType(Serializer.class));

        if (returnClass.isPrimitive()) {

            // ASTORE argumentsSize + 1

            _assertVarInsnNode(iterator.next(), Opcodes.ASTORE, argumentsSize + 1);

            // ALOAD argumentsSize + 1

            _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize + 1);

            // IFNULL nullCheckLabel

            LabelNode nullCheckLabelNode = _assertJumpInsnNode(iterator.next(), Opcodes.IFNULL);

            // ALOAD argumentsSize + 1

            _assertVarInsnNode(iterator.next(), Opcodes.ALOAD, argumentsSize + 1);

            // CHECKCAST returnType

            _assertTypeInsnNode(iterator.next(), Opcodes.CHECKCAST, _autoboxingMap.get(returnClass));

            if (returnClass == boolean.class) {

                // INVOKEVIRTUAL java/lang/Boolean booleanValue ()Z

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Boolean.class), "booleanValue", Type.BOOLEAN_TYPE);
            } else if (returnClass == byte.class) {

                // INVOKEVIRTUAL java/lang/Number intValue ()I

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Number.class), "intValue", Type.INT_TYPE);
            } else if (returnClass == char.class) {

                // INVOKEVIRTUAL java/lang/Character charValue ()C

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Character.class), "charValue", Type.CHAR_TYPE);
            } else if (returnClass == double.class) {

                // INVOKEVIRTUAL java/lang/Number doubleValue ()D

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Number.class), "doubleValue", Type.DOUBLE_TYPE);
            } else if (returnClass == float.class) {

                // INVOKEVIRTUAL java/lang/Number floatValue ()F

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Number.class), "floatValue", Type.FLOAT_TYPE);
            } else if (returnClass == int.class) {

                // INVOKEVIRTUAL java/lang/Number intValue ()I

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Number.class), "intValue", Type.INT_TYPE);
            } else if (returnClass == long.class) {

                // INVOKEVIRTUAL java/lang/Number longValue ()J

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Number.class), "longValue", Type.LONG_TYPE);
            } else if (returnClass == short.class) {

                // INVOKEVIRTUAL java/lang/Number intValue ()I

                _assertMethodInsnNode(iterator.next(), Opcodes.INVOKEVIRTUAL,
                        Type.getInternalName(Number.class), "intValue", Type.INT_TYPE);
            }

            // xRETURN

            _assertInsnNode(iterator.next(), returnType.getOpcode(Opcodes.IRETURN));

            // nullCheckLabel

            Assert.assertSame(nullCheckLabelNode, iterator.next());

            // xRETURN null/0

            if (!returnClass.isPrimitive()) {
                _assertInsnNode(iterator.next(), Opcodes.ACONST_NULL);
                _assertInsnNode(iterator.next(), Opcodes.ARETURN);
            } else if (returnClass == void.class) {
                _assertInsnNode(iterator.next(), Opcodes.RETURN);
            } else if (returnClass == float.class) {
                _assertInsnNode(iterator.next(), Opcodes.FCONST_0);
                _assertInsnNode(iterator.next(), Opcodes.FRETURN);
            } else if (returnClass == double.class) {
                _assertInsnNode(iterator.next(), Opcodes.DCONST_0);
                _assertInsnNode(iterator.next(), Opcodes.DRETURN);
            } else if (returnClass == long.class) {
                _assertInsnNode(iterator.next(), Opcodes.LCONST_0);
                _assertInsnNode(iterator.next(), Opcodes.LRETURN);
            } else {
                _assertInsnNode(iterator.next(), Opcodes.ICONST_0);
                _assertInsnNode(iterator.next(), Opcodes.IRETURN);
            }
        } else {
            if (returnClass != Object.class) {

                // CHECKCAST

                _assertTypeInsnNode(iterator.next(), Opcodes.CHECKCAST, returnClass);
            }

            // ARETURN

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

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

From source file:com.lucidtechnics.blackboard.TargetConstructor.java

License:Apache License

private static final byte[] createWrapperObjectByteArray(String _targetName, Class _class) {
    ClassWriter classWriter = new ClassWriter(true);

    FieldVisitor fieldVisitor;/*from  w  w  w.  j a  va2s.c o  m*/
    MethodVisitor methodVisitor;
    String superClassName = _class.getCanonicalName().replaceAll("\\.", "/");

    String[] superClassNameParts = superClassName.split("/");
    String simpleClassName = superClassNameParts[superClassNameParts.length - 1];
    String blackboardSubClassName = "com/lucidtechnics/blackboard/wrapper/" + superClassName + "Wrapper";

    classWriter.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, blackboardSubClassName, null,
            superClassName, new String[] { "com/lucidtechnics/blackboard/Target" });

    classWriter.visitSource(simpleClassName, null);

    {
        fieldVisitor = classWriter.visitField(Opcodes.ACC_PRIVATE, "blackboardObject", "Ljava/lang/Object;",
                null, null);
        fieldVisitor.visitEnd();
    }

    {
        fieldVisitor = classWriter.visitField(Opcodes.ACC_PRIVATE, "intercepter",
                "Lcom/lucidtechnics/blackboard/Intercepter;", null, null);
        fieldVisitor.visitEnd();
    }

    {
        methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "getBlackboardObject",
                "()Ljava/lang/Object;", null, null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitFieldInsn(Opcodes.GETFIELD, blackboardSubClassName, "blackboardObject",
                "Ljava/lang/Object;");
        methodVisitor.visitInsn(Opcodes.ARETURN);
        methodVisitor.visitMaxs(0, 0);
        methodVisitor.visitEnd();
    }

    {
        methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "getIntercepter",
                "()Lcom/lucidtechnics/blackboard/Intercepter;", null, null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitFieldInsn(Opcodes.GETFIELD, blackboardSubClassName, "intercepter",
                "Lcom/lucidtechnics/blackboard/Intercepter;");
        methodVisitor.visitInsn(Opcodes.ARETURN);
        methodVisitor.visitMaxs(0, 0);
        methodVisitor.visitEnd();
    }

    {
        methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "setBlackboardObject",
                "(Ljava/lang/Object;)V", null, null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, blackboardSubClassName, "blackboardObject",
                "Ljava/lang/Object;");
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitMaxs(0, 0);
        methodVisitor.visitEnd();
    }

    {
        methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "setIntercepter",
                "(Lcom/lucidtechnics/blackboard/Intercepter;)V", null, null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
        methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, blackboardSubClassName, "intercepter",
                "Lcom/lucidtechnics/blackboard/Intercepter;");
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitMaxs(0, 0);
        methodVisitor.visitEnd();
    }

    {
        methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superClassName, "<init>", "()V");
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitMaxs(0, 0);
        methodVisitor.visitEnd();
    }

    List mutatorMethodList = findMutatorMethods(_class);

    Iterator mutatorMethods = mutatorMethodList.iterator();

    while (mutatorMethods.hasNext() == true) {
        Method method = (Method) mutatorMethods.next();
        MethodInfo methodInfo = (MethodInfo) MethodInfoTransformer.getInstance().transform(method);
        Signature signature = methodInfo.getSignature();
        String methodName = signature.getName();

        String parameterType = signature.getArgumentTypes()[0].getDescriptor();
        String returnType = signature.getReturnType().getDescriptor();
        String[] exceptionTypeArray = createExceptionTypeArray(methodInfo.getExceptionTypes());

        {
            methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, methodName,
                    "(" + parameterType + ")" + returnType, null, exceptionTypeArray);

            methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
            methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, blackboardSubClassName, "getIntercepter",
                    "()Lcom/lucidtechnics/blackboard/Intercepter;");
            Label l1 = new Label();
            methodVisitor.visitJumpInsn(Opcodes.IFNULL, l1);

            methodVisitor.visitCode();
            methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
            methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, blackboardSubClassName, "getIntercepter",
                    "()Lcom/lucidtechnics/blackboard/Intercepter;");

            methodVisitor.visitLdcInsn(superClassName);
            methodVisitor.visitLdcInsn(blackboardSubClassName);
            methodVisitor.visitLdcInsn(methodName);
            methodVisitor.visitLdcInsn(_targetName);
            methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);

            int tempOpCode = getLoadOpcode(parameterType);
            methodVisitor.visitVarInsn(tempOpCode, 1);

            if (tempOpCode == Opcodes.ALOAD) {
                //this is an object.
                methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE,
                        "com/lucidtechnics/blackboard/Intercepter", "monitor",
                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)V");
            } else {
                //it is a primitive.
                methodVisitor.visitMethodInsn(Opcodes.INVOKEINTERFACE,
                        "com/lucidtechnics/blackboard/Intercepter", "monitor",
                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;"
                                + parameterType + ")V");
            }

            methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
            methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, blackboardSubClassName, "getBlackboardObject",
                    "()Ljava/lang/Object;");
            methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, superClassName);
            methodVisitor.visitVarInsn(getLoadOpcode(parameterType), 1);
            methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, superClassName, methodName,
                    "(" + parameterType + ")" + returnType);

            methodVisitor.visitLabel(l1);

            methodVisitor.visitInsn(getReturnOpcode(returnType));
            methodVisitor.visitMaxs(0, 0);
            methodVisitor.visitEnd();
        }
    }

    List otherPublicMethodList = findOtherPublicMethods(_class);

    Iterator otherPublicMethods = otherPublicMethodList.iterator();

    while (otherPublicMethods.hasNext() == true) {
        Method method = (Method) otherPublicMethods.next();
        MethodInfo methodInfo = (MethodInfo) MethodInfoTransformer.getInstance().transform(method);
        Signature signature = methodInfo.getSignature();
        String methodName = signature.getName();

        String parameterTypes = constructParameterTypes(signature.getArgumentTypes());
        String returnType = signature.getReturnType().getDescriptor();
        String[] exceptionTypeArray = createExceptionTypeArray(methodInfo.getExceptionTypes());

        if (logger.isDebugEnabled() == true) {
            logger.debug("Processing method: " + methodName);
        }
        if (logger.isDebugEnabled() == true) {
            logger.debug("Parameter types are: " + parameterTypes);
        }

        if ("toString".equalsIgnoreCase(methodName) == false) {
            methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, methodName,
                    "(" + parameterTypes + ")" + returnType, null, exceptionTypeArray);

            methodVisitor.visitCode();
            methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
            methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, blackboardSubClassName, "getBlackboardObject",
                    "()Ljava/lang/Object;");
            methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, superClassName);
            methodVisitor.visitVarInsn(Opcodes.ASTORE, signature.getArgumentTypes().length + 1);
            methodVisitor.visitVarInsn(Opcodes.ALOAD, signature.getArgumentTypes().length + 1);

            loadParameters(methodVisitor, signature.getArgumentTypes());

            methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, superClassName, methodName,
                    "(" + parameterTypes + ")" + returnType);
            methodVisitor.visitInsn(getReturnOpcode(returnType));
            methodVisitor.visitMaxs(0, 0);
            methodVisitor.visitEnd();
        }
    }

    List protectedMethodList = findProtectedMethods(_class);

    Iterator protectedMethods = protectedMethodList.iterator();

    while (protectedMethods.hasNext() == true) {
        Method method = (Method) protectedMethods.next();
        MethodInfo methodInfo = (MethodInfo) MethodInfoTransformer.getInstance().transform(method);
        Signature signature = methodInfo.getSignature();
        String methodName = signature.getName();

        String parameterTypes = constructParameterTypes(signature.getArgumentTypes());
        String returnType = signature.getReturnType().getDescriptor();
        String[] exceptionTypeArray = createExceptionTypeArray(methodInfo.getExceptionTypes());

        if (logger.isDebugEnabled() == true) {
            logger.debug("Processing method: " + methodName + " and parameter types are: " + parameterTypes);
        }

        {
            methodVisitor = classWriter.visitMethod(Opcodes.ACC_PROTECTED, methodName,
                    "(" + parameterTypes + ")" + returnType, null, exceptionTypeArray);

            methodVisitor.visitCode();
            methodVisitor.visitTypeInsn(Opcodes.NEW, "com/lucidtechnics/blackboard/BlackboardException");
            methodVisitor.visitInsn(Opcodes.DUP);
            methodVisitor.visitLdcInsn("Unable to access protected methods on Blackboard object");
            methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL,
                    "com/lucidtechnics/blackboard/BlackboardException", "<init>", "(Ljava/lang/String;)V");
            methodVisitor.visitInsn(Opcodes.ATHROW);
            methodVisitor.visitMaxs(0, 0);
            methodVisitor.visitEnd();
        }
    }

    {
        methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC, "toString", "()Ljava/lang/String;", null,
                null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, blackboardSubClassName, "getBlackboardObject",
                "()Ljava/lang/Object;");
        methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, "com/lucidtechnics/blackboard/util/Utility",
                "toString", "(Ljava/lang/Object;)Ljava/lang/String;");
        methodVisitor.visitInsn(getReturnOpcode("Ljava/lang/String;"));
        methodVisitor.visitMaxs(0, 0);
        methodVisitor.visitEnd();
    }

    classWriter.visitEnd();

    if (logger.isDebugEnabled() == true) {
        logger.debug("Finished creating new class: " + blackboardSubClassName + " for target class: "
                + superClassName + ".");
    }

    return classWriter.toByteArray();
}

From source file:com.lucidtechnics.blackboard.TargetConstructor.java

License:Apache License

private static int getReturnOpcode(String _returnType) {
    int opCode = 0;

    char firstChar = _returnType.toCharArray()[0];

    switch (firstChar) {
    case 'Z':
    case 'B':
    case 'C':
    case 'I':
    case 'S':
        opCode = Opcodes.IRETURN;// www .  jav a  2 s.co  m
        break;

    case 'D':
        opCode = Opcodes.DRETURN;
        break;

    case 'F':
        opCode = Opcodes.FRETURN;
        break;

    case 'J':
        opCode = Opcodes.LRETURN;
        break;

    case 'L':
        opCode = Opcodes.ARETURN;
        break;

    case 'V':
        opCode = Opcodes.RETURN;
        break;

    case '[':
        opCode = Opcodes.ARETURN;
        break;

    default:
        if (true)
            throw new RuntimeException("Cannot handle return type identified by first character: " + firstChar);
        break;
    }

    return opCode;
}

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

License:Apache License

public void performInsn(int opcode) {
    switch (opcode) {
    case Opcodes.NOP:
        break;//from   w  ww.j a v a2s .c om

    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.AdvancedAdviceAdapter.java

License:Open Source License

protected ExitStatus translateExitCode(int opcode) {
    switch (opcode) {
    case Opcodes.ATHROW:
        return ExitStatus.EXIT_WITH_EXCEPTION;

    case Opcodes.IRETURN:
    case Opcodes.FRETURN:
    case Opcodes.LRETURN:
    case Opcodes.DRETURN:
    case Opcodes.ARETURN:
        return ExitStatus.EXIT_WITH_RETURN_VALUE;

    case Opcodes.RETURN:
        return ExitStatus.EXIT_VOID;

    default:/*from w  w w.  j  a va 2s  .c o  m*/
        return ExitStatus.EXIT_UNKNOWN;
    }
}

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

License:Apache License

/**
 * To each class, add the dispatch method called by the original code that acts as a trampoline to
 * invoke the changed methods.//from w  w w . j  a  v a 2s  .  c o m
 * <p/>
 * Pseudo code:
 * <code>
 * Object access$dispatch(String name, object[] args) {
 * if (name.equals(
 * "firstMethod.(L$type;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;")) {
 * return firstMethod(($type)arg[0], (String)arg[1], arg[2]);
 * }
 * if (name.equals("secondMethod.(L$type;Ljava/lang/String;I;)V")) {
 * secondMethod(($type)arg[0], (String)arg[1], (int)arg[2]);
 * return;
 * }
 * ...
 * StringBuilder $local1 = new StringBuilder();
 * $local1.append("Method not found ");
 * $local1.append(name);
 * $local1.append(" in " + visitedClassName +
 * "$dispatch implementation, restart the application");
 * throw new $package/InstantReloadException($local1.toString());
 * }
 * </code>
 */
private void addDispatchMethod() {
    int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_VARARGS;
    Method m = new Method("access$dispatch", "(I[Ljava/lang/Object;)Ljava/lang/Object;");
    MethodVisitor visitor = super.visitMethod(access, m.getName(), m.getDescriptor(), null, null);

    final GeneratorAdapter mv = new GeneratorAdapter(access, m, visitor);

    if (TRACING_ENABLED) {
        mv.push("Redirecting ");
        mv.loadArg(0);
        trace(mv, 2);
    }

    List<MethodNode> allMethods = new ArrayList();

    // if we are disabled, do not generate any dispatch, the method will throw an exception
    // if invoked which should never happen.
    if (!instantRunDisabled) {
        //noinspection unchecked
        allMethods.addAll(classNode.methods);
        allMethods.addAll(addedMethods);
    }

    final Map<String, MethodNode> methods = new HashMap();
    for (MethodNode methodNode : allMethods) {
        if (methodNode.name.equals("<clinit>") || methodNode.name.equals("<init>")) {
            continue;
        }
        if (!isAccessCompatibleWithInstantRun(methodNode.access)) {
            continue;
        }
        methods.put(methodNode.name + "." + methodNode.desc, methodNode);
    }

    new IntSwitch() {
        @Override
        void visitString() {
            mv.visitVarInsn(Opcodes.ALOAD, 1);
        }

        @Override
        void visitInt() {
            mv.visitVarInsn(Opcodes.ILOAD, 1);
        }

        @Override
        void visitCase(String methodName) {
            MethodNode methodNode = methods.get(methodName);
            String name = methodNode.name;
            boolean isStatic = (methodNode.access & Opcodes.ACC_STATIC) != 0;
            String newDesc = computeOverrideMethodDesc(methodNode.desc, isStatic);

            if (TRACING_ENABLED) {
                trace(mv, "M: " + name + " P:" + newDesc);
            }
            Type[] args = Type.getArgumentTypes(newDesc);
            int argc = 0;
            for (Type t : args) {
                mv.visitVarInsn(Opcodes.ALOAD, 2);
                mv.push(argc);
                mv.visitInsn(Opcodes.AALOAD);
                ByteCodeUtils.unbox(mv, t);
                argc++;
            }
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, visitedClassName + "$override",
                    isStatic ? computeOverrideMethodName(name, methodNode.desc) : name, newDesc, false);
            Type ret = Type.getReturnType(methodNode.desc);
            if (ret.getSort() == Type.VOID) {
                mv.visitInsn(Opcodes.ACONST_NULL);
            } else {
                mv.box(ret);
            }
            mv.visitInsn(Opcodes.ARETURN);
        }

        @Override
        void visitDefault() {
            writeMissingMessageWithHash(mv, visitedClassName);
        }
    }.visit(mv, methods.keySet(), visitedClassName);

    mv.visitMaxs(0, 0);
    mv.visitEnd();

}

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.");
    }/*from   w  w w .j  a v a 2s . c  o  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();

}