Example usage for org.objectweb.asm Opcodes RETURN

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

Introduction

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

Prototype

int RETURN

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

Click Source Link

Usage

From source file:org.compass.core.util.reflection.asm.AsmReflectionFieldGenerator.java

License:Apache License

private static void createConstructor(ClassVisitor cw) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/reflect/Field;)V", null, null);
    mv.visitCode();//ww  w . j  a  va  2s. com
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(PlainReflectionField.class), "<init>",
            "(Ljava/lang/reflect/Field;)V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionFieldGenerator.java

License:Apache License

private static void createSetMethod(ClassVisitor cw, Class entryClass, Field field) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "set", "(Ljava/lang/Object;Ljava/lang/Object;)V",
            null, new String[] { "java/lang/IllegalArgumentException", "java/lang/IllegalAccessException" });
    mv.visitCode();/*from  ww w .j  a  v a  2s.c o m*/
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(entryClass));
    mv.visitVarInsn(Opcodes.ALOAD, 2);
    castAndUnboxIfNeeded(mv, field);
    mv.visitFieldInsn(Opcodes.PUTFIELD, Type.getInternalName(entryClass), field.getName(),
            Type.getDescriptor(field.getType()));
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 3);
    mv.visitEnd();
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionMethodGenerator.java

License:Apache License

/**
 * Creates the class constructor which delegates the input to the super.
 *//*  w w w. ja  va  2s .  c o  m*/
private static void createCtor(ClassWriter cw) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/lang/reflect/Method;)V", null,
            null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitVarInsn(Opcodes.ALOAD, 1);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(PlainReflectionMethod.class), "<init>",
            "(Ljava/lang/reflect/Method;)V");
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 2);
    mv.visitEnd();
}

From source file:org.compass.core.util.reflection.asm.AsmReflectionMethodGenerator.java

License:Apache License

/**
 * Creates the method invoking wrapper method.
 *//*from www. ja v a 2s  .  co  m*/
private static void createMethod(Class clazz, String name, Method refMethod, ClassWriter cw, String methodName,
        String desc, boolean argsParams, boolean returnValue, Class... parameterTypes) {

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_VARARGS, methodName, desc, null, null);

    boolean isStatic = Modifier.isStatic(refMethod.getModifiers());
    boolean isInteface = Modifier.isInterface(refMethod.getDeclaringClass().getModifiers());

    final int invokeCode;
    if (isStatic) {
        invokeCode = Opcodes.INVOKESTATIC;
    } else {
        invokeCode = isInteface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL;
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitTypeInsn(Opcodes.CHECKCAST, Type.getInternalName(clazz));
    }

    if (argsParams) {
        for (int i = 0; i < parameterTypes.length; ++i) {
            mv.visitVarInsn(Opcodes.ALOAD, 2);
            mv.visitIntInsn(Opcodes.BIPUSH, i);
            mv.visitInsn(Opcodes.AALOAD);
            prepareParameter(mv, Type.getType(parameterTypes[i]));
        }
    } else {
        for (int i = 0; i < parameterTypes.length; ++i) {
            mv.visitVarInsn(Opcodes.ALOAD, i + 2);
            prepareParameter(mv, Type.getType(parameterTypes[i]));
        }
    }

    mv.visitMethodInsn(invokeCode, Type.getInternalName(clazz), name, Type.getMethodDescriptor(refMethod));

    if (returnValue) {
        prepareResult(mv, refMethod);
        mv.visitInsn(Opcodes.ARETURN);
    } else {
        mv.visitInsn(Opcodes.RETURN);
    }

    mv.visitMaxs(1, 1); // ignored since ClassWriter set as ClassWriter.COMPUTE_MAXS
    mv.visitEnd();
}

From source file:org.decojer.cavaj.readers.asm.ReadMethodVisitor.java

License:Open Source License

@Override
public void visitInsn(final int opcode) {
    T t = null;/* w  w  w. j av a2s  .c o m*/
    int iValue = Integer.MIN_VALUE;
    Object oValue = null;

    switch (opcode) {
    case Opcodes.NOP:
        // nothing to do, ignore
        break;
    /*******
     * ADD *
     *******/
    case Opcodes.DADD:
        t = T.DOUBLE;
        // fall through
    case Opcodes.FADD:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IADD:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LADD:
        if (t == null) {
            t = T.LONG;
        }
        add(new ADD(this.ops.size(), opcode, this.line, t));
        break;
    /*********
     * ALOAD *
     *********/
    case Opcodes.AALOAD:
        t = T.REF;
        // fall through
    case Opcodes.BALOAD:
        if (t == null) {
            t = T.SMALL;
        }
        // fall through
    case Opcodes.CALOAD:
        if (t == null) {
            t = T.CHAR;
        }
        // fall through
    case Opcodes.DALOAD:
        if (t == null) {
            t = T.DOUBLE;
        }
        // fall through
    case Opcodes.FALOAD:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IALOAD:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LALOAD:
        if (t == null) {
            t = T.LONG;
        }
        // fall through
    case Opcodes.SALOAD:
        if (t == null) {
            t = T.SHORT;
        }
        add(new ALOAD(this.ops.size(), opcode, this.line, t));
        break;
    /*******
     * AND *
     *******/
    case Opcodes.IAND:
        t = T.AINT;
        // fall through
    case Opcodes.LAND:
        if (t == null) {
            t = T.LONG;
        }
        add(new AND(this.ops.size(), opcode, this.line, t));
        break;
    /***************
     * ARRAYLENGTH *
     ***************/
    case Opcodes.ARRAYLENGTH:
        add(new ARRAYLENGTH(this.ops.size(), opcode, this.line));
        break;
    /**********
     * ASTORE *
     **********/
    case Opcodes.AASTORE:
        t = T.REF;
        // fall through
    case Opcodes.BASTORE:
        if (t == null) {
            t = T.SMALL;
        }
        // fall through
    case Opcodes.CASTORE:
        if (t == null) {
            t = T.CHAR;
        }
        // fall through
    case Opcodes.DASTORE:
        if (t == null) {
            t = T.DOUBLE;
        }
        // fall through
    case Opcodes.FASTORE:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IASTORE:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LASTORE:
        if (t == null) {
            t = T.LONG;
        }
        // fall through
    case Opcodes.SASTORE:
        if (t == null) {
            t = T.SHORT;
        }
        add(new ASTORE(this.ops.size(), opcode, this.line, t));
        break;
    /********
     * CAST *
     ********/
    case Opcodes.D2F:
        t = T.DOUBLE;
        oValue = T.FLOAT;
        // fall through
    case Opcodes.D2I:
        if (t == null) {
            t = T.DOUBLE;
            oValue = T.INT;
        }
        // fall through
    case Opcodes.D2L:
        if (t == null) {
            t = T.DOUBLE;
            oValue = T.LONG;
        }
        // fall through
    case Opcodes.F2D:
        if (t == null) {
            t = T.FLOAT;
            oValue = T.DOUBLE;
        }
        // fall through
    case Opcodes.F2I:
        if (t == null) {
            t = T.FLOAT;
            oValue = T.INT;
        }
        // fall through
    case Opcodes.F2L:
        if (t == null) {
            t = T.FLOAT;
            oValue = T.LONG;
        }
        // fall through
    case Opcodes.I2B:
        if (t == null) {
            t = T.INT;
            oValue = T.BYTE;
        }
        // fall through
    case Opcodes.I2C:
        if (t == null) {
            t = T.INT;
            oValue = T.CHAR;
        }
        // fall through
    case Opcodes.I2D:
        if (t == null) {
            t = T.INT;
            oValue = T.DOUBLE;
        }
        // fall through
    case Opcodes.I2F:
        if (t == null) {
            t = T.INT;
            oValue = T.FLOAT;
        }
        // fall through
    case Opcodes.I2L:
        if (t == null) {
            t = T.INT;
            oValue = T.LONG;
        }
        // fall through
    case Opcodes.I2S:
        if (t == null) {
            t = T.INT;
            oValue = T.SHORT;
        }
        // fall through
    case Opcodes.L2D:
        if (t == null) {
            t = T.LONG;
            oValue = T.DOUBLE;
        }
        // fall through
    case Opcodes.L2F:
        if (t == null) {
            t = T.LONG;
            oValue = T.FLOAT;
        }
        // fall through
    case Opcodes.L2I:
        if (t == null) {
            t = T.LONG;
            oValue = T.INT;
        }
        assert oValue instanceof T;
        add(new CAST(this.ops.size(), opcode, this.line, t, (T) oValue));
        break;
    /*******
     * CMP *
     *******/
    case Opcodes.DCMPG:
        t = T.DOUBLE;
        iValue = CMP.T_G;
        // fall through
    case Opcodes.DCMPL:
        if (t == null) {
            t = T.DOUBLE;
            iValue = CMP.T_L;
        }
        // fall through
    case Opcodes.FCMPG:
        if (t == null) {
            t = T.FLOAT;
            iValue = CMP.T_G;
        }
        // fall through
    case Opcodes.FCMPL:
        if (t == null) {
            t = T.FLOAT;
            iValue = CMP.T_L;
        }
        // fall through
    case Opcodes.LCMP:
        if (t == null) {
            t = T.LONG;
            iValue = CMP.T_0;
        }
        add(new CMP(this.ops.size(), opcode, this.line, t, iValue));
        break;
    /*******
     * DIV *
     *******/
    case Opcodes.DDIV:
        t = T.DOUBLE;
        // fall through
    case Opcodes.FDIV:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IDIV:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LDIV:
        if (t == null) {
            t = T.LONG;
        }
        add(new DIV(this.ops.size(), opcode, this.line, t));
        break;
    /*******
     * DUP *
     *******/
    case Opcodes.DUP:
        oValue = DUP.Kind.DUP;
        // fall through
    case Opcodes.DUP_X1:
        if (oValue == null) {
            oValue = DUP.Kind.DUP_X1;
        }
        // fall through
    case Opcodes.DUP_X2:
        if (oValue == null) {
            oValue = DUP.Kind.DUP_X2;
        }
        // fall through
    case Opcodes.DUP2:
        if (oValue == null) {
            oValue = DUP.Kind.DUP2;
        }
        // fall through
    case Opcodes.DUP2_X1:
        if (oValue == null) {
            oValue = DUP.Kind.DUP2_X1;
        }
        // fall through
    case Opcodes.DUP2_X2:
        if (oValue == null) {
            oValue = DUP.Kind.DUP2_X2;
        }
        add(new DUP(this.ops.size(), opcode, this.line, (DUP.Kind) oValue));
        break;
    /***********
     * MONITOR *
     ***********/
    case Opcodes.MONITORENTER:
        oValue = MONITOR.Kind.ENTER;
        // fall through
    case Opcodes.MONITOREXIT:
        if (oValue == null) {
            oValue = MONITOR.Kind.EXIT;
        }
        add(new MONITOR(this.ops.size(), opcode, this.line, (MONITOR.Kind) oValue));
        break;
    /*******
     * MUL *
     *******/
    case Opcodes.DMUL:
        t = T.DOUBLE;
        // fall through
    case Opcodes.FMUL:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IMUL:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LMUL:
        if (t == null) {
            t = T.LONG;
        }
        add(new MUL(this.ops.size(), opcode, this.line, t));
        break;
    /*******
     * NEG *
     *******/
    case Opcodes.DNEG:
        t = T.DOUBLE;
        // fall through
    case Opcodes.FNEG:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.INEG:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LNEG:
        if (t == null) {
            t = T.LONG;
        }
        add(new NEG(this.ops.size(), opcode, this.line, t));
        break;
    /******
     * OR *
     ******/
    case Opcodes.IOR:
        t = T.AINT;
        // fall through
    case Opcodes.LOR:
        if (t == null) {
            t = T.LONG;
        }
        add(new OR(this.ops.size(), opcode, this.line, t));
        break;
    /*******
     * POP *
     *******/
    case Opcodes.POP:
        oValue = POP.Kind.POP;
        // fall through
    case Opcodes.POP2:
        if (oValue == null) {
            oValue = POP.Kind.POP2;
        }
        add(new POP(this.ops.size(), opcode, this.line, (POP.Kind) oValue));
        break;
    /********
     * PUSH *
     ********/
    case Opcodes.ACONST_NULL:
        t = T.REF;
        // fall through
    case Opcodes.DCONST_0:
        if (t == null) {
            oValue = 0D;
            t = T.DOUBLE;
        }
        // fall through
    case Opcodes.FCONST_0:
        if (t == null) {
            oValue = 0F;
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.ICONST_0:
        if (t == null) {
            oValue = 0;
            t = T.getJvmIntT(0);
        }
        // fall through
    case Opcodes.LCONST_0:
        if (t == null) {
            oValue = 0L;
            t = T.LONG;
        }
        // fall through
    case Opcodes.DCONST_1:
        if (t == null) {
            oValue = 1D;
            t = T.DOUBLE;
        }
        // fall through
    case Opcodes.FCONST_1:
        if (t == null) {
            oValue = 1F;
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.ICONST_1:
        if (t == null) {
            oValue = 1;
            t = T.getJvmIntT(1);
        }
        // fall through
    case Opcodes.LCONST_1:
        if (t == null) {
            oValue = 1L;
            t = T.LONG;
        }
        // fall through
    case Opcodes.FCONST_2:
        if (t == null) {
            oValue = 2F;
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.ICONST_2:
        if (t == null) {
            oValue = 2;
            t = T.getJvmIntT(2);
        }
        // fall through
    case Opcodes.ICONST_3:
        if (t == null) {
            oValue = 3;
            t = T.getJvmIntT(3);
        }
        // fall through
    case Opcodes.ICONST_4:
        if (t == null) {
            oValue = 4;
            t = T.getJvmIntT(4);
        }
        // fall through
    case Opcodes.ICONST_5:
        if (t == null) {
            oValue = 5;
            t = T.getJvmIntT(5);
        }
        // fall through
    case Opcodes.ICONST_M1:
        if (t == null) {
            oValue = -1;
            t = T.getJvmIntT(-1);
        }
        add(new PUSH(this.ops.size(), opcode, this.line, t, oValue));
        break;
    /*******
     * REM *
     *******/
    case Opcodes.DREM:
        t = T.DOUBLE;
        // fall through
    case Opcodes.FREM:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IREM:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LREM:
        if (t == null) {
            t = T.LONG;
        }
        add(new REM(this.ops.size(), opcode, this.line, t));
        break;
    /**********
     * RETURN *
     **********/
    case Opcodes.ARETURN:
        t = T.REF;
        // fall through
    case Opcodes.DRETURN:
        if (t == null) {
            t = T.DOUBLE;
        }
        // fall through
    case Opcodes.FRETURN:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.IRETURN:
        if (t == null) {
            t = T.AINT;
        }
        // fall through
    case Opcodes.LRETURN:
        if (t == null) {
            t = T.LONG;
        }
        // fall through
    case Opcodes.RETURN:
        if (t == null) {
            t = T.VOID;
        }
        add(new RETURN(this.ops.size(), opcode, this.line, t));
        break;
    /*******
     * SHL *
     *******/
    case Opcodes.ISHL:
        t = T.INT;
        // fall through
    case Opcodes.LSHL:
        if (t == null) {
            t = T.LONG;
        }
        add(new SHL(this.ops.size(), opcode, this.line, t, T.INT));
        break;
    /*******
     * SHR *
     *******/
    case Opcodes.ISHR:
    case Opcodes.IUSHR:
        t = T.INT;
        // fall through
    case Opcodes.LSHR:
    case Opcodes.LUSHR:
        if (t == null) {
            t = T.LONG;
        }
        add(new SHR(this.ops.size(), opcode, this.line, t, T.INT,
                opcode == Opcodes.IUSHR || opcode == Opcodes.LUSHR));
        break;
    /*******
     * SUB *
     *******/
    case Opcodes.DSUB:
        t = T.DOUBLE;
        // fall through
    case Opcodes.FSUB:
        if (t == null) {
            t = T.FLOAT;
        }
        // fall through
    case Opcodes.ISUB:
        if (t == null) {
            t = T.INT;
        }
        // fall through
    case Opcodes.LSUB:
        if (t == null) {
            t = T.LONG;
        }
        add(new SUB(this.ops.size(), opcode, this.line, t));
        break;
    /********
     * SWAP *
     ********/
    case Opcodes.SWAP:
        add(new SWAP(this.ops.size(), opcode, this.line));
        break;
    /*********
     * THROW *
     *********/
    case Opcodes.ATHROW:
        add(new THROW(this.ops.size(), opcode, this.line));
        break;
    /*******
     * XOR *
     *******/
    case Opcodes.IXOR:
        t = T.AINT;
        // fall through
    case Opcodes.LXOR: {
        if (t == null) {
            t = T.LONG;
        }
        add(new XOR(this.ops.size(), opcode, this.line, t));
        break;
    }
    default:
        log.warn(getM() + ": Unknown insn opcode '" + opcode + "'!");
    }
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java

License:Open Source License

/**
 * Returns the instructions, that are needed to convert 
 * a return value of the type {@link Object} to the real type
 * @param returnType the real type/*from   w  ww. j  a v  a  2 s.c  om*/
 * @return
 */
protected InsnList getUnboxingInstructionsForReturnValue(Type returnType) {
    InsnList instructions = new InsnList();
    switch (returnType.getSort()) {
    case Type.VOID:
        instructions.add(new InsnNode(Opcodes.POP));
        instructions.add(new InsnNode(Opcodes.RETURN));
        break;
    case Type.ARRAY: // fallthrough
    case Type.OBJECT:
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, returnType.getInternalName()));
        instructions.add(new InsnNode(Opcodes.ARETURN));
        break;
    default:
        String objectType = AsmTypeHelper.getObjectType(returnType);
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType));
        instructions.add(AsmTypeHelper.getUnboxingInstructionForType(returnType, objectType));
        instructions.add(new InsnNode(returnType.getOpcode(Opcodes.IRETURN)));
    }
    return instructions;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractTransformableClassNode.java

License:Open Source License

/**
 * Replace all return statements in the given instructions with new 
 * statements that convert the real return value to {@link Object}
 * and return this new {@link Object}/*from  w  w w .j  a v  a2  s  .co m*/
 * 
 * @param instructions
 * @param returnType
 */
protected void replaceReturn(InsnList instructions, Type returnType) {
    if (returnType.getSort() != Type.OBJECT && returnType.getSort() != Type.ARRAY
            && returnType.getSort() != Type.VOID) {
        ListIterator<AbstractInsnNode> orgMethodIter = instructions.iterator();
        while (orgMethodIter.hasNext()) {
            AbstractInsnNode orgMethodNode = orgMethodIter.next();
            if (orgMethodNode.getOpcode() == returnType.getOpcode(Opcodes.IRETURN)) {
                instructions.insertBefore(orgMethodNode, AsmTypeHelper.getBoxingInstructionForType(returnType));
                instructions.insertBefore(orgMethodNode, new InsnNode(Opcodes.ARETURN));
                instructions.remove(orgMethodNode);
            }
        }
    } else if (returnType.getSort() == Type.VOID) {
        ListIterator<AbstractInsnNode> orgMethodIter = instructions.iterator();
        while (orgMethodIter.hasNext()) {
            AbstractInsnNode orgMethodNode = orgMethodIter.next();
            if (orgMethodNode.getOpcode() == Opcodes.RETURN) {
                instructions.insertBefore(orgMethodNode, new InsnNode(Opcodes.ACONST_NULL));
                instructions.insertBefore(orgMethodNode, new InsnNode(Opcodes.ARETURN));
                instructions.remove(orgMethodNode);
            }
        }
    }
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AddEmptyMethodAdapter.java

License:Open Source License

@Override
public void visitEnd() {
    MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
    if ((this.access & Opcodes.ACC_ABSTRACT) != 0) {
        mv.visitEnd();/*www .j a  va 2 s . c o  m*/
        return;
    }
    mv.visitCode();
    boolean needConstValue = true;
    if (superToCall != null) {
        needConstValue = false;
        boolean isStatic = (this.access & Opcodes.ACC_STATIC) != 0;
        int firstArgIndex = isStatic ? 0 : 1;
        if (!isStatic)
            mv.visitVarInsn(Opcodes.ALOAD, 0); // "this"
        Type[] args = Type.getArgumentTypes(desc);
        for (int i = 0, slot = firstArgIndex; i < args.length; slot += args[i++].getSize())
            mv.visitVarInsn(args[i].getOpcode(Opcodes.ILOAD), slot);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superToCall, name, desc, false);
    }
    Type returnType = Type.getReturnType(this.desc);
    switch (returnType.getSort()) {
    case Type.VOID:
        mv.visitInsn(Opcodes.RETURN);
        break;
    case Type.INT:
    case Type.BOOLEAN:
    case Type.CHAR:
    case Type.BYTE:
    case Type.SHORT:
        if (needConstValue)
            mv.visitInsn(Opcodes.ICONST_1);
        mv.visitInsn(Opcodes.IRETURN);
        break;
    case Type.FLOAT:
        if (needConstValue)
            mv.visitInsn(Opcodes.FCONST_1);
        mv.visitInsn(Opcodes.FRETURN);
        break;
    case Type.LONG:
        if (needConstValue)
            mv.visitInsn(Opcodes.LCONST_1);
        mv.visitInsn(Opcodes.LRETURN);
        break;
    case Type.DOUBLE:
    case Type.OBJECT:
    case Type.ARRAY:
        if (needConstValue)
            mv.visitInsn(Opcodes.ACONST_NULL);
        mv.visitInsn(Opcodes.ARETURN);
        break;
    }
    mv.visitMaxs(1, maxLocals);
    mv.visitEnd();
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateAddRemoveRoleMethod.java

License:Open Source License

@Override
protected boolean transform() {
    // void _OT$addRemoveRole(Object role, boolean isAdding) {
    MethodNode method = getMethod(ConstantMembers.addOrRemoveRole);
    final int ROLE_SLOT = 1, IS_ADDING_SLOT = 2;
    Label start = new Label(), end = new Label();
    method.instructions.clear();/*  www . ja va 2 s.  c  om*/
    method.visitLabel(start);

    // set = <initialized _OT$roleSet;>
    final int SET_SLOT = 3;
    method.visitLocalVariable("set", "Ljava/util/Set;", null, start, end, SET_SLOT);
    genGetInitializedRoleSet(method.instructions, SET_SLOT);

    // if (isAdding) {
    method.instructions.add(new IntInsnNode(Opcodes.ILOAD, IS_ADDING_SLOT));
    LabelNode jumpToRemove = new LabelNode();
    method.instructions.add(new JumpInsnNode(Opcodes.IFEQ, jumpToRemove));

    // set.add(role);
    method.instructions.add(new IntInsnNode(Opcodes.ALOAD, SET_SLOT));
    method.instructions.add(new IntInsnNode(Opcodes.ALOAD, ROLE_SLOT));
    method.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, ClassNames.HASH_SET_SLASH, "add",
            "(Ljava/lang/Object;)Z", false));
    method.instructions.add(new InsnNode(Opcodes.POP));

    LabelNode jumpToEnd = new LabelNode();
    method.instructions.add(new JumpInsnNode(Opcodes.GOTO, jumpToEnd));
    // } else {
    method.instructions.add(jumpToRemove);
    // set.remove(role);
    method.instructions.add(new IntInsnNode(Opcodes.ALOAD, SET_SLOT));
    method.instructions.add(new IntInsnNode(Opcodes.ALOAD, ROLE_SLOT));
    method.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, ClassNames.HASH_SET_SLASH, "remove",
            "(Ljava/lang/Object;)Z", false));
    method.instructions.add(new InsnNode(Opcodes.POP));

    method.instructions.add(jumpToEnd);
    // }

    method.instructions.add(new InsnNode(Opcodes.RETURN));
    method.visitLabel(end);
    // }
    // maxs are computed, maxStack from flow, maxLocals from localVariable-slots
    return true;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.CreateCallAllBindingsCallInOrgMethod.java

License:Open Source License

@Override
public boolean transform() {
    MethodNode method = getMethod(orgMethod);
    if ((method.access & Opcodes.ACC_ABSTRACT) != 0)
        return false;

    //      if (method.name.equals("<init>")) {
    //         int size = method.instructions.size();
    //         for (int i = 0; i < size; i++) {
    //            AbstractInsnNode insn = method.instructions.get(i);
    //            System.out.println(insn+" "+insn.getOpcode());
    //         }//  w w w .  j  a va  2s.  co m
    //      }
    //      System.out.println("================");

    AbstractInsnNode insertBefore = null;
    if (orgMethod.getName().equals("<init>")) {
        // keep instructions, find insertion point:
        int last = method.instructions.size();
        LabelNode callAll = new LabelNode();
        while (--last >= 0) {
            if (method.instructions.get(last).getOpcode() == Opcodes.RETURN) {
                AbstractInsnNode ret = method.instructions.get(last);
                method.instructions.set(ret, callAll);
                insertBefore = callAll;
                break;
            }
        }
        if (insertBefore == null)
            throw new IllegalStateException("Insertion point for weaving into ctor not found!!!");

        // FIXME: triggers NPE in MethodVisitor.visitMaxs
        //         // replace RETURN with GOTO
        //         for (int i=0; i<last; i++) {
        //            AbstractInsnNode current = method.instructions.get(i);
        //            if (current.getOpcode() == Opcodes.RETURN)
        //               method.instructions.set(current, new JumpInsnNode(Opcodes.GOTO, callAll));
        //         }

    } else {
        method.instructions.clear();
    }

    // start of try-block:
    InsnList newInstructions = new InsnList();
    LabelNode start = new LabelNode();
    newInstructions.add(start);

    // put this on the stack
    newInstructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    // put boundMethodId on the stack
    newInstructions.add(createLoadIntConstant(boundMethodId));
    Type[] args = Type.getArgumentTypes(method.desc);
    // box the arguments
    newInstructions.add(getBoxingInstructions(args, false));

    // this.callAllBindings(boundMethodId, args);
    newInstructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, this.name,
            ConstantMembers.callAllBindingsClient.getName(),
            ConstantMembers.callAllBindingsClient.getSignature()));
    Type returnType = Type.getReturnType(method.desc);
    newInstructions.add(getUnboxingInstructionsForReturnValue(returnType));

    if (insertBefore != null) {
        method.instructions.insertBefore(insertBefore, newInstructions);
        method.instructions.remove(insertBefore); // remove extra RETURN
    } else {
        method.instructions.add(newInstructions);
    }

    //      if (method.name.equals("<init>")) {
    //         int size = method.instructions.size();
    //         for (int i = 0; i < size; i++) {
    //            AbstractInsnNode insn = method.instructions.get(i);
    //            System.out.println(insn+" "+insn.getOpcode());
    //         }
    //      }

    // catch and unwrap SneakyException:
    addCatchSneakyException(method, start);

    int localSlots = 0;
    int maxArgSize = 1;
    for (Type type : args) {
        int size = type.getSize();
        localSlots += size;
        if (size == 2)
            maxArgSize = 2;
    }
    method.maxStack = args.length > 0 ? 5 + maxArgSize : 3;
    method.maxLocals = localSlots + 1;

    return true;
}