Search in sources :

Example 51 with ClassNode

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;
}
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 52 with ClassNode

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;
}
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 53 with ClassNode

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

Example 54 with ClassNode

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;
}
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 55 with ClassNode

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

ClassNode (org.objectweb.asm.tree.ClassNode)323 ClassReader (org.objectweb.asm.ClassReader)148 MethodNode (org.objectweb.asm.tree.MethodNode)120 ClassWriter (org.objectweb.asm.ClassWriter)70 IOException (java.io.IOException)44 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)37 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)34 InsnList (org.objectweb.asm.tree.InsnList)29 FieldNode (org.objectweb.asm.tree.FieldNode)28 ArrayList (java.util.ArrayList)27 Test (org.junit.Test)26 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)26 Label (org.objectweb.asm.Label)21 InputStream (java.io.InputStream)20 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)18 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)18 InnerClassNode (org.objectweb.asm.tree.InnerClassNode)17 InsnNode (org.objectweb.asm.tree.InsnNode)17 Method (java.lang.reflect.Method)16 List (java.util.List)16