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.eclipse.objectteams.otredyn.bytecode.asm.CreateFieldAccessAdapter.java

License:Open Source License

@Override
public boolean transform() {

    InsnList instructions = new InsnList();
    // put accessId on the stack
    instructions.add(new IntInsnNode(Opcodes.ILOAD, firstArgIndex + 1));
    // read or write access
    LabelNode writeAccess = new LabelNode();
    instructions.add(new JumpInsnNode(Opcodes.IFNE, writeAccess));
    // read access
    if (field.isStatic()) {
        // get value of field
        instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, name, field.getName(), field.getSignature()));
    } else {/*from  w w  w  . j a  va2 s .c o  m*/
        // put "this" on the stack
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
        // get value of field
        instructions.add(new FieldInsnNode(Opcodes.GETFIELD, name, field.getName(), field.getSignature()));
    }

    //box value as "Object"
    Type type = Type.getType(field.getSignature());
    instructions.add(AsmTypeHelper.getBoxingInstructionForType(type));
    instructions.add(new InsnNode(Opcodes.ARETURN));

    //write access
    instructions.add(writeAccess);
    //put "args" on the stack 
    instructions.add(new IntInsnNode(Opcodes.ALOAD, firstArgIndex + 2));
    //get the first element of "args"
    instructions.add(new InsnNode(Opcodes.ICONST_0));
    instructions.add(new InsnNode(Opcodes.AALOAD));
    //unbox it
    if (type.getSort() != Type.ARRAY && type.getSort() != Type.OBJECT) {
        String objectType = AsmTypeHelper.getObjectType(type);
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType));
        instructions.add(AsmTypeHelper.getUnboxingInstructionForType(type, objectType));
    } else {
        instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, type.getInternalName()));
    }

    if (field.isStatic()) {
        //save value in field
        instructions.add(new FieldInsnNode(Opcodes.PUTSTATIC, name, field.getName(), field.getSignature()));
    } else {
        //put "this" on the stack
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
        instructions.add(new InsnNode(Opcodes.SWAP));
        //save value in field
        instructions.add(new FieldInsnNode(Opcodes.PUTFIELD, name, field.getName(), field.getSignature()));
    }

    //dummy return 
    instructions.add(new InsnNode(Opcodes.ACONST_NULL));
    instructions.add(new InsnNode(Opcodes.ARETURN));

    //add the instructions to a new label in the existing switch
    MethodNode method = getMethod(access);
    addNewLabelToSwitch(method.instructions, instructions, accessId);

    return true;
}

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

License:Open Source License

@Override
public boolean transform() {
    MethodNode methodNode = getMethod(method);
    InsnList instructions = new InsnList();

    if (isConstructor) {
        // create empty object for constructor invocation:
        instructions.add(new TypeInsnNode(Opcodes.NEW, name));
        instructions.add(new InsnNode(Opcodes.DUP));
    } else if (!method.isStatic()) {
        //put "this" on the stack for a non-static method
        instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));
    }/*  w w  w .  ja  va  2 s  .  c  o  m*/

    //Unbox arguments
    Type[] args = Type.getArgumentTypes(methodNode.desc);

    if (args.length > 0) {

        for (int i = 0; i < args.length; i++) {
            instructions.add(new IntInsnNode(Opcodes.ALOAD, firstArgIndex + 2));
            instructions.add(createLoadIntConstant(i));
            instructions.add(new InsnNode(Opcodes.AALOAD));
            Type arg = args[i];
            if (arg.getSort() != Type.ARRAY && arg.getSort() != Type.OBJECT) {
                String objectType = AsmTypeHelper.getObjectType(arg);
                instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, objectType));
                instructions.add(AsmTypeHelper.getUnboxingInstructionForType(arg, objectType));
            } else {
                instructions.add(new TypeInsnNode(Opcodes.CHECKCAST, arg.getInternalName()));
            }
        }
    }

    //call original method
    int opcode = Opcodes.INVOKEVIRTUAL;
    if (method.isStatic()) {
        opcode = Opcodes.INVOKESTATIC;
    } else if (isConstructor) {
        opcode = Opcodes.INVOKESPECIAL;
    }
    instructions.add(new MethodInsnNode(opcode, name, method.getName(), method.getSignature()));

    //box return value
    Type returnType = Type.getReturnType(methodNode.desc);

    if (returnType.getSort() != Type.OBJECT && returnType.getSort() != Type.ARRAY
            && returnType.getSort() != Type.VOID) {

        instructions.add(AsmTypeHelper.getBoxingInstructionForType(returnType));
        instructions.add(new InsnNode(Opcodes.ARETURN));
    } else if (returnType.getSort() == Type.VOID && !isConstructor) {
        instructions.add(new InsnNode(Opcodes.ACONST_NULL));
        instructions.add(new InsnNode(Opcodes.ARETURN));
    } else {
        instructions.add(new InsnNode(Opcodes.ARETURN));
    }

    //add the instructions to a new label in the existing switch
    MethodNode access = getMethod(this.access);
    addNewLabelToSwitch(access.instructions, instructions, accessId);

    return true;
}

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

License:Open Source License

@Override
public boolean transform() {
    MethodNode callOrig = getMethod(ConstantMembers.callOrig);
    InsnList instructions = new InsnList();

    Type[] args = Type.getArgumentTypes(callOrig.desc);
    addInstructionsForLoadArguments(instructions, args, false);

    instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, superClassName,
            ConstantMembers.callOrig.getName(), ConstantMembers.callOrig.getSignature()));
    instructions.add(new InsnNode(Opcodes.ARETURN));
    addNewLabelToSwitch(callOrig.instructions, instructions, joinpointId);
    callOrig.maxStack = Math.max(callOrig.maxStack, args.length + 1);
    return true;//from w  w w .ja va 2 s .c  o m
}

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

License:Open Source License

/**
 * Adds instructions for the default label.
 * @param method//from   w  w  w.j a  v a  2 s  . co m
 */
protected void addInstructionForDefaultLabel(MethodNode method) {
    method.instructions.add(new InsnNode(Opcodes.ACONST_NULL));
    method.instructions.add(new InsnNode(Opcodes.ARETURN));
}

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

License:Open Source License

@Override
protected void addInstructionForDefaultLabel(MethodNode method) {
    if (superClassName.equals("java/lang/Object")) {
        method.instructions.add(new TypeInsnNode(Opcodes.NEW, "org/objectteams/NoSuchMethodError"));
        method.instructions.add(new InsnNode(Opcodes.DUP));
        method.instructions.add(new IntInsnNode(Opcodes.ILOAD, getFirstArgIndex())); // accessId
        method.instructions.add(new LdcInsnNode(clazz.getName())); // current class
        method.instructions.add(new LdcInsnNode("decapsulating access")); // access reason
        method.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "org/objectteams/NoSuchMethodError",
                "<init>", "(ILjava/lang/String;Ljava/lang/String;)V", false));
        method.instructions.add(new InsnNode(Opcodes.ATHROW));
    } else {/* www . j a  v a2  s .com*/
        Type[] args = Type.getArgumentTypes(method.desc);
        addInstructionsForLoadArguments(method.instructions, args, getMethod().isStatic());

        int opcode = Opcodes.INVOKESPECIAL;
        if (getMethod().isStatic()) {
            opcode = Opcodes.INVOKESTATIC;
        }
        method.instructions.add(
                new MethodInsnNode(opcode, superClassName, getMethod().getName(), getMethod().getSignature()));
        method.instructions.add(new InsnNode(Opcodes.ARETURN));
    }
}

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

License:Open Source License

@Override
protected void addPostSwitchInstructions(MethodNode method) {
    method.instructions.add(gotoLabel);//  w w  w.j a va  2 s  .c  om
    method.instructions.add(new IntInsnNode(Opcodes.ALOAD, 0));

    args = Type.getArgumentTypes(method.desc);
    int length = args.length;
    for (int i = 0; i < length; i++) {
        Type arg = args[i];
        method.instructions.add(new IntInsnNode(arg.getOpcode(Opcodes.ILOAD), i + 1));
    }

    // return callOrig(boundMethodId, args);
    method.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, name, ConstantMembers.callOrig.getName(),
            ConstantMembers.callOrig.getSignature()));
    method.instructions.add(new InsnNode(Opcodes.ARETURN));
}

From source file:org.evosuite.graphs.cfg.ASMWrapper.java

License:Open Source License

/**
 * <p>//from   w w  w . j a  v a  2s.  com
 * isReturn
 * </p>
 * 
 * @return a boolean.
 */
public boolean isReturn() {
    switch (asmNode.getOpcode()) {
    case Opcodes.RETURN:
    case Opcodes.ARETURN:
    case Opcodes.IRETURN:
    case Opcodes.LRETURN:
    case Opcodes.DRETURN:
    case Opcodes.FRETURN:
        return true;
    default:
        return false;
    }
}

From source file:org.evosuite.instrumentation.coverage.LCSAJsInstrumentation.java

License:Open Source License

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
//using external lib
@Override/*  w  ww  .ja  v a2s  .c  o  m*/
public void analyze(ClassLoader classLoader, MethodNode mn, String className, String methodName, int access) {

    Queue<LCSAJ> lcsaj_queue = new LinkedList<LCSAJ>();
    HashSet<Integer> targets_reached = new HashSet<Integer>();

    AbstractInsnNode start = mn.instructions.getFirst();
    int startID = 0;

    // TODO: This should replace the hack below
    if (methodName.startsWith("<init>")) {
        Iterator<AbstractInsnNode> j = mn.instructions.iterator();
        boolean constructorInvoked = false;
        while (j.hasNext()) {
            AbstractInsnNode in = j.next();
            startID++;
            if (!constructorInvoked) {
                if (in.getOpcode() == Opcodes.INVOKESPECIAL) {
                    MethodInsnNode cn = (MethodInsnNode) in;
                    Collection<String> superClasses = DependencyAnalysis.getInheritanceTree()
                            .getSuperclasses(className);
                    superClasses.add(className);
                    String classNameWithDots = ResourceList.getClassNameFromResourcePath(cn.owner);
                    if (superClasses.contains(classNameWithDots)) {
                        constructorInvoked = true;
                        break;
                    }
                } else {
                    continue;
                }
            }
        }
    }

    /*
    if (methodName.startsWith("<init>")) {
       if (mn.instructions.size() >= 4) {
    start = mn.instructions.get(4);
    startID = 4;
       }
    }
    */

    LCSAJ a = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader)
            .getInstruction(className, methodName, startID, start));
    lcsaj_queue.add(a);

    targets_reached.add(0);

    ArrayList<TryCatchBlockNode> tc_blocks = (ArrayList<TryCatchBlockNode>) mn.tryCatchBlocks;

    for (TryCatchBlockNode t : tc_blocks) {
        LCSAJ b = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader)
                .getInstruction(className, methodName, mn.instructions.indexOf(t.handler), t.handler));
        lcsaj_queue.add(b);
    }

    while (!lcsaj_queue.isEmpty()) {

        LCSAJ currentLCSAJ = lcsaj_queue.poll();
        int position = mn.instructions.indexOf(currentLCSAJ.getLastNodeAccessed());
        // go to next bytecode instruction
        position++;

        if (position >= mn.instructions.size()) {
            // New LCSAJ for current + return
            LCSAJPool.add_lcsaj(className, methodName, currentLCSAJ);
            continue;
        }

        AbstractInsnNode next = mn.instructions.get(position);
        currentLCSAJ.lookupInstruction(position, BytecodeInstructionPool.getInstance(classLoader)
                .getInstruction(className, methodName, position, next));

        if (next instanceof JumpInsnNode) {

            JumpInsnNode jump = (JumpInsnNode) next;
            // New LCSAJ for current + jump to target
            LCSAJPool.add_lcsaj(className, methodName, currentLCSAJ);
            LabelNode target = jump.label;
            int targetPosition = mn.instructions.indexOf(target);

            if (jump.getOpcode() != Opcodes.GOTO) {

                LCSAJ copy = new LCSAJ(currentLCSAJ);
                lcsaj_queue.add(copy);

            }

            if (!targets_reached.contains(targetPosition)) {
                LCSAJ c = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader)
                        .getInstruction(className, methodName, targetPosition, target));
                lcsaj_queue.add(c);

                targets_reached.add(targetPosition);
            }

        } else if (next instanceof TableSwitchInsnNode) {

            TableSwitchInsnNode tswitch = (TableSwitchInsnNode) next;
            List<LabelNode> allTargets = tswitch.labels;

            for (LabelNode target : allTargets) {

                int targetPosition = mn.instructions.indexOf(target);

                if (!targets_reached.contains(targetPosition)) {

                    LCSAJ b = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader)
                            .getInstruction(className, methodName, targetPosition, target));
                    lcsaj_queue.add(b);

                    targets_reached.add(targetPosition);
                }
            }

        } else if (next instanceof InsnNode) {
            InsnNode insn = (InsnNode) next;
            // New LCSAJ for current + throw / return
            if (insn.getOpcode() == Opcodes.ATHROW || insn.getOpcode() == Opcodes.RETURN
                    || insn.getOpcode() == Opcodes.ARETURN || insn.getOpcode() == Opcodes.IRETURN
                    || insn.getOpcode() == Opcodes.DRETURN || insn.getOpcode() == Opcodes.LRETURN
                    || insn.getOpcode() == Opcodes.FRETURN) {

                LCSAJPool.add_lcsaj(className, methodName, currentLCSAJ);
            } else
                lcsaj_queue.add(currentLCSAJ);
        } else
            lcsaj_queue.add(currentLCSAJ);
    }

    if (Properties.STRATEGY != Strategy.EVOSUITE)
        addInstrumentation(classLoader, mn, className, methodName);

    //      if (Properties.WRITE_CFG)
    //         for (LCSAJ l : LCSAJPool.getLCSAJs(className, methodName)) {
    //            LCSAJGraph graph = new LCSAJGraph(l, false);
    //            String graphDestination = "evosuite-graphs/LCSAJGraphs/" + className
    //                    + "/" + methodName;
    //            File dir = new File(graphDestination);
    //            if (dir.mkdirs())
    //               graph.generate(new File(graphDestination + "/LCSAJGraph no: "
    //                       + l.getID() + ".dot"));
    //            else if (dir.exists())
    //               graph.generate(new File(graphDestination + "/LCSAJGraph no: "
    //                       + l.getID() + ".dot"));
    //         }
}

From source file:org.evosuite.instrumentation.ReturnValueAdapter.java

License:Open Source License

/** {@inheritDoc} */
@Override//from w w  w.  j ava2s  . c o m
public void visitInsn(int opcode) {
    if (!methodName.equals("<clinit>")) {
        switch (opcode) {
        case Opcodes.IRETURN:
            callLogIReturn();
            break;
        case Opcodes.ARETURN:
            callLogAReturn();
            break;
        case Opcodes.ATHROW:
            break;
        case Opcodes.DRETURN:
            callLogDReturn();
            break;
        case Opcodes.FRETURN:
            callLogFReturn();
            break;
        case Opcodes.LRETURN:
            callLogLReturn();
            break;
        case Opcodes.RETURN:
            break;
        default:
            break;
        }
    }
    super.visitInsn(opcode);

}

From source file:org.evosuite.testcarver.instrument.Instrumenter.java

License:Open Source License

private void addReturnInsn(final InsnList il, final Type type) {
    if (type.equals(Type.BOOLEAN_TYPE)) {
        il.add(new InsnNode(Opcodes.IRETURN));
    } else if (type.equals(Type.CHAR_TYPE)) {
        il.add(new InsnNode(Opcodes.IRETURN));
    } else if (type.equals(Type.BYTE_TYPE)) {
        il.add(new InsnNode(Opcodes.IRETURN));
    } else if (type.equals(Type.SHORT_TYPE)) {
        il.add(new InsnNode(Opcodes.IRETURN));
    } else if (type.equals(Type.INT_TYPE)) {
        il.add(new InsnNode(Opcodes.IRETURN));
    } else if (type.equals(Type.FLOAT_TYPE)) {
        il.add(new InsnNode(Opcodes.FRETURN));
    } else if (type.equals(Type.LONG_TYPE)) {
        il.add(new InsnNode(Opcodes.LRETURN));
    } else if (type.equals(Type.DOUBLE_TYPE)) {
        il.add(new InsnNode(Opcodes.DRETURN));
    } else {/*  w w  w.j ava  2 s. c o  m*/
        il.add(new InsnNode(Opcodes.ARETURN));
    }
}