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

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

Introduction

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

Prototype

public void insert(final InsnList insnList) 

Source Link

Document

Inserts the given instructions at the beginning of this list.

Usage

From source file:gemlite.core.internal.measurement.MeasureHelper.java

License:Apache License

public final static void instrumentCheckPoint(String className, MethodNode mn) {
    if (LogUtil.getCoreLog().isTraceEnabled())
        LogUtil.getCoreLog().trace("Found check point, class:" + className + " method:" + mn.name);

    InsnList insn = mn.instructions;
    List<AbstractInsnNode> returnIndex = new ArrayList<>();
    // return/*w  ww.  ja  va 2  s  .c o m*/
    int localVarCount = mn.localVariables.size();
    for (int i = 0; i < insn.size(); i++) {
        AbstractInsnNode insnNode = (AbstractInsnNode) insn.get(i);
        switch (insnNode.getOpcode()) {
        case Opcodes.ARETURN:
        case Opcodes.IRETURN:
        case Opcodes.DRETURN:
        case Opcodes.LRETURN:
        case Opcodes.FRETURN:
            returnIndex.add(insnNode.getPrevious());
            break;
        case Opcodes.RETURN:
            returnIndex.add(insnNode);
            break;
        }
    }
    // 
    insn.insert(new VarInsnNode(Opcodes.LSTORE, localVarCount + 2));
    insn.insert(
            new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System", "currentTimeMillis", "()J", false));
    // ?
    for (AbstractInsnNode insnNode : returnIndex) {
        insn.insertBefore(insnNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "java/lang/System",
                "currentTimeMillis", "()J", false));
        insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LSTORE, localVarCount + 4));

        insn.insertBefore(insnNode, new LdcInsnNode(className));
        insn.insertBefore(insnNode, new LdcInsnNode(mn.name));
        insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LLOAD, localVarCount + 2));
        insn.insertBefore(insnNode, new VarInsnNode(Opcodes.LLOAD, localVarCount + 4));
        insn.insertBefore(insnNode,
                new MethodInsnNode(Opcodes.INVOKESTATIC, "gemlite/core/internal/measurement/MeasureHelper",
                        "recordCheckPoint", "(Ljava/lang/String;Ljava/lang/String;JJ)V", false));
    }
}

From source file:org.evosuite.instrumentation.mutation.DeleteStatement.java

License:Open Source License

/** {@inheritDoc} */
@Override// w  ww .j  av a  2s . co m
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction,
        Frame frame) {

    List<Mutation> mutations = new LinkedList<Mutation>();

    MethodInsnNode node = (MethodInsnNode) instruction.getASMNode();
    Type returnType = Type.getReturnType(node.desc);

    // insert mutation into bytecode with conditional
    InsnList mutation = new InsnList();
    logger.info("Mutation deletestatement for statement " + node.name + node.desc);
    for (Type argType : Type.getArgumentTypes(node.desc)) {
        if (argType.getSize() == 0)
            logger.info("Ignoring parameter of type " + argType);
        else if (argType.getSize() == 2) {
            mutation.insert(new InsnNode(Opcodes.POP2));
            logger.debug("Deleting parameter of 2 type " + argType);
        } else {
            logger.debug("Deleting parameter of 1 type " + argType);
            mutation.insert(new InsnNode(Opcodes.POP));
        }
    }
    if (node.getOpcode() == Opcodes.INVOKEVIRTUAL) {
        logger.debug("Deleting callee of type " + node.owner);
        mutation.add(new InsnNode(Opcodes.POP));
    } else if (node.getOpcode() == Opcodes.INVOKEINTERFACE) {
        boolean isStatic = false;
        try {
            Class<?> clazz = Class.forName(node.owner.replace('/', '.'), false,
                    DeleteStatement.class.getClassLoader());
            for (java.lang.reflect.Method method : clazz.getMethods()) {
                if (method.getName().equals(node.name)) {
                    if (Type.getMethodDescriptor(method).equals(node.desc)) {
                        if (Modifier.isStatic(method.getModifiers()))
                            isStatic = true;
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            logger.warn("Could not find class: " + node.owner + ", this is likely a severe problem");
        }
        if (!isStatic) {
            logger.info("Deleting callee of type " + node.owner);
            mutation.add(new InsnNode(Opcodes.POP));
        }
    }
    mutation.add(getDefault(returnType));

    // insert mutation into pool
    Mutation mutationObject = MutationPool.addMutation(className, methodName,
            NAME + " " + node.name + node.desc, instruction, mutation, Mutation.getDefaultInfectionDistance());

    mutations.add(mutationObject);
    return mutations;
}