Search in sources :

Example 41 with MethodNode

use of org.objectweb.asm.tree.MethodNode 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;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 42 with MethodNode

use of org.objectweb.asm.tree.MethodNode 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;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Printer(org.objectweb.asm.util.Printer) Textifier(org.objectweb.asm.util.Textifier) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) ArrayList(java.util.ArrayList) InsnList(org.objectweb.asm.tree.InsnList) List(java.util.List) ResourceList(org.evosuite.classpath.ResourceList)

Example 43 with MethodNode

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

the class CastClassAnalyzer method handle.

@SuppressWarnings("unchecked")
public void handle(ClassNode targetClass, String methodName, int depth) {
    handleClassSignature(targetClass);
    List<MethodNode> methods = targetClass.methods;
    for (MethodNode mn : methods) {
        if (methodName.equals(mn.name + mn.desc))
            handleMethodNode(targetClass, mn, depth);
    }
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode)

Example 44 with MethodNode

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

the class CastClassAnalyzer method handle.

@SuppressWarnings("unchecked")
public void handle(ClassNode targetClass, int depth) {
    handleClassSignature(targetClass);
    List<MethodNode> methods = targetClass.methods;
    for (MethodNode mn : methods) {
        logger.debug("Method: " + mn.name);
        handleMethodNode(targetClass, mn, depth);
    }
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode)

Example 45 with MethodNode

use of org.objectweb.asm.tree.MethodNode 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();
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Aggregations

MethodNode (org.objectweb.asm.tree.MethodNode)322 ClassNode (org.objectweb.asm.tree.ClassNode)123 Test (org.junit.Test)94 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)59 ClassReader (org.objectweb.asm.ClassReader)57 InsnList (org.objectweb.asm.tree.InsnList)49 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)47 Label (org.objectweb.asm.Label)44 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)41 InsnNode (org.objectweb.asm.tree.InsnNode)34 ClassWriter (org.objectweb.asm.ClassWriter)26 FieldNode (org.objectweb.asm.tree.FieldNode)26 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 ArrayList (java.util.ArrayList)24 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)24 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)21 LabelNode (org.objectweb.asm.tree.LabelNode)19 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)19 List (java.util.List)17 Type (org.objectweb.asm.Type)17