Search in sources :

Example 36 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project evosuite by EvoSuite.

the class ReplaceVariable method getFieldReplacements.

private Map<String, InsnList> getFieldReplacements(MethodNode mn, String className, String desc, AbstractInsnNode node) {
    Map<String, InsnList> alternatives = new HashMap<String, InsnList>();
    boolean isStatic = (mn.access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC;
    String otherName = "";
    if (node instanceof FieldInsnNode) {
        FieldInsnNode fNode = (FieldInsnNode) node;
        otherName = fNode.name;
    }
    try {
        logger.info("Checking class " + className);
        Class<?> clazz = Class.forName(className, false, ReplaceVariable.class.getClassLoader());
        for (Field field : TestClusterUtils.getFields(clazz)) {
            // that we access the CUT before it is fully initialised
            if (!canUse(field))
                continue;
            Type type = Type.getType(field.getType());
            logger.info("Checking replacement field variable " + field.getName());
            if (field.getName().equals(otherName))
                continue;
            if (isStatic && !(Modifier.isStatic(field.getModifiers())))
                continue;
            if (type.getDescriptor().equals(desc)) {
                logger.info("Adding replacement field variable " + field.getName());
                InsnList list = new InsnList();
                if (node.getOpcode() == Opcodes.GETFIELD) {
                    // Remove field owner from stack
                    list.add(new InsnNode(Opcodes.POP));
                }
                // new fieldinsnnode
                if (Modifier.isStatic(field.getModifiers()))
                    list.add(new FieldInsnNode(Opcodes.GETSTATIC, className.replace('.', '/'), field.getName(), type.getDescriptor()));
                else {
                    // this
                    list.add(new VarInsnNode(Opcodes.ALOAD, 0));
                    list.add(new FieldInsnNode(Opcodes.GETFIELD, className.replace('.', '/'), field.getName(), type.getDescriptor()));
                }
                alternatives.put(field.getName(), list);
            } else {
                logger.info("Descriptor does not match: " + field.getName() + " - " + type.getDescriptor());
            }
        }
    } catch (Throwable t) {
        logger.info("Class not found: " + className);
    // TODO Auto-generated catch block
    // e.printStackTrace();
    }
    return alternatives;
}
Also used : HashMap(java.util.HashMap) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) Field(java.lang.reflect.Field) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 37 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project evosuite by EvoSuite.

the class ReplaceVariable method getType.

private Type getType(MethodNode mn, AbstractInsnNode node) throws VariableNotFoundException {
    if (node instanceof VarInsnNode) {
        LocalVariableNode var = getLocal(mn, node, ((VarInsnNode) node).var);
        return Type.getType(var.desc);
    } else if (node instanceof FieldInsnNode) {
        return Type.getType(((FieldInsnNode) node).desc);
    } else if (node instanceof IincInsnNode) {
        IincInsnNode incNode = (IincInsnNode) node;
        LocalVariableNode var = getLocal(mn, node, incNode.var);
        return Type.getType(var.desc);
    } else {
        throw new RuntimeException("Unknown variable node: " + node);
    }
}
Also used : IincInsnNode(org.objectweb.asm.tree.IincInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) LocalVariableNode(org.objectweb.asm.tree.LocalVariableNode)

Example 38 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project evosuite by EvoSuite.

the class ReplaceVariable method getReplacements.

/**
 * Retrieve the set of variables that have the same type and are in scope
 *
 * @param node
 * @return
 */
private Map<String, InsnList> getReplacements(MethodNode mn, String className, AbstractInsnNode node, Frame frame) {
    Map<String, InsnList> variables = new HashMap<String, InsnList>();
    if (node instanceof VarInsnNode) {
        VarInsnNode var = (VarInsnNode) node;
        try {
            LocalVariableNode origVar = getLocal(mn, node, var.var);
            // LocalVariableNode origVar = (LocalVariableNode) mn.localVariables.get(var.var);
            logger.debug("Looking for replacements for " + origVar.name + " of type " + origVar.desc + " at index " + origVar.index);
            // FIXXME: ASM gets scopes wrong, so we only use primitive vars?
            // if (!origVar.desc.startsWith("L"))
            variables.putAll(getLocalReplacements(mn, origVar.desc, node, frame));
            variables.putAll(getFieldReplacements(mn, className, origVar.desc, node));
        } catch (VariableNotFoundException e) {
            logger.info("Could not find variable, not replacing it: " + var.var);
            Iterator<?> it = mn.localVariables.iterator();
            while (it.hasNext()) {
                LocalVariableNode n = (LocalVariableNode) it.next();
                logger.info(n.index + ": " + n.name);
            }
            logger.info(e.toString());
            e.printStackTrace();
        }
    } else if (node instanceof FieldInsnNode) {
        FieldInsnNode field = (FieldInsnNode) node;
        if (field.owner.replace('/', '.').equals(className)) {
            logger.info("Looking for replacements for static field " + field.name + " of type " + field.desc);
            variables.putAll(getLocalReplacements(mn, field.desc, node, frame));
            variables.putAll(getFieldReplacements(mn, className, field.desc, node));
        }
    } else if (node instanceof IincInsnNode) {
        IincInsnNode incNode = (IincInsnNode) node;
        try {
            LocalVariableNode origVar = getLocal(mn, node, incNode.var);
            variables.putAll(getLocalReplacementsInc(mn, origVar.desc, incNode, frame));
        } catch (VariableNotFoundException e) {
            logger.info("Could not find variable, not replacing it: " + incNode.var);
        }
    } else {
    // throw new RuntimeException("Unknown type: " + node);
    }
    return variables;
}
Also used : HashMap(java.util.HashMap) Iterator(java.util.Iterator) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) LocalVariableNode(org.objectweb.asm.tree.LocalVariableNode)

Example 39 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project evosuite by EvoSuite.

the class MutationInstrumentation method addInstrumentation.

/**
 * <p>
 * addInstrumentation
 * </p>
 *
 * @param mn
 *            a {@link org.objectweb.asm.tree.MethodNode} object.
 * @param original
 *            a {@link org.objectweb.asm.tree.AbstractInsnNode} object.
 * @param mutations
 *            a {@link java.util.List} object.
 */
protected void addInstrumentation(MethodNode mn, AbstractInsnNode original, List<Mutation> mutations) {
    InsnList instructions = new InsnList();
    // TODO: All mutations in the id are touched, not just one!
    for (Mutation mutation : mutations) {
        instructions.add(mutation.getInfectionDistance());
        instructions.add(new LdcInsnNode(mutation.getId()));
        MethodInsnNode touched = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(ExecutionTracer.class), "passedMutation", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.DOUBLE_TYPE, Type.INT_TYPE }), false);
        instructions.add(touched);
    }
    LabelNode endLabel = new LabelNode();
    for (Mutation mutation : mutations) {
        LabelNode nextLabel = new LabelNode();
        LdcInsnNode mutationId = new LdcInsnNode(mutation.getId());
        instructions.add(mutationId);
        FieldInsnNode activeId = new FieldInsnNode(Opcodes.GETSTATIC, Type.getInternalName(MutationObserver.class), "activeMutation", "I");
        instructions.add(activeId);
        instructions.add(new JumpInsnNode(Opcodes.IF_ICMPNE, nextLabel));
        instructions.add(mutation.getMutation());
        instructions.add(new JumpInsnNode(Opcodes.GOTO, endLabel));
        instructions.add(nextLabel);
    }
    mn.instructions.insertBefore(original, instructions);
    mn.instructions.insert(original, endLabel);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) Type(org.objectweb.asm.Type) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) MutationObserver(org.evosuite.coverage.mutation.MutationObserver) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) Mutation(org.evosuite.coverage.mutation.Mutation) InsnList(org.objectweb.asm.tree.InsnList) ExecutionTracer(org.evosuite.testcase.execution.ExecutionTracer)

Example 40 with FieldInsnNode

use of org.objectweb.asm.tree.FieldInsnNode in project evosuite by EvoSuite.

the class MethodNodeTransformer method transform.

/**
 * <p>transform</p>
 *
 * @param mn a {@link org.objectweb.asm.tree.MethodNode} object.
 */
public void transform(MethodNode mn) {
    setupLocals(mn);
    Set<AbstractInsnNode> originalNodes = new HashSet<AbstractInsnNode>();
    AbstractInsnNode node = mn.instructions.getFirst();
    while (node != mn.instructions.getLast()) {
        originalNodes.add(node);
        node = node.getNext();
    }
    // int currentIndex = 0;
    node = mn.instructions.getFirst();
    // while (currentIndex < mn.instructions.size()) {
    boolean finished = false;
    while (!finished) {
        // } else
        if (node instanceof MethodInsnNode) {
            node = transformMethodInsnNode(mn, (MethodInsnNode) node);
        } else if (node instanceof VarInsnNode) {
            node = transformVarInsnNode(mn, (VarInsnNode) node);
        } else if (node instanceof FieldInsnNode) {
            node = transformFieldInsnNode(mn, (FieldInsnNode) node);
        } else if (node instanceof InsnNode) {
            node = transformInsnNode(mn, (InsnNode) node);
        } else if (node instanceof TypeInsnNode) {
            node = transformTypeInsnNode(mn, (TypeInsnNode) node);
        } else if (node instanceof JumpInsnNode) {
            node = transformJumpInsnNode(mn, (JumpInsnNode) node);
        } else if (node instanceof LabelNode) {
            node = transformLabelNode(mn, (LabelNode) node);
        } else if (node instanceof IntInsnNode) {
            node = transformIntInsnNode(mn, (IntInsnNode) node);
        } else if (node instanceof MultiANewArrayInsnNode) {
            node = transformMultiANewArrayInsnNode(mn, (MultiANewArrayInsnNode) node);
        }
        if (node == mn.instructions.getLast()) {
            finished = true;
        } else {
            node = node.getNext();
        }
    }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) MultiANewArrayInsnNode(org.objectweb.asm.tree.MultiANewArrayInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) MultiANewArrayInsnNode(org.objectweb.asm.tree.MultiANewArrayInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) HashSet(java.util.HashSet)

Aggregations

FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)53 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)29 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)28 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)26 InsnList (org.objectweb.asm.tree.InsnList)24 InsnNode (org.objectweb.asm.tree.InsnNode)20 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)20 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)16 MethodNode (org.objectweb.asm.tree.MethodNode)15 Type (org.objectweb.asm.Type)14 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)13 IincInsnNode (org.objectweb.asm.tree.IincInsnNode)11 ClassNode (org.objectweb.asm.tree.ClassNode)10 LabelNode (org.objectweb.asm.tree.LabelNode)9 IntInsnNode (org.objectweb.asm.tree.IntInsnNode)8 ClassReader (org.objectweb.asm.ClassReader)7 Label (org.objectweb.asm.Label)5 FieldNode (org.objectweb.asm.tree.FieldNode)5 LocalVariableNode (org.objectweb.asm.tree.LocalVariableNode)5 FrameNode (org.objectweb.asm.tree.FrameNode)4