Example usage for org.objectweb.asm.tree InsnList add

List of usage examples for org.objectweb.asm.tree InsnList add

Introduction

In this page you can find the example usage for org.objectweb.asm.tree InsnList add.

Prototype

public void add(final InsnList insnList) 

Source Link

Document

Adds the given instructions to the end of this list.

Usage

From source file:org.evosuite.instrumentation.testability.transformer.ImplicitElseTransformer.java

License:Open Source License

private void handleDependency(ControlDependency dependency, ControlDependenceGraph cdg, MethodNode mn,
        FieldInsnNode varNode, BytecodeInstruction parentLevel) {

    if (addedNodes.contains(dependency))
        return;// w w w.java2  s  .c  om

    // Get the basic blocks reachable if the dependency would evaluate different
    Set<BasicBlock> blocks = cdg.getAlternativeBlocks(dependency);
    addedNodes.add(dependency);

    Set<ControlDependency> dependencies = dependency.getBranch().getInstruction().getControlDependencies();
    //if (dependencies.size() == 1) {
    //   ControlDependency dep = dependencies.iterator().next();
    for (ControlDependency dep : dependencies) {
        if (!addedNodes.contains(dep) && dep != dependency)
            handleDependency(dep, cdg, mn, varNode, dependency.getBranch().getInstruction());
    }

    // TODO: Need to check that there is an assignment in every alternative path through CDG

    boolean hasAssignment = false;
    for (BasicBlock block : blocks) {
        // If this block also assigns a value to the same variable
        for (BytecodeInstruction instruction : block) {
            if (instruction.getASMNode().getOpcode() == Opcodes.PUTFIELD
                    || instruction.getASMNode().getOpcode() == Opcodes.PUTSTATIC) {
                FieldInsnNode otherFieldNode = (FieldInsnNode) instruction.getASMNode();
                FieldInsnNode thisFieldNode = varNode;
                if (otherFieldNode.owner.equals(thisFieldNode.owner)
                        && otherFieldNode.name.equals(thisFieldNode.name)) {
                    hasAssignment = true;
                    break;
                }
            }
        }
        if (hasAssignment) {
            break;
        }
    }

    // The Flag assignment is is the dependency evaluates to the given value
    // We thus need to insert the tautoligical assignment either directly after the IF (if the value is true)
    // or before the jump target (if the value is false)

    if (!hasAssignment) {
        if (dependency.getBranch().getInstruction().isSwitch()) {
            BooleanTestabilityTransformation.logger.warn("Don't know how to handle Switches yet");
            return;
        }

        TransformationStatistics.transformedImplicitElse();

        JumpInsnNode jumpNode = (JumpInsnNode) dependency.getBranch().getInstruction().getASMNode();
        FieldInsnNode newLoad = new FieldInsnNode(
                varNode.getOpcode() == Opcodes.PUTSTATIC ? Opcodes.GETSTATIC : Opcodes.GETFIELD, varNode.owner,
                varNode.name, varNode.desc);
        FieldInsnNode newStore = new FieldInsnNode(varNode.getOpcode(), varNode.owner, varNode.name,
                varNode.desc);
        AbstractInsnNode newOwnerLoad1 = null;
        AbstractInsnNode newOwnerLoad2 = null;
        if (varNode.getOpcode() == Opcodes.PUTFIELD) {
            // Need to copy the bloody owner
            // Check for VarInsn
            //if (varNode.getPrevious().getOpcode() == Opcodes.ALOAD) {
            newOwnerLoad1 = new VarInsnNode(Opcodes.ALOAD, 0);
            newOwnerLoad2 = new VarInsnNode(Opcodes.ALOAD, 0);
            /*
            } else {
            // Else use helper function
            // Insert DUP and
            logger.info("Wargh");
            System.exit(0);
            fieldOwnerId++;
            InsnNode dupNode = new InsnNode(Opcodes.DUP);
            mn.instructions.insertBefore(varNode, new LdcInsnNode(
            fieldOwnerId));
            mn.instructions.insertBefore(varNode, dupNode);
            registerInstruction(mn, varNode, dupNode);
            MethodInsnNode storeOwner = new MethodInsnNode(
            Opcodes.INVOKESTATIC,
            "org/evosuite/instrumentation/BooleanHelper",
            "setFieldOwner", "(ILjava/lang/Object;)V");
            mn.instructions.insertBefore(varNode, storeOwner);
            registerInstruction(mn, varNode, storeOwner);
            newOwnerLoad1 = new MethodInsnNode(Opcodes.INVOKESTATIC,
            "org/evosuite/instrumentation/BooleanHelper",
            "getFieldOwner", "(I)Ljava/lang/Object;");
            newOwnerLoad2 = new MethodInsnNode(Opcodes.INVOKESTATIC,
            "org/evosuite/instrumentation/BooleanHelper",
            "getFieldOwner", "(I)Ljava/lang/Object;");
            }
            */
        }

        if (dependency.getBranchExpressionValue()) {
            BooleanTestabilityTransformation.logger.info("Inserting after if");
            // Insert directly after if
            mn.instructions.insert(jumpNode, newStore);
            mn.instructions.insert(jumpNode, newLoad);
            if (newOwnerLoad1 != null) {
                mn.instructions.insert(jumpNode, newOwnerLoad1);
                registerInstruction(mn, varNode, newOwnerLoad1);
            }
            if (newOwnerLoad2 != null) {
                mn.instructions.insert(jumpNode, newOwnerLoad2);
                registerInstruction(mn, varNode, newOwnerLoad2);
            }
            registerInstruction(mn, varNode, newStore);
            registerInstruction(mn, varNode, newLoad);

        } else {
            BooleanTestabilityTransformation.logger.info("Inserting as jump target");

            // Insert as jump target
            LabelNode target = jumpNode.label;
            LabelNode newTarget = new LabelNode(new Label());

            registerInstruction(mn, target, newStore);
            registerInstruction(mn, target, newLoad);

            InsnList assignment = new InsnList();
            assignment.add(new JumpInsnNode(Opcodes.GOTO, target));
            assignment.add(newTarget);
            if (newOwnerLoad1 != null) {
                assignment.add(newOwnerLoad1);
                registerInstruction(mn, target, newOwnerLoad1);
            }
            if (newOwnerLoad2 != null) {
                assignment.add(newOwnerLoad2);
                registerInstruction(mn, target, newOwnerLoad2);
            }
            assignment.add(newLoad);
            assignment.add(newStore);
            jumpNode.label = newTarget;

            mn.instructions.insertBefore(target, assignment);
        }
        addedInsns.add(newStore);
        addedInsns.add(newLoad);
    }

}

From source file:org.evosuite.instrumentation.testability.transformer.ImplicitElseTransformer.java

License:Open Source License

private void handleDependency(ControlDependency dependency, ControlDependenceGraph cdg, MethodNode mn,
        VarInsnNode varNode, BytecodeInstruction parentLevel) {

    if (addedNodes.contains(dependency))
        return;//from  w w  w . j a va2 s .co m

    // Get the basic blocks reachable if the dependency would evaluate different
    Set<BasicBlock> blocks = cdg.getAlternativeBlocks(dependency);
    addedNodes.add(dependency);

    Set<ControlDependency> dependencies = dependency.getBranch().getInstruction().getControlDependencies();
    //if (dependencies.size() == 1) {
    //   ControlDependency dep = dependencies.iterator().next();
    for (ControlDependency dep : dependencies) {
        if (!addedNodes.contains(dep) && dep != dependency)
            handleDependency(dep, cdg, mn, varNode, dependency.getBranch().getInstruction());
    }

    // TODO: Need to check that there is an assignment in every alternative path through CDG

    boolean hasAssignment = false;
    for (BasicBlock block : blocks) {
        // If this block also assigns a value to the same variable
        for (BytecodeInstruction instruction : block) {
            if (instruction.getASMNode().getOpcode() == Opcodes.ISTORE) {
                VarInsnNode otherVarNode = (VarInsnNode) instruction.getASMNode();
                VarInsnNode thisVarNode = varNode;
                if (otherVarNode.var == thisVarNode.var) {
                    hasAssignment = true;
                    break;
                }
            }
        }
        if (hasAssignment) {
            break;
        }
    }

    // The Flag assignment is is the dependency evaluates to the given value
    // We thus need to insert the tautoligical assignment either directly after the IF (if the value is true)
    // or before the jump target (if the value is false)

    if (!hasAssignment) {
        TransformationStatistics.transformedImplicitElse();
        if (dependency.getBranch().getInstruction().isSwitch()) {
            BooleanTestabilityTransformation.logger.warn("Don't know how to handle Switches yet");
            return;
        }
        JumpInsnNode jumpNode = (JumpInsnNode) dependency.getBranch().getInstruction().getASMNode();
        VarInsnNode newStore = new VarInsnNode(Opcodes.ISTORE, varNode.var);
        VarInsnNode newLoad = new VarInsnNode(Opcodes.ILOAD, varNode.var);
        if (dependency.getBranchExpressionValue()) {
            BooleanTestabilityTransformation.logger.info("Inserting else branch directly after if");
            // Insert directly after if
            if (isDefinedBefore(mn, varNode, jumpNode)) {
                mn.instructions.insert(jumpNode, newStore);
                mn.instructions.insert(jumpNode, newLoad);
                registerInstruction(mn, varNode, newStore);
                registerInstruction(mn, varNode, newLoad);
            }

        } else {
            BooleanTestabilityTransformation.logger.info("Inserting else branch as jump target");
            // Insert as jump target
            if (isDefinedBefore(mn, varNode, jumpNode)) {

                LabelNode target = jumpNode.label;
                LabelNode newTarget = new LabelNode(new Label());

                // jumpNode or target?
                registerInstruction(mn, jumpNode.getNext(), newStore);
                registerInstruction(mn, jumpNode.getNext(), newLoad);

                InsnList assignment = new InsnList();
                assignment.add(new JumpInsnNode(Opcodes.GOTO, target));
                assignment.add(newTarget);
                assignment.add(newLoad);
                assignment.add(newStore);
                jumpNode.label = newTarget;

                mn.instructions.insertBefore(target, assignment);
            }
        }
    }

}

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

License:Open Source License

@SuppressWarnings("unchecked")
private void addFieldRegistryRegisterCall(final MethodNode methodNode) {
    AbstractInsnNode ins = null;//  ww w .j a va 2 s.c om
    ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator();

    int numInvokeSpecials = 0; // number of invokespecial calls before actual constructor call

    while (iter.hasNext()) {
        ins = iter.next();

        if (ins instanceof MethodInsnNode) {
            MethodInsnNode mins = (MethodInsnNode) ins;
            if (ins.getOpcode() == Opcodes.INVOKESPECIAL) {
                if (mins.name.startsWith("<init>")) {
                    if (numInvokeSpecials == 0) {
                        break;
                    } else {
                        numInvokeSpecials--;
                    }
                }
            }
        } else if (ins instanceof TypeInsnNode) {
            TypeInsnNode typeIns = (TypeInsnNode) ins;
            if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) {
                numInvokeSpecials++;
            }
        }
    }

    final InsnList instructions = new InsnList();

    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(FieldRegistry.class),
            "register", "(Ljava/lang/Object;)V"));

    methodNode.instructions.insert(ins, instructions);
}

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

License:Open Source License

private void instrumentGETXXXFieldAccesses(final ClassNode cn, final String internalClassName,
        final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;

    AbstractInsnNode ins = null;/* www  .  j av a 2s. c om*/
    FieldInsnNode fieldIns = null;

    for (int i = 0; i < instructions.size(); i++) {
        ins = instructions.get(i);
        if (ins instanceof FieldInsnNode) {
            fieldIns = (FieldInsnNode) ins;

            /*
             * Is field referencing outermost instance? if yes, ignore it
             * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html
             */
            if (fieldIns.name.endsWith("$0")) {
                continue;
            }

            final int opcode = ins.getOpcode();
            if (opcode == Opcodes.GETFIELD || opcode == Opcodes.GETSTATIC) {
                final InsnList il = new InsnList();

                if (opcode == Opcodes.GETFIELD) {
                    Type fieldType = Type.getType(fieldIns.desc);
                    if (fieldType.getSize() == 1) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP));
                        il.add(new InsnNode(Opcodes.SWAP));
                    } else if (fieldType.getSize() == 2) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP));
                        // v
                        // GETFIELD
                        // v, w
                        il.add(new InsnNode(Opcodes.DUP2_X1));
                        // w, v, w
                        il.add(new InsnNode(Opcodes.POP2));
                        // w, v
                        // -> Call
                        // w
                    }
                } else
                    il.add(new InsnNode(Opcodes.ACONST_NULL));

                il.add(new LdcInsnNode(this.captureId));
                il.add(new LdcInsnNode(fieldIns.owner));
                il.add(new LdcInsnNode(fieldIns.name));
                il.add(new LdcInsnNode(fieldIns.desc));

                il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                        PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.FieldRegistry.class),
                        "notifyReadAccess",
                        "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"));

                i += il.size();

                instructions.insert(fieldIns, il);
                this.captureId++;
            }
        }
    }
}

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

License:Open Source License

private void instrumentPUTXXXFieldAccesses(final ClassNode cn, final String internalClassName,
        final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;

    AbstractInsnNode ins = null;//w w w.  j a  va  2s.co  m
    FieldInsnNode fieldIns = null;

    // needed get right receiver var in case of PUTFIELD

    for (int i = 0; i < instructions.size(); i++) {
        ins = instructions.get(i);
        if (ins instanceof FieldInsnNode) {
            fieldIns = (FieldInsnNode) ins;

            /*
             * Is field referencing outermost instance? if yes, ignore it
             * http://tns-www.lcs.mit.edu/manuals/java-1.1.1/guide/innerclasses/spec/innerclasses.doc10.html
             */
            if (fieldIns.name.endsWith("$0")) {
                continue;
            }

            final int opcode = ins.getOpcode();
            if (opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC) {
                // construction of  
                //   Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams)
                // call
                final InsnList il = new InsnList();

                if (opcode == Opcodes.PUTFIELD) {
                    Type fieldType = Type.getType(fieldIns.desc);
                    if (fieldType.getSize() == 1) {
                        instructions.insertBefore(fieldIns, new InsnNode(Opcodes.DUP2));
                        il.add(new InsnNode(Opcodes.POP));
                    } else if (fieldType.getSize() == 2) {
                        InsnList uglyList = new InsnList();
                        // v, w
                        uglyList.add(new InsnNode(Opcodes.DUP2_X1));
                        // w, v, w
                        uglyList.add(new InsnNode(Opcodes.POP2));
                        // w, v
                        uglyList.add(new InsnNode(Opcodes.DUP));
                        // w, v, v
                        uglyList.add(new InsnNode(Opcodes.DUP2_X2));
                        // v, v, w, v, v
                        uglyList.add(new InsnNode(Opcodes.POP2));
                        // v, v, w
                        instructions.insertBefore(fieldIns, uglyList);
                        // PUTFIELD
                        // v
                    }
                } else
                    il.add(new InsnNode(Opcodes.ACONST_NULL));

                il.add(new LdcInsnNode(this.captureId));
                il.add(new LdcInsnNode(fieldIns.owner));
                il.add(new LdcInsnNode(fieldIns.name));
                il.add(new LdcInsnNode(fieldIns.desc));

                il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                        PackageInfo.getNameWithSlash(FieldRegistry.class), "notifyModification",
                        "(Ljava/lang/Object;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V"));

                // PUTFIELDRegistry.notifyModification also adds corresponding GETFIELD capture instructions
                this.captureId++;
                i += il.size();

                instructions.insert(fieldIns, il);
                this.captureId++;
            }
        }
    }
}

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

License:Open Source License

private InsnList addCaptureCall(final boolean isStatic, final String internalClassName, final String methodName,
        final String methodDesc, final Type[] argTypes) {
    // construction of  
    //   Capturer.capture(final Object receiver, final String methodName, final Object[] methodParams)
    // call//from  w w  w  . j av  a 2  s  .  com
    final InsnList il = new InsnList();

    il.add(new LdcInsnNode(this.captureId));

    // --- load receiver argument
    int varIndex;
    if (isStatic) {
        // static method invocation
        il.add(new LdcInsnNode(internalClassName));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(CaptureUtil.class),
                "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));

        varIndex = 0;
    } else {
        // non-static method call
        il.add(new VarInsnNode(Opcodes.ALOAD, 0));
        varIndex = 1;
    }

    // --- load method name argument

    il.add(new LdcInsnNode(methodName));

    // --- load method description argument

    il.add(new LdcInsnNode(methodDesc));

    // --- load methodParams arguments

    // load methodParams length
    // TODO ICONST_1 to ICONST_5 would be more efficient
    il.add(new IntInsnNode(Opcodes.BIPUSH, argTypes.length));

    // create array object
    il.add(new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));

    // fill the array

    for (int i = 0; i < argTypes.length; i++) {
        il.add(new InsnNode(Opcodes.DUP));

        // TODO ICONST_1 to ICONST_5 would be more efficient
        il.add(new IntInsnNode(Opcodes.BIPUSH, i));

        //check for primitives
        this.loadAndConvertToObject(il, argTypes[i], varIndex++);
        il.add(new InsnNode(Opcodes.AASTORE));

        // long/double take two registers
        if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
            varIndex++;
        }
    }

    // --- construct Capture.capture() call

    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.Capturer.class), "capture",
            "(ILjava/lang/Object;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V"));

    return il;
}

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

License:Open Source License

private void addCaptureEnableStatement(final String className, final MethodNode mn, final InsnList il,
        final int returnValueVar) {
    il.add(new LdcInsnNode(this.captureId));

    if (TransformerUtil.isStatic(mn.access)) {
        // static method

        il.add(new LdcInsnNode(className));
        il.add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(CaptureUtil.class),
                "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"));
    } else {//w  w  w .  ja va2s .c o  m
        // non-static method

        il.add(new VarInsnNode(Opcodes.ALOAD, 0));
    }

    final Type returnType = Type.getReturnType(mn.desc);
    if (returnType.equals(Type.VOID_TYPE)) {
        // load return value for VOID methods
        il.add(new FieldInsnNode(Opcodes.GETSTATIC, PackageInfo.getNameWithSlash(CaptureLog.class),
                "RETURN_TYPE_VOID", Type.getDescriptor(Object.class)));
    } else {
        // load return value as object
        il.add(new VarInsnNode(Opcodes.ALOAD, returnValueVar));
    }

    il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
            PackageInfo.getNameWithSlash(org.evosuite.testcarver.capture.Capturer.class), "enable",
            "(ILjava/lang/Object;Ljava/lang/Object;)V"));
}

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

License:Open Source License

/**
 *    public int myMethod(int i)/*from   w w  w  .  ja  v  a  2 s  .com*/
   {
 try
 {
    return _sw_prototype_original_myMethod(i)
 }
 finally
 {
    Capturer.enable();
 }
   }
        
 * @param classNode
 * @param className
 * @param methodNode
 */
@SuppressWarnings("unchecked")
private MethodNode wrapMethod(final ClassNode classNode, final String className, final MethodNode methodNode) {
    methodNode.maxStack += 4;

    // create wrapper for original method
    final MethodNode wrappingMethodNode = new MethodNode(methodNode.access, methodNode.name, methodNode.desc,
            methodNode.signature,
            (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()]));
    wrappingMethodNode.maxStack = methodNode.maxStack;

    // assign annotations to wrapping method
    wrappingMethodNode.visibleAnnotations = methodNode.visibleAnnotations;
    wrappingMethodNode.visibleParameterAnnotations = methodNode.visibleParameterAnnotations;

    // remove annotations from wrapped method to avoid wrong behavior controlled by annotations
    methodNode.visibleAnnotations = null;
    methodNode.visibleParameterAnnotations = null;

    // rename original method
    methodNode.access = TransformerUtil.modifyVisibility(methodNode.access, Opcodes.ACC_PRIVATE);

    final LabelNode l0 = new LabelNode();
    final LabelNode l1 = new LabelNode();
    final LabelNode l2 = new LabelNode();

    final InsnList wInstructions = wrappingMethodNode.instructions;

    if ("<init>".equals(methodNode.name)) {
        // wrap a constructor 

        methodNode.name = WRAP_NAME_PREFIX + "init" + WRAP_NAME_PREFIX;

        // move call to other constructors to new method
        AbstractInsnNode ins = null;
        ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator();

        int numInvokeSpecials = 0; // number of invokespecial calls before actual constructor call

        while (iter.hasNext()) {
            ins = iter.next();
            iter.remove();
            wInstructions.add(ins);

            if (ins instanceof MethodInsnNode) {
                MethodInsnNode mins = (MethodInsnNode) ins;
                if (ins.getOpcode() == Opcodes.INVOKESPECIAL) {
                    if (mins.name.startsWith("<init>")) {
                        if (numInvokeSpecials == 0) {
                            break;
                        } else {
                            numInvokeSpecials--;
                        }
                    }
                }
            } else if (ins instanceof TypeInsnNode) {
                TypeInsnNode typeIns = (TypeInsnNode) ins;
                if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) {
                    numInvokeSpecials++;
                }
            }
        }
    } else {
        methodNode.name = WRAP_NAME_PREFIX + methodNode.name;
    }

    int varReturnValue = 0;

    final Type returnType = Type.getReturnType(methodNode.desc);

    if (returnType.equals(Type.VOID_TYPE)) {
        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, "java/lang/Throwable"));

    } else {

        wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Throwable"));

        //--- create "Object returnValue = null;"

        if (!TransformerUtil.isStatic(methodNode.access)) {
            // load "this"
            varReturnValue++;
        }

        // consider method arguments to find right variable index
        final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
        for (int i = 0; i < argTypes.length; i++) {
            varReturnValue++;

            // long/double take two registers
            if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
                varReturnValue++;
            }
        }

        // push NULL on the stack and initialize variable for return value for it
        wInstructions.add(new InsnNode(Opcodes.ACONST_NULL));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
    }

    int var = 0;

    // --- L0
    wInstructions.add(l0);

    wInstructions.add(this.addCaptureCall(TransformerUtil.isStatic(methodNode.access), className,
            wrappingMethodNode.name, wrappingMethodNode.desc, Type.getArgumentTypes(methodNode.desc)));

    // --- construct call to wrapped methode

    if (!TransformerUtil.isStatic(methodNode.access)) {
        // load "this" to call method
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
        var++;
    }

    final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
    for (int i = 0; i < argTypes.length; i++) {
        this.addLoadInsn(wInstructions, argTypes[i], var++);

        // long/double take two registers
        if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
            var++;
        }
    }

    if (TransformerUtil.isStatic(methodNode.access)) {
        wInstructions.add(
                new MethodInsnNode(Opcodes.INVOKESTATIC, classNode.name, methodNode.name, methodNode.desc));
    } else {
        wInstructions.add(
                new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, methodNode.name, methodNode.desc));
    }

    var++;

    if (returnType.equals(Type.VOID_TYPE)) {
        wInstructions.add(new JumpInsnNode(Opcodes.GOTO, l2));

        // --- L1

        wInstructions.add(l1);

        wInstructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));

        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);

        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));

        // FIXME <--- DUPLICATE CODE

        // --- L2

        wInstructions.add(l2);
        wInstructions.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);

        wInstructions.add(new InsnNode(Opcodes.RETURN));
    } else {
        // construct store of the wrapped method call's result

        this.addBoxingStmt(wInstructions, returnType);

        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, varReturnValue));

        this.addUnBoxingStmt(wInstructions, returnType);

        final int storeOpcode = returnType.getOpcode(Opcodes.ISTORE);
        wInstructions.add(new VarInsnNode(storeOpcode, ++var)); // might be only var

        // --- L1

        wInstructions.add(l1);

        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);

        // construct load of the wrapped method call's result
        int loadOpcode = returnType.getOpcode(Opcodes.ILOAD);
        wInstructions.add(new VarInsnNode(loadOpcode, var));

        // construct return of the wrapped method call's result
        this.addReturnInsn(wInstructions, returnType);

        //---- L2

        wInstructions.add(l2);

        wInstructions.add(
                new FrameNode(Opcodes.F_FULL, 2, new Object[] { className, this.getInternalName(returnType) },
                        1, new Object[] { "java/lang/Throwable" }));
        wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));

        this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);

        wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
        wInstructions.add(new InsnNode(Opcodes.ATHROW));
    }
    transformWrapperCalls(methodNode);
    return wrappingMethodNode;
}

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 {// ww  w .j a  va 2 s  . c  o  m
        il.add(new InsnNode(Opcodes.ARETURN));
    }
}

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

License:Open Source License

private void addLoadInsn(final InsnList il, final Type type, final int argLocation) {
    if (type.equals(Type.BOOLEAN_TYPE)) {
        il.add(new VarInsnNode(Opcodes.ILOAD, argLocation));
    } else if (type.equals(Type.CHAR_TYPE)) {
        il.add(new VarInsnNode(Opcodes.ILOAD, argLocation));
    } else if (type.equals(Type.BYTE_TYPE)) {
        il.add(new VarInsnNode(Opcodes.ILOAD, argLocation));
    } else if (type.equals(Type.SHORT_TYPE)) {
        il.add(new VarInsnNode(Opcodes.ILOAD, argLocation));
    } else if (type.equals(Type.INT_TYPE)) {
        il.add(new VarInsnNode(Opcodes.ILOAD, argLocation));
    } else if (type.equals(Type.FLOAT_TYPE)) {
        il.add(new VarInsnNode(Opcodes.FLOAD, argLocation));
    } else if (type.equals(Type.LONG_TYPE)) {
        il.add(new VarInsnNode(Opcodes.LLOAD, argLocation));
    } else if (type.equals(Type.DOUBLE_TYPE)) {
        il.add(new VarInsnNode(Opcodes.DLOAD, argLocation));
    } else {/*from  w w  w  . j  av a  2s . c  o m*/
        il.add(new VarInsnNode(Opcodes.ALOAD, argLocation));
    }
}