Search in sources :

Example 41 with FieldInsnNode

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

the class DeleteField method getInfectionDistance.

/**
 * <p>
 * getInfectionDistance
 * </p>
 *
 * @param original
 *            a {@link org.objectweb.asm.tree.FieldInsnNode} object.
 * @param mutant
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(FieldInsnNode original, InsnList mutant) {
    InsnList distance = new InsnList();
    if (original.getOpcode() == Opcodes.GETFIELD)
        // make sure to re-load this for GETFIELD
        distance.add(new InsnNode(Opcodes.DUP));
    distance.add(new FieldInsnNode(original.getOpcode(), original.owner, original.name, original.desc));
    Type type = Type.getType(original.desc);
    if (type.getDescriptor().startsWith("L") || type.getDescriptor().startsWith("[")) {
        ReplaceVariable.addReferenceDistanceCheck(distance, type, mutant);
    } else {
        ReplaceVariable.addPrimitiveDistanceCheck(distance, type, mutant);
    }
    return distance;
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList)

Example 42 with FieldInsnNode

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

the class InsertUnaryOperator method getName.

private String getName(MethodNode mn, AbstractInsnNode node) throws VariableNotFoundException {
    if (node instanceof VarInsnNode) {
        LocalVariableNode var = getLocal(mn, node, ((VarInsnNode) node).var);
        return var.name;
    } else if (node instanceof FieldInsnNode) {
        return ((FieldInsnNode) node).name;
    } else if (node instanceof IincInsnNode) {
        IincInsnNode incNode = (IincInsnNode) node;
        LocalVariableNode var = getLocal(mn, node, incNode.var);
        return var.name;
    } 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 43 with FieldInsnNode

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

the class InsertUnaryOperator method apply.

/* (non-Javadoc)
	 * @see org.evosuite.cfg.instrumentation.mutation.MutationOperator#apply(org.objectweb.asm.tree.MethodNode, java.lang.String, java.lang.String, org.evosuite.cfg.BytecodeInstruction)
	 */
/**
 * {@inheritDoc}
 */
@Override
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction, Frame frame) {
    // TODO - need to keep InsnList in Mutation, not only Instruction?
    // Mutation: Insert an INEG _after_ an iload
    List<Mutation> mutations = new LinkedList<Mutation>();
    List<InsnList> mutationCode = new LinkedList<InsnList>();
    List<String> descriptions = new LinkedList<String>();
    if (instruction.getASMNode() instanceof VarInsnNode) {
        try {
            InsnList mutation = new InsnList();
            VarInsnNode node = (VarInsnNode) instruction.getASMNode();
            // insert mutation into bytecode with conditional
            mutation.add(new VarInsnNode(node.getOpcode(), node.var));
            mutation.add(new InsnNode(getNegation(node.getOpcode())));
            mutationCode.add(mutation);
            if (!mn.localVariables.isEmpty())
                descriptions.add("Negation of " + getName(mn, node));
            else
                descriptions.add("Negation");
            if (node.getOpcode() == Opcodes.ILOAD) {
                if (frame.getStack(frame.getStackSize() - 1) != BooleanValueInterpreter.BOOLEAN_VALUE) {
                    mutation = new InsnList();
                    mutation.add(new IincInsnNode(node.var, 1));
                    mutation.add(new VarInsnNode(node.getOpcode(), node.var));
                    if (!mn.localVariables.isEmpty())
                        descriptions.add("IINC 1 " + getName(mn, node));
                    else
                        descriptions.add("IINC 1");
                    mutationCode.add(mutation);
                    mutation = new InsnList();
                    mutation.add(new IincInsnNode(node.var, -1));
                    mutation.add(new VarInsnNode(node.getOpcode(), node.var));
                    if (!mn.localVariables.isEmpty())
                        descriptions.add("IINC -1 " + getName(mn, node));
                    else
                        descriptions.add("IINC -1");
                    mutationCode.add(mutation);
                }
            }
        } catch (VariableNotFoundException e) {
            logger.info("Could not find variable: " + e);
            return new ArrayList<Mutation>();
        }
    } else {
        InsnList mutation = new InsnList();
        FieldInsnNode node = (FieldInsnNode) instruction.getASMNode();
        Type type = Type.getType(node.desc);
        mutation.add(new FieldInsnNode(node.getOpcode(), node.owner, node.name, node.desc));
        mutation.add(new InsnNode(getNegation(type)));
        descriptions.add("Negation");
        mutationCode.add(mutation);
        if (type == Type.INT_TYPE) {
            mutation = new InsnList();
            mutation.add(new FieldInsnNode(node.getOpcode(), node.owner, node.name, node.desc));
            mutation.add(new InsnNode(Opcodes.ICONST_1));
            mutation.add(new InsnNode(Opcodes.IADD));
            descriptions.add("+1");
            mutationCode.add(mutation);
            mutation = new InsnList();
            mutation.add(new FieldInsnNode(node.getOpcode(), node.owner, node.name, node.desc));
            mutation.add(new InsnNode(Opcodes.ICONST_M1));
            mutation.add(new InsnNode(Opcodes.IADD));
            descriptions.add("-1");
            mutationCode.add(mutation);
        }
    }
    int i = 0;
    for (InsnList mutation : mutationCode) {
        // insert mutation into pool
        Mutation mutationObject = MutationPool.addMutation(className, methodName, NAME + " " + descriptions.get(i++), instruction, mutation, Mutation.getDefaultInfectionDistance());
        mutations.add(mutationObject);
    }
    return mutations;
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) LinkedList(java.util.LinkedList) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) IincInsnNode(org.objectweb.asm.tree.IincInsnNode) Mutation(org.evosuite.coverage.mutation.Mutation) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 44 with FieldInsnNode

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

the class GetStaticGraphGenerator method handleMethodNode.

/**
 * Add all possible calls for a given method
 *
 * @param callGraph
 * @param mn
 */
@SuppressWarnings("unchecked")
private static void handleMethodNode(GetStaticGraph staticUsageTree, ClassNode cn, MethodNode mn, int depth) {
    InsnList instructions = mn.instructions;
    Iterator<AbstractInsnNode> iterator = instructions.iterator();
    // TODO: This really shouldn't be here but in its own class
    while (iterator.hasNext()) {
        AbstractInsnNode insn = iterator.next();
        if (insn instanceof MethodInsnNode) {
            handleMethodInsnNode(staticUsageTree, cn, mn, (MethodInsnNode) insn, depth + 1);
        } else if (insn instanceof FieldInsnNode) {
            handleFieldInsnNode(staticUsageTree, cn, mn, (FieldInsnNode) insn, depth + 1);
        }
    }
}
Also used : MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 45 with FieldInsnNode

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

the class Instrumenter method instrumentGETXXXFieldAccesses.

private void instrumentGETXXXFieldAccesses(final ClassNode cn, final String internalClassName, final MethodNode methodNode) {
    final InsnList instructions = methodNode.instructions;
    AbstractInsnNode ins = null;
    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++;
            }
        }
    }
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) 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) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

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