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

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

Introduction

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

Prototype

public void insertBefore(final AbstractInsnNode nextInsn, final InsnList insnList) 

Source Link

Document

Inserts the given instructions before the specified instruction.

Usage

From source file:org.evosuite.instrumentation.testability.BooleanTestabilityTransformation.java

License:Open Source License

/**
 * Insert a call to the isNull helper function
 * /*from  w w  w.  ja v a2s .  co  m*/
 * @param opcode
 * @param position
 * @param list
 */
public void insertPushNull(int opcode, JumpInsnNode position, InsnList list) {
    int branchId = getBranchID(currentMethodNode, position);
    logger.info("Inserting instrumentation for NULL check at branch " + branchId + " in method "
            + currentMethodNode.name);

    MethodInsnNode nullCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
            Type.getInternalName(BooleanHelper.class), "isNull",
            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.getType(Object.class), Type.INT_TYPE }),
            false);
    list.insertBefore(position, new InsnNode(Opcodes.DUP));
    list.insertBefore(position, new LdcInsnNode(opcode));
    list.insertBefore(position, nullCheck);
    //list.insertBefore(position,
    //                  new LdcInsnNode(getBranchID(currentMethodNode, position)));
    insertBranchIdPlaceholder(currentMethodNode, position, branchId);
    MethodInsnNode push = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class),
            "pushPredicate",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE, Type.INT_TYPE }), false);
    list.insertBefore(position, push);

}

From source file:org.evosuite.instrumentation.testability.BooleanTestabilityTransformation.java

License:Open Source License

/**
 * Insert a call to the reference equality check helper function
 * /*from  ww  w  . ja  va2 s . c om*/
 * @param opcode
 * @param position
 * @param list
 */
public void insertPushEquals(int opcode, JumpInsnNode position, InsnList list) {
    MethodInsnNode equalCheck = new MethodInsnNode(Opcodes.INVOKESTATIC,
            Type.getInternalName(BooleanHelper.class), "isEqual",
            Type.getMethodDescriptor(Type.INT_TYPE,
                    new Type[] { Type.getType(Object.class), Type.getType(Object.class), Type.INT_TYPE }),
            false);
    list.insertBefore(position, new InsnNode(Opcodes.DUP2));
    list.insertBefore(position, new LdcInsnNode(opcode));
    list.insertBefore(position, equalCheck);
    //list.insertBefore(position,
    //                  new LdcInsnNode(getBranchID(currentMethodNode, position)));
    insertBranchIdPlaceholder(currentMethodNode, position);
    MethodInsnNode push = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class),
            "pushPredicate",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE, Type.INT_TYPE }), false);
    list.insertBefore(position, push);

}

From source file:org.evosuite.instrumentation.testability.BooleanTestabilityTransformation.java

License:Open Source License

/**
 * Insert a call to the distance function for unary comparison
 * /*  w  w w  .j  a  v  a 2 s .c o m*/
 * @param opcode
 * @param position
 * @param list
 */
public void insertPush(int opcode, JumpInsnNode position, InsnList list) {
    list.insertBefore(position, new InsnNode(Opcodes.DUP));
    // TODO: We have to put a placeholder here instead of the actual branch ID
    // TODO: And then later add another transformation where we replace this with
    //       actual branch IDs
    //list.insertBefore(position,
    //                  new LdcInsnNode(getBranchID(currentMethodNode, position)));
    insertBranchIdPlaceholder(currentMethodNode, position);
    MethodInsnNode push = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class),
            "pushPredicate",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE, Type.INT_TYPE }), false);
    list.insertBefore(position, push);

}

From source file:org.evosuite.instrumentation.testability.BooleanTestabilityTransformation.java

License:Open Source License

/**
 * Insert a call to the distance function for binary comparison
 * /*from  w ww .  j av  a 2  s .  c  o m*/
 * @param opcode
 * @param position
 * @param list
 */
public void insertPush2(int opcode, JumpInsnNode position, InsnList list) {
    list.insertBefore(position, new InsnNode(Opcodes.DUP2));
    //list.insertBefore(position, new InsnNode(Opcodes.ISUB));
    MethodInsnNode sub = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class),
            "intSub", Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.INT_TYPE, Type.INT_TYPE }),
            false);
    list.insertBefore(position, sub);
    insertBranchIdPlaceholder(currentMethodNode, position);

    //      list.insertBefore(position,
    //                        new LdcInsnNode(getBranchID(currentMethodNode, position)));
    MethodInsnNode push = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class),
            "pushPredicate",
            Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.INT_TYPE, Type.INT_TYPE }), false);
    list.insertBefore(position, push);

}

From source file:org.evosuite.instrumentation.testability.BooleanTestabilityTransformation.java

License:Open Source License

/**
 * Insert a call that takes a boolean from the stack, and returns the
 * appropriate distance/* w w w .  java 2 s .  co m*/
 * 
 * @param position
 * @param list
 */
public void insertGetBefore(AbstractInsnNode position, InsnList list) {
    logger.info("Inserting get call before");
    // Here, branchId is the first control dependency
    //list.insertBefore(position,
    //                  new LdcInsnNode(getControlDependentBranchID(currentMethodNode,
    //                                                              position)));
    // insertControlDependencyPlaceholder(currentMethodNode, position);

    // branch
    // approx
    // value

    Label label = new Label();
    LabelNode labelNode = new LabelNode(label);
    //BooleanTestabilityPlaceholderTransformer.addControlDependencyPlaceholder(label,
    //                                                                         insnNode);
    currentMethodNode.instructions.insertBefore(position, labelNode);
    //instructions.insertBefore(insnNode, new LdcInsnNode(0));
    //mn.instructions.insertBefore(insnNode, new LdcInsnNode(0));
    currentMethodNode.instructions.insertBefore(position,
            new LdcInsnNode(getControlDependentBranchID(currentMethodNode, position)));
    currentMethodNode.instructions.insertBefore(position, new InsnNode(Opcodes.SWAP));
    currentMethodNode.instructions.insertBefore(position,
            new LdcInsnNode(getApproximationLevel(currentMethodNode, position)));
    currentMethodNode.instructions.insertBefore(position, new InsnNode(Opcodes.SWAP));

    MethodInsnNode get = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class),
            "getDistance",
            Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.INT_TYPE, Type.INT_TYPE, Type.INT_TYPE }),
            false);
    list.insertBefore(position, get);
}

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;/*from  www . ja  v  a 2 s.c o  m*/
    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;/*  ww w .  j  a  v a2 s  . c o 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.jacoco.playground.filter.MethodDumper.java

License:Open Source License

private static void insertLabels(InsnList list, TestCaseDumper printer) {
    boolean hasLabel = false;
    for (AbstractInsnNode node = list.getFirst(); node != null; node = node.getNext()) {
        switch (node.getType()) {
        case AbstractInsnNode.LINE:
            break;
        case AbstractInsnNode.LABEL:
            printer.declareLabel(((LabelNode) node).getLabel());
            hasLabel = true;/*from   w  ww  .j  a  va  2s.  c o m*/
            break;
        default:
            if (hasLabel) {
                hasLabel = false;
            } else {
                final Label label = new Label();
                printer.declareLabel(label);
                list.insertBefore(node, new LabelNode(label));
            }
            break;
        }
    }
}

From source file:org.parboiled.transform.process.GroupClassGenerator.java

License:Apache License

protected static void insertSetContextCalls(final InstructionGroup group, int localVarIx) {
    final InsnList instructions = group.getInstructions();
    final CodeBlock block = CodeBlock.newCodeBlock();

    for (final InstructionGraphNode node : group.getNodes()) {
        if (!node.isCallOnContextAware())
            continue;

        final AbstractInsnNode insn = node.getInstruction();

        if (node.getPredecessors().size() > 1) {
            // store the target of the call in a new local variable
            final AbstractInsnNode loadTarget = node.getPredecessors().get(0).getInstruction();

            block.clear().dup().astore(++localVarIx);
            instructions.insert(loadTarget, block.getInstructionList());

            // immediately before the call get the target from the local var
            // and set the context on it
            instructions.insertBefore(insn, new VarInsnNode(ALOAD, localVarIx));
        } else {/*ww w.  ja va  2  s.  co  m*/
            // if we have only one predecessor the call does not take any
            // parameters and we can skip the storing and loading of the
            // invocation target
            instructions.insertBefore(insn, new InsnNode(DUP));
        }

        block.clear().aload(1).invokeinterface(CodegenUtils.p(ContextAware.class), "setContext",
                CodegenUtils.sig(void.class, Context.class));

        instructions.insertBefore(insn, block.getInstructionList());
    }
}

From source file:org.parboiled.transform.process.LabellingGenerator.java

License:Apache License

@Override
public void process(@Nonnull final ParserClassNode classNode, @Nonnull final RuleMethod method)
        throws Exception {
    Objects.requireNonNull(classNode, "classNode");
    Objects.requireNonNull(method, "method");
    // super methods have flag moved to the overriding method
    Preconditions.checkState(!method.isSuperMethod());

    final InsnList instructions = method.instructions;

    AbstractInsnNode retInsn = instructions.getLast();
    while (retInsn.getOpcode() != ARETURN)
        retInsn = retInsn.getPrevious();

    final LabelNode label = new LabelNode();
    final CodeBlock block = CodeBlock.newCodeBlock().dup().ifnull(label).ldc(getLabelText(method))
            .invokeinterface(CodegenUtils.p(Rule.class), "label", CodegenUtils.sig(Rule.class, String.class))
            .label(label);/* w w w.j av  a 2 s .  c om*/

    instructions.insertBefore(retInsn, block.getInstructionList());
}