Search in sources :

Example 36 with MethodNode

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

the class GetStaticGraphGenerator method handle.

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

Example 37 with MethodNode

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

the class InheritanceTreeGenerator method analyzeClassNode.

@SuppressWarnings("unchecked")
private static void analyzeClassNode(InheritanceTree inheritanceTree, ClassNode cn, boolean onlyPublic) {
    logger.info("Analyzing class {}", cn.name);
    // Don't load classes already seen from a different CP entry
    if (inheritanceTree.hasClass(cn.name))
        return;
    if ((Opcodes.ACC_INTERFACE & cn.access) != Opcodes.ACC_INTERFACE) {
        for (Object m : cn.methods) {
            MethodNode mn = (MethodNode) m;
            inheritanceTree.addAnalyzedMethod(cn.name, mn.name, mn.desc);
        }
        if ((Opcodes.ACC_ABSTRACT & cn.access) == Opcodes.ACC_ABSTRACT) {
            inheritanceTree.registerAbstractClass(cn.name);
        }
    } else {
        inheritanceTree.registerInterface(cn.name);
    }
    if (onlyPublic) {
        if ((cn.access & Opcodes.ACC_PUBLIC) == 0) {
            return;
        }
    // } else {
    // if (!canUse(cn)) {
    // logger.info("Cannot use "+cn.name);
    // return;
    // }
    }
    if (cn.superName != null)
        inheritanceTree.addSuperclass(cn.name, cn.superName, cn.access);
    List<String> interfaces = cn.interfaces;
    for (String interfaceName : interfaces) {
        inheritanceTree.addInterface(cn.name, interfaceName);
    }
}
Also used : MethodNode(org.objectweb.asm.tree.MethodNode)

Example 38 with MethodNode

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

the class PutStaticMethodCollector method collectMethods.

@SuppressWarnings("unchecked")
public Set<MethodIdentifier> collectMethods() {
    Set<MethodIdentifier> methods = new LinkedHashSet<MethodIdentifier>();
    for (String calledClassName : getStaticFields.keySet()) {
        ClassNode classNode = DependencyAnalysis.getClassNode(calledClassName);
        List<MethodNode> classMethods = classNode.methods;
        for (MethodNode mn : classMethods) {
            if (mn.name.equals(CLINIT))
                continue;
            InsnList instructions = mn.instructions;
            Iterator<AbstractInsnNode> it = instructions.iterator();
            while (it.hasNext()) {
                AbstractInsnNode insn = it.next();
                if (insn instanceof FieldInsnNode) {
                    FieldInsnNode fieldInsn = (FieldInsnNode) insn;
                    if (fieldInsn.getOpcode() != Opcodes.PUTSTATIC) {
                        continue;
                    }
                    String calleeClassName = fieldInsn.owner.replaceAll("/", ".");
                    String calleeFieldName = fieldInsn.name;
                    if (contains(getStaticFields, calleeClassName, calleeFieldName)) {
                        MethodIdentifier methodIdentifier = new MethodIdentifier(calledClassName, mn.name, mn.desc);
                        methods.add(methodIdentifier);
                    }
                }
            }
        }
    }
    return methods;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 39 with MethodNode

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

the class CallGraphGenerator method handle.

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

Example 40 with MethodNode

use of org.objectweb.asm.tree.MethodNode 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)

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