use of org.objectweb.asm.tree.ClassNode in project evosuite by EvoSuite.
the class DescriptorMapping method isOutsideMethod.
private boolean isOutsideMethod(String className, String methodName, String desc) {
Set<String> visited = new HashSet<String>();
Queue<String> parents = new LinkedList<String>();
parents.add(className);
while (!parents.isEmpty()) {
String name = parents.poll();
if (name == null)
continue;
visited.add(name);
logger.info("Visiting class " + name + " while looking for source of " + className + "." + methodName);
ClassReader reader;
try {
reader = new ClassReader(ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getClassAsStream(name));
ClassNode parent = new ClassNode();
reader.accept(parent, ClassReader.EXPAND_FRAMES);
boolean isInside = isInside(parent.name);
// boolean isInside = parent.name.startsWith(Properties.PROJECT_PREFIX.replace(".",
// "/"))
// || (!Properties.TARGET_CLASS_PREFIX.isEmpty() && parent.name.startsWith(Properties.TARGET_CLASS_PREFIX.replace(".",
// "/")));
logger.info("Checking " + parent.name);
for (Object o : parent.methods) {
MethodNode mn2 = (MethodNode) o;
if (mn2.name.equals(methodName) && mn2.desc.equals(desc)) {
if (!isInside) {
logger.info("Method " + name + " was defined outside the test package");
return true;
} else {
logger.info("Method " + name + " was defined outside the test package");
// return false;
}
}
}
for (Object o : parent.interfaces) {
String par = (String) o;
if (!visited.contains(par) && !parents.contains(par)) {
parents.add(par);
}
}
if (!visited.contains(parent.superName) && !parents.contains(parent.superName)) {
parents.add(parent.superName);
}
} catch (IOException e) {
logger.info("Error reading class " + name);
}
}
return false;
}
use of org.objectweb.asm.tree.ClassNode in project evosuite by EvoSuite.
the class DescriptorMapping method transformMethodName.
private String transformMethodName(String className, String methodName, String desc, String transformedDesc) {
Set<String> visited = new HashSet<String>();
Queue<String> parents = new LinkedList<String>();
parents.add(className);
while (!parents.isEmpty()) {
String name = parents.poll();
if (name == null)
continue;
visited.add(name);
logger.info("Visiting class " + name + " while looking for name clashes of " + className + "." + methodName + transformedDesc);
ClassReader reader;
try {
reader = new ClassReader(ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getClassAsStream(name));
ClassNode parent = new ClassNode();
reader.accept(parent, ClassReader.EXPAND_FRAMES);
if (originalDesc.containsKey(className + "." + methodName + transformedDesc)) {
logger.info("Method " + methodName + " has conflicting transformed method");
return methodName + "_transformed" + (id++);
}
for (Object o : parent.methods) {
MethodNode mn2 = (MethodNode) o;
// logger.info("Checking " + parent.name + "." + mn2.name + mn2.desc);
if (mn2.name.equals(methodName) && mn2.desc.equals(transformedDesc)) {
logger.info("Method " + methodName + " has conflicting method");
if (methodName.equals("<init>"))
// TODO: This should be a bit nicer
return null;
return methodName + "_transformed" + (id++);
}
}
for (Object o : parent.interfaces) {
String par = (String) o;
if (!visited.contains(par) && !parents.contains(par)) {
parents.add(par);
}
}
if (!visited.contains(parent.superName) && !parents.contains(parent.superName)) {
parents.add(parent.superName);
}
} catch (IOException e) {
logger.info("Error reading class " + name);
}
}
return methodName;
}
use of org.objectweb.asm.tree.ClassNode in project evosuite by EvoSuite.
the class DescriptorMapping method isOutsideField.
private boolean isOutsideField(String className, String fieldName, String desc) {
Set<String> visited = new HashSet<String>();
Queue<String> parents = new LinkedList<String>();
parents.add(className);
while (!parents.isEmpty()) {
String name = parents.poll();
if (name == null)
continue;
visited.add(name);
logger.info("Checking class " + name + " while looking for definition of field " + fieldName);
ClassReader reader;
try {
reader = new ClassReader(ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getClassAsStream(name));
ClassNode parent = new ClassNode();
reader.accept(parent, ClassReader.EXPAND_FRAMES);
boolean isInside = isInside(parent.name);
for (Object o : parent.fields) {
FieldNode mn2 = (FieldNode) o;
if (mn2.name.equals(fieldName) && mn2.desc.equals(desc)) {
// }
if (!isInside) {
logger.info("Field " + name + " was defined outside the test package - " + parent.name);
return true;
} else {
logger.info("Field " + name + " was defined inside the test package " + parent.name);
return false;
}
}
}
for (Object o : parent.interfaces) {
String par = (String) o;
if (!visited.contains(par) && !parents.contains(par)) {
parents.add(par);
}
}
if (!visited.contains(parent.superName) && !parents.contains(parent.superName)) {
parents.add(parent.superName);
}
} catch (IOException e) {
logger.info("Error reading class " + name);
}
}
return false;
}
use of org.objectweb.asm.tree.ClassNode in project evosuite by EvoSuite.
the class RegressionClassDiff method getClassInstructions.
/*
* Get bytecode instructions for the class based on the following format: Method -> List of
* instructions
*/
private static Map<String, List<Integer>> getClassInstructions(InputStream classAsInputStream) {
HashMap<String, List<Integer>> methodInstructionsMap = new HashMap<>();
try {
ClassReader reader = new ClassReader(classAsInputStream);
ClassNode classNode = new ClassNode();
reader.accept(classNode, 0);
@SuppressWarnings("unchecked") final List<MethodNode> methods = classNode.methods;
Printer printer = new Textifier();
TraceMethodVisitor mp = new TraceMethodVisitor(printer);
for (MethodNode m : methods) {
List<Integer> instructions = new ArrayList<>();
InsnList inList = m.instructions;
String mathodID = m.name + ": " + m.desc;
System.out.println(mathodID);
int[] methodInstructions = new int[inList.size()];
for (int i = 0; i < inList.size(); i++) {
int op = inList.get(i).getOpcode();
methodInstructions[i] = op;
AbstractInsnNode insn = inList.get(i);
insn.accept(mp);
// logger.warn("{} -> {}", sw.toString(), op);
if (op != -1)
instructions.add(op);
}
methodInstructionsMap.put(mathodID, instructions);
}
} catch (IOException e) {
// Will fail if ClassReader fails
e.printStackTrace();
}
return methodInstructionsMap;
}
use of org.objectweb.asm.tree.ClassNode in project Random-Things by lumien231.
the class ClassTransformer method patchWorldGenTrees.
private byte[] patchWorldGenTrees(byte[] basicClass) {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(basicClass);
classReader.accept(classNode, 0);
logger.log(Level.DEBUG, "Found WorldGenAbstractTree Class: " + classNode.name);
MethodNode setDirtAt = null;
for (MethodNode mn : classNode.methods) {
if (mn.name.equals(MCPNames.method("func_175921_a"))) {
setDirtAt = mn;
break;
}
}
if (setDirtAt != null) {
logger.log(Level.DEBUG, " - Patching setDirtAt");
for (int i = 0; i < setDirtAt.instructions.size(); i++) {
AbstractInsnNode ain = setDirtAt.instructions.get(i);
if (ain instanceof JumpInsnNode) {
JumpInsnNode jin = (JumpInsnNode) ain;
if (jin.getOpcode() == Opcodes.IF_ACMPEQ) {
LabelNode l = jin.label;
InsnList toInsert = new InsnList();
toInsert.add(new VarInsnNode(ALOAD, 1));
toInsert.add(new VarInsnNode(ALOAD, 2));
toInsert.add(new MethodInsnNode(INVOKEVIRTUAL, "net/minecraft/world/World", MCPNames.method("func_180495_p"), "(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/state/IBlockState;", false));
toInsert.add(new MethodInsnNode(INVOKEINTERFACE, "net/minecraft/block/state/IBlockState", MCPNames.method("func_177230_c"), "()Lnet/minecraft/block/Block;", true));
toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "protectGround", "(Lnet/minecraft/block/Block;)Z", false));
toInsert.add(new JumpInsnNode(IFGT, new LabelNode(l.getLabel())));
setDirtAt.instructions.insert(jin, toInsert);
logger.log(Level.DEBUG, " - Patched setDirtAt");
break;
}
}
}
}
CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(writer);
return writer.toByteArray();
}
Aggregations