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:org.cacheonix.impl.transformer.ByteInstructionTest.java

License:LGPL

/**
 * Test method for {@link ByteInstruction#getReturnCode(String desc)} .
 *///from   www. j  av a 2 s  . co  m
public void testGetReturnCode() {

    String desc = "(IF)V";
    int ret = ByteInstruction.getReturnCode(desc);
    assertEquals(ret, Opcodes.RETURN);

    desc = "(IF)I";
    ret = ByteInstruction.getReturnCode(desc);
    assertEquals(ret, Opcodes.IRETURN);

    desc = "(IF)D";
    ret = ByteInstruction.getReturnCode(desc);
    assertEquals(ret, Opcodes.DRETURN);

    desc = "(IF)Ljava/lang/Object;";
    ret = ByteInstruction.getReturnCode(desc);
    assertEquals(ret, Opcodes.ARETURN);

}

From source file:org.codehaus.groovy.reflection.MethodHandleFactory.java

License:Apache License

@Deprecated
public static void genInvokeXxxWithArray(ClassWriter cw, Method method) {
    MethodVisitor mv;/*from  w  w w  . j  av a2s . c o m*/
    mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "invoke",
            "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", null, EXCEPTIONS);
    mv.visitCode();

    Class callClass = method.getDeclaringClass();
    boolean useInterface = callClass.isInterface();

    String type = BytecodeHelper.getClassInternalName(callClass.getName());
    String descriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(), method.getParameterTypes());

    // make call
    if (Modifier.isStatic(method.getModifiers())) {
        genLoadParameters(2, mv, method);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, type, method.getName(), descriptor);
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        BytecodeHelper.doCast(mv, callClass);
        genLoadParameters(2, mv, method);
        mv.visitMethodInsn((useInterface) ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, type,
                method.getName(), descriptor);
    }

    BytecodeHelper.box(mv, method.getReturnType());
    if (method.getReturnType() == void.class) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    }

    mv.visitInsn(Opcodes.ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}

From source file:org.codehaus.groovy.reflection.MethodHandleFactory.java

License:Apache License

private static void genInvokeWithFixedParams(ClassWriter cw, Method method) {
    MethodVisitor mv;//from  w  ww .  j av  a2s  .c  om
    final int pc = method.getParameterTypes().length;
    if (pc <= 4) {
        StringBuilder pdescb = new StringBuilder();
        for (int i = 0; i != pc; ++i)
            pdescb.append("Ljava/lang/Object;");

        String pdesc = pdescb.toString();

        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "invoke", "(Ljava/lang/Object;" + pdesc + ")Ljava/lang/Object;",
                null, EXCEPTIONS);
        mv.visitCode();

        Class callClass = method.getDeclaringClass();
        boolean useInterface = callClass.isInterface();

        String type = BytecodeHelper.getClassInternalName(callClass.getName());
        String descriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(),
                method.getParameterTypes());

        // make call
        if (Modifier.isStatic(method.getModifiers())) {
            MethodHandleFactory.genLoadParametersDirect(2, mv, method);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, type, method.getName(), descriptor);
        } else {
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            BytecodeHelper.doCast(mv, callClass);
            MethodHandleFactory.genLoadParametersDirect(2, mv, method);
            mv.visitMethodInsn((useInterface) ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, type,
                    method.getName(), descriptor);
        }

        BytecodeHelper.box(mv, method.getReturnType());
        if (method.getReturnType() == void.class) {
            mv.visitInsn(Opcodes.ACONST_NULL);
        }

        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
}

From source file:org.codehaus.groovy.reflection.MethodHandleFactory.java

License:Apache License

private static void genInvokeWithFixedPrimitiveParams(ClassWriter cw, Method method) {
    MethodVisitor mv;/* w ww.j av a 2  s.  c  om*/
    final Class<?>[] pt = method.getParameterTypes();
    final int pc = pt.length;
    if (pc > 0 && pc <= 3) {
        StringBuilder pdescb = new StringBuilder();
        boolean hasPrimitive = false;
        for (int i = 0; i != pc; ++i)
            if (pt[i].isPrimitive()) {
                hasPrimitive = true;
                pdescb.append(BytecodeHelper.getTypeDescription(pt[i]));
            } else
                pdescb.append("Ljava/lang/Object;");

        if (!hasPrimitive)
            return;

        String pdesc = pdescb.toString();

        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "invoke", "(Ljava/lang/Object;" + pdesc + ")Ljava/lang/Object;",
                null, EXCEPTIONS);
        mv.visitCode();

        Class callClass = method.getDeclaringClass();
        boolean useInterface = callClass.isInterface();

        String type = BytecodeHelper.getClassInternalName(callClass.getName());
        String descriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(),
                method.getParameterTypes());

        // make call
        if (Modifier.isStatic(method.getModifiers())) {
            MethodHandleFactory.genLoadParametersPrimitiveDirect(2, mv, method);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, type, method.getName(), descriptor);
        } else {
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            BytecodeHelper.doCast(mv, callClass);
            MethodHandleFactory.genLoadParametersPrimitiveDirect(2, mv, method);
            mv.visitMethodInsn((useInterface) ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, type,
                    method.getName(), descriptor);
        }

        BytecodeHelper.box(mv, method.getReturnType());
        if (method.getReturnType() == void.class) {
            mv.visitInsn(Opcodes.ACONST_NULL);
        }

        mv.visitInsn(Opcodes.ARETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
}

From source file:org.codehaus.groovy.runtime.callsite.CallSiteGenerator.java

License:Apache License

private static MethodVisitor writeMethod(ClassWriter cw, String name, int argumentCount,
        final String superClass, CachedMethod cachedMethod, String receiverType, String parameterDescription,
        boolean useArray) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "call" + name,
            "(L" + receiverType + ";" + parameterDescription + ")Ljava/lang/Object;", null, null);
    mv.visitCode();/*from  w  w w  .  j av a2  s .  c  o  m*/

    final Label tryStart = new Label();
    mv.visitLabel(tryStart);

    // call for checking if method is still valid
    for (int i = 0; i < argumentCount; ++i)
        mv.visitVarInsn(Opcodes.ALOAD, i);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, superClass, "checkCall",
            "(Ljava/lang/Object;" + parameterDescription + ")Z", false);
    Label l0 = new Label();
    mv.visitJumpInsn(Opcodes.IFEQ, l0);

    // valid method branch

    Class callClass = cachedMethod.getDeclaringClass().getTheClass();
    boolean useInterface = callClass.isInterface();

    String type = BytecodeHelper.getClassInternalName(callClass.getName());
    String descriptor = BytecodeHelper.getMethodDescriptor(cachedMethod.getReturnType(),
            cachedMethod.getNativeParameterTypes());

    // prepare call
    int invokeMethodCode = Opcodes.INVOKEVIRTUAL;
    if (cachedMethod.isStatic()) {
        invokeMethodCode = Opcodes.INVOKESTATIC;
    } else {
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        BytecodeHelper.doCast(mv, callClass);
        if (useInterface)
            invokeMethodCode = Opcodes.INVOKEINTERFACE;
    }

    Class<?>[] parameters = cachedMethod.getPT();
    int size = parameters.length;
    for (int i = 0; i < size; i++) {
        if (useArray) {
            // unpack argument from Object[]
            mv.visitVarInsn(Opcodes.ALOAD, 2);
            BytecodeHelper.pushConstant(mv, i);
            mv.visitInsn(Opcodes.AALOAD);
        } else {
            mv.visitVarInsn(Opcodes.ALOAD, i + 2);
        }

        // cast argument to parameter class, inclusive unboxing
        // for methods with primitive types
        BytecodeHelper.doCast(mv, parameters[i]);
    }

    // make call
    mv.visitMethodInsn(invokeMethodCode, type, cachedMethod.getName(), descriptor, useInterface);

    // produce result
    BytecodeHelper.box(mv, cachedMethod.getReturnType());
    if (cachedMethod.getReturnType() == void.class) {
        mv.visitInsn(Opcodes.ACONST_NULL);
    }

    // return
    mv.visitInsn(Opcodes.ARETURN);

    // fall back after method change
    mv.visitLabel(l0);
    for (int i = 0; i < argumentCount; ++i)
        mv.visitVarInsn(Opcodes.ALOAD, i);
    if (!useArray) {
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/ArrayUtil", "createArray",
                "(" + parameterDescription + ")[Ljava/lang/Object;", false);
    }
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/callsite/CallSiteArray",
            "defaultCall" + name, "(Lorg/codehaus/groovy/runtime/callsite/CallSite;L" + receiverType
                    + ";[Ljava/lang/Object;)Ljava/lang/Object;",
            false);
    mv.visitInsn(Opcodes.ARETURN);

    // exception unwrapping for stackless exceptions
    final Label tryEnd = new Label();
    mv.visitLabel(tryEnd);
    final Label catchStart = new Label();
    mv.visitLabel(catchStart);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "org/codehaus/groovy/runtime/ScriptBytecodeAdapter", "unwrap",
            "(Lgroovy/lang/GroovyRuntimeException;)Ljava/lang/Throwable;", false);
    mv.visitInsn(Opcodes.ATHROW);
    mv.visitTryCatchBlock(tryStart, tryEnd, catchStart, "groovy/lang/GroovyRuntimeException");

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

From source file:org.coldswap.asm.method.PublicFloatMethodReplacer.java

License:Open Source License

private InsnList replaceReturn(InsnList insnList, Type retType) {
    final Type rretType = retType;
    int retOpcode = MethodUtil.getRetOpcodeToReplace(retType);
    for (int i = 0; i < insnList.size(); i++) {
        AbstractInsnNode absIns = insnList.get(i);
        int opcode = absIns.getOpcode();
        if (opcode == retOpcode) {
            // if tries to return a Reference type into a primitive then
            // remove the unbox( we return an Object). If a primitive is returned
            // into a  primitive then we must try to box from primitive to Object/Integer, etc..

            // check if an unbox takes place before return
            final boolean[] isBoxUnbox = { false, false };
            AbstractInsnNode valueOf = null;
            AbstractInsnNode primitiveValue = null;
            if (i > 1) {
                valueOf = insnList.get(i - 1);
                primitiveValue = insnList.get(i - 2);
                if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) {
                    valueOf.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if (AutoBoxing.isPrimitive(rretType.getDescriptor())) {
                                if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) {
                                    isBoxUnbox[0] = true;
                                }/*from  ww  w.  ja  v  a  2s . c om*/
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }

                if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                    primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) {
                                isBoxUnbox[1] = true;
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }
            }

            if (isBoxUnbox[0] && isBoxUnbox[1]) {
                // remove indexes
                insnList.remove(valueOf);
                insnList.remove(primitiveValue);
            } else {
                InsnList iList = new InsnList();
                iList.add(AutoBoxing.box(retType));
                iList.add(new InsnNode(Opcodes.ARETURN));
                insnList.insertBefore(absIns, iList);
                insnList.remove(absIns);

            }
        }
    }
    return insnList;
}

From source file:org.coldswap.asm.method.PublicObjectMethodReplacer.java

License:Open Source License

private InsnList replaceReturn(InsnList insnList, Type retType) {
    final Type rretType = retType;
    int retOpcode = MethodUtil.getRetOpcodeToReplace(retType);
    for (int i = 0; i < insnList.size(); i++) {
        AbstractInsnNode absIns = insnList.get(i);
        int opcode = absIns.getOpcode();
        if (opcode == retOpcode) {
            // if tries to return a Reference type into a primitive then
            // remove the unbox( we return an Object). If a primitive is returned
            // into a  primitive then we must try to box from primitive to Object/Integer, etc..

            // check if an unbox takes place before return
            final boolean[] isBoxUnbox = { false, false };
            AbstractInsnNode valueOf = null;
            AbstractInsnNode primitiveValue = null;
            if (i > 1) {
                valueOf = insnList.get(i - 1);
                primitiveValue = insnList.get(i - 2);
                if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) {
                    valueOf.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if (AutoBoxing.isPrimitive(rretType.getDescriptor())) {
                                if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) {
                                    isBoxUnbox[0] = true;
                                }/*from w w  w  .  j a  v  a2s .c  om*/
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }

                if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                    primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) {
                                isBoxUnbox[1] = true;
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }
            }

            if (isBoxUnbox[0] && isBoxUnbox[1]) {
                // remove indexes
                insnList.remove(valueOf);
                insnList.remove(primitiveValue);
            } else {
                InsnList iList = new InsnList();
                iList.add(AutoBoxing.box(retType));
                iList.add(new InsnNode(Opcodes.ARETURN));
                insnList.insertBefore(absIns, iList);
                insnList.remove(absIns);
            }
        }
    }
    return insnList;
}

From source file:org.coldswap.util.MethodUtil.java

License:Open Source License

public static MethodNode createObjectHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getObjectMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "([Ljava/lang/Object;)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);/*ww w.  j  av a 2  s .  c  o  m*/
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("args", "[Ljava/lang/Object;", null, l0, l1, 1));
    mn.maxStack = 1;
    mn.maxLocals = 2;
    return mn;

}

From source file:org.coldswap.util.MethodUtil.java

License:Open Source License

public static MethodNode createIntHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getIntMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "(I)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);// w  w w .j  a  v a 2 s. c om
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("arg", "I", null, l0, l1, 1));
    mn.maxStack = 1;
    mn.maxLocals = 2;
    return mn;
}

From source file:org.coldswap.util.MethodUtil.java

License:Open Source License

public static MethodNode createLongHelperMethod(String className, int counter) {
    int acc = Opcodes.ACC_PUBLIC;
    String methodName = TransformerNameGenerator.getLongMethodNameWithCounter(className, counter);
    MethodNode mn = new MethodNode(acc, methodName, "(J)Ljava/lang/Object;", null, null);
    InsnList insnList = mn.instructions;
    LabelNode l0 = new LabelNode();
    insnList.add(l0);//from  ww  w  .j a v a 2  s. c  o  m
    insnList.add(new InsnNode(Opcodes.ACONST_NULL));
    insnList.add(new InsnNode(Opcodes.ARETURN));
    LabelNode l1 = new LabelNode();
    insnList.add(l1);
    String classLiteral = "L" + className + ";";
    mn.localVariables.add(new LocalVariableNode("this", classLiteral, null, l0, l1, 0));
    mn.localVariables.add(new LocalVariableNode("arg", "J", null, l0, l1, 1));
    mn.maxStack = 1;
    mn.maxLocals = 3;
    return mn;
}