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.diorite.inject.controller.TransformerFieldInjector.java

License:Open Source License

private void injectField(TransformerFieldPair fieldPair, TransformerInjectTracker result) {
    ControllerFieldData<?> fieldData = fieldPair.data;
    assert fieldData != null;

    MethodInsnNode injectionInvokeNode = result.getResult();
    FieldInsnNode putfieldNode = result.getFieldInsnNode();

    // insert invoke methods:
    MethodNode codeBefore = new MethodNode();
    MethodNode codeAfter = new MethodNode();
    this.injectTransformer.fillMethodInvokes(codeBefore, codeAfter, fieldData);

    // node list for PUTFIELD
    InsnList initNodeList = result.getInitNodeList();
    // node list for invoke execution
    InsnList resultNodeList = result.getResultNodeList();

    if (codeBefore.instructions.size() != 0) {
        // invoke before should be added before PUTFIELD and before INVOKE
        resultNodeList.insertBefore(injectionInvokeNode, codeBefore.instructions);
    }//w  w  w. ja  v  a2  s.c o m
    if (codeAfter.instructions.size() != 0) {
        // invoke after should be added after PUTFIELD
        initNodeList.insert(putfieldNode, codeAfter.instructions);
    }

    // and replace placeholder node with real injections:
    {
        MethodNode tempNode = new MethodNode();
        TransformerInvokerGenerator.generateFieldInjection(this.injectTransformer.classData, fieldData,
                tempNode, -1);
        resultNodeList.insertBefore(injectionInvokeNode, tempNode.instructions);
        resultNodeList.remove(injectionInvokeNode);
    }
}

From source file:org.diorite.inject.controller.TransformerMethodInjector.java

License:Open Source License

private void injectMethods() {
    MethodNode methodNode = new MethodNode();
    for (ControllerMethodData methodData : this.injectTransformer.classData.getMethods()) {
        TransformerInvokerGenerator.generateMethodInjection(this.injectTransformer.classData, methodData,
                methodNode, true, -1);/*from w  w w  .j  ava2  s.c  o m*/
    }
    for (TransformerInitMethodData methodData : this.injectTransformer.inits.values()) {
        InsnList instructions = methodData.node.instructions;
        for (InsnNode node : methodData.returns) {
            instructions.insertBefore(node, methodNode.instructions);
        }
    }
}

From source file:org.diorite.inject.impl.controller.TransformerFieldInjector.java

License:Open Source License

private void injectField(TransformerFieldPair fieldPair, TransformerInjectTracker result) {
    ControllerFieldData<?> fieldData = fieldPair.data;
    assert fieldData != null;

    MethodInsnNode injectionInvokeNode = result.getResult();
    FieldInsnNode putfieldNode = result.getFieldInsnNode();

    // insert invoke methods:
    MethodNode codeBefore = new MethodNode();
    MethodNode codeAfter = new MethodNode();
    this.injectTransformer.fillMethodInvokes(codeBefore, codeAfter, fieldData);

    // node list for PUTFIELD
    InsnList initNodeList = result.getInitNodeList();
    // node list for invoke execution
    InsnList resultNodeList = result.getResultNodeList();

    if (codeBefore.instructions.size() != 0) {
        // invoke before should be added before PUTFIELD and before INVOKE
        resultNodeList.insertBefore(injectionInvokeNode, codeBefore.instructions);
    }/*  ww  w  .j  a va2s.  co  m*/
    if (codeAfter.instructions.size() != 0) {
        // invoke after should be added after PUTFIELD
        initNodeList.insert(putfieldNode, codeAfter.instructions);
    }

    // and replace placeholder node with real injections:
    {
        MethodNode tempNode = new MethodNode();
        TransformerInvokerGenerator.generateFieldInjection(this.injectTransformer.classData, fieldData,
                tempNode, -1, fieldPair.placeholderType);
        resultNodeList.insertBefore(injectionInvokeNode, tempNode.instructions);
        resultNodeList.remove(injectionInvokeNode);
    }
}

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 a  2s.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.MoveCodeToCallOrigAdapter.java

License:Open Source License

/** To avoid infinite recursion, calls super.m(a1, a2) must be translated to super.callOrig(boundMethodId, new Object[] {a1, a2}). */
private void adjustSuperCalls(InsnList instructions, String selector, Type[] args, Type returnType,
        int boundMethodIdSlot) {
    // search:// w w  w .  j  a va2s . c o m
    List<MethodInsnNode> toReplace = new ArrayList<MethodInsnNode>();
    ListIterator<AbstractInsnNode> orgMethodIter = instructions.iterator();
    while (orgMethodIter.hasNext()) {
        AbstractInsnNode orgMethodNode = orgMethodIter.next();
        if (orgMethodNode.getOpcode() == Opcodes.INVOKESPECIAL
                && ((MethodInsnNode) orgMethodNode).name.equals(selector))
            toReplace.add((MethodInsnNode) orgMethodNode);
    }
    if (toReplace.isEmpty())
        return;
    // replace:
    for (MethodInsnNode oldNode : toReplace) {
        // we need to insert into the loading sequence before the invocation, find the insertion points:
        AbstractInsnNode[] insertionPoints = StackBalanceAnalyzer.findInsertionPointsBefore(oldNode, args);
        AbstractInsnNode firstInsert = insertionPoints.length > 0 ? insertionPoints[0] : oldNode;

        // push first arg to _OT$callOrig():
        instructions.insertBefore(firstInsert, new IntInsnNode(Opcodes.ILOAD, boundMethodIdSlot));

        // prepare array as second arg to _OT$callOrig():
        instructions.insertBefore(firstInsert, new IntInsnNode(Opcodes.BIPUSH, args.length));
        instructions.insertBefore(firstInsert, new TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/Object"));

        for (int i = 0; i < insertionPoints.length; i++) {
            // NB: each iteration has an even stack balance, where the top is the Object[].
            instructions.insertBefore(insertionPoints[i], new InsnNode(Opcodes.DUP));
            instructions.insertBefore(insertionPoints[i], new IntInsnNode(Opcodes.BIPUSH, i));
            // leave the original loading sequence in tact and continue at the next point:
            AbstractInsnNode insertAt = (i + 1 < insertionPoints.length) ? insertionPoints[i + 1] : oldNode;
            instructions.insertBefore(insertAt, AsmTypeHelper.getBoxingInstructionForType(args[i]));
            instructions.insertBefore(insertAt, new InsnNode(Opcodes.AASTORE));
        }

        if (returnType == Type.VOID_TYPE)
            instructions.insert(oldNode, new InsnNode(Opcodes.POP));
        else
            instructions.insert(oldNode, AsmTypeHelper.getUnboxingInstructionForType(returnType));

        instructions.set(oldNode, new MethodInsnNode(Opcodes.INVOKESPECIAL, ((MethodInsnNode) oldNode).owner,
                callOrig.getName(), callOrig.getSignature()));
    }
}

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

License:Open Source License

/**
 * Insert a call to the isNull helper function
 * /* w w  w  .  ja va 2s. 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 {} in method {}", branchId,
            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 }));
    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 }));
    list.insertBefore(position, push);

}

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

License:Open Source License

/**
 * Insert a call to the reference equality check helper function
 * /*ww  w. j ava2s .  co  m*/
 * @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 }));
    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 }));
    list.insertBefore(position, push);

}

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

License:Open Source License

/**
 * Insert a call to the distance function for unary comparison
 * /*  w w  w  .j  a  va  2s .co 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 }));
    list.insertBefore(position, push);

}

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

License:Open Source License

/**
 * Insert a call to the distance function for binary comparison
 * //from  w w  w  .  j a v  a 2s. 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 }));
    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 }));
    list.insertBefore(position, push);

}

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

License:Open Source License

/**
 * Insert a call that takes a boolean from the stack, and returns the
 * appropriate distance/*w  ww .j a va2s. 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 }));
    list.insertBefore(position, get);
}