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;
}
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);
}
}
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;
}
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);
}
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();
}
}
}
Aggregations