Search in sources :

Example 46 with ClassNode

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

the class CallGraphGenerator method analyze.

public static CallGraph analyze(String className) {
    ClassNode targetClass = DependencyAnalysis.getClassNode(className);
    CallGraph callgraph = new CallGraph(className);
    if (targetClass != null)
        handle(callgraph, targetClass, 0);
    if (Properties.INSTRUMENT_PARENT) {
        handleSuperClasses(callgraph, targetClass);
    }
    return callgraph;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode)

Example 47 with ClassNode

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

the class CarvingClassLoader method instrumentClass.

private Class<?> instrumentClass(String fullyQualifiedTargetClass) throws ClassNotFoundException {
    logger.warn("Instrumenting class '" + fullyQualifiedTargetClass + "'.");
    try {
        String className = fullyQualifiedTargetClass.replace('.', '/');
        InputStream is = ResourceList.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getClassAsStream(className);
        if (is == null) {
            throw new ClassNotFoundException("Class '" + className + ".class" + "' should be in target project, but could not be found!");
        }
        ClassReader reader = new ClassReader(is);
        ClassNode classNode = new ClassNode();
        reader.accept(classNode, ClassReader.SKIP_FRAMES);
        instrumenter.transformClassNode(classNode, className);
        ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        classNode.accept(new JSRInlinerClassVisitor(writer));
        // classNode.accept(writer);
        byte[] byteBuffer = writer.toByteArray();
        Class<?> result = defineClass(fullyQualifiedTargetClass, byteBuffer, 0, byteBuffer.length);
        if (Modifier.isPrivate(result.getModifiers())) {
            logger.info("REPLACING PRIVATE CLASS " + fullyQualifiedTargetClass);
            result = super.loadClass(fullyQualifiedTargetClass);
        }
        classes.put(fullyQualifiedTargetClass, result);
        logger.info("Keeping class: " + fullyQualifiedTargetClass);
        return result;
    } catch (Throwable t) {
        logger.info("Error: " + t);
        for (StackTraceElement e : t.getStackTrace()) {
            logger.info(e.toString());
        }
        throw new ClassNotFoundException(t.getMessage(), t);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) JSRInlinerClassVisitor(org.evosuite.runtime.instrumentation.JSRInlinerClassVisitor) InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Example 48 with ClassNode

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

the class ProjectGraph method isAbstract.

/**
 * Is the given class name representing an abstract class in the SUT?
 * @param className
 * @return
 * @throws IllegalArgumentException
 */
public boolean isAbstract(String className) throws IllegalArgumentException {
    checkClass(className);
    ClassNode node = getClassNode(className);
    return (node.access & Opcodes.ACC_ABSTRACT) == Opcodes.ACC_ABSTRACT;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode)

Example 49 with ClassNode

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

the class ProjectGraph method isInterface.

/**
 * Is the given class name representing an interface in the SUT?
 * @param className
 * @return
 * @throws IllegalArgumentException
 */
public boolean isInterface(String className) throws IllegalArgumentException {
    checkClass(className);
    ClassNode node = getClassNode(className);
    if ((node.access & Opcodes.ACC_INTERFACE) == Opcodes.ACC_INTERFACE)
        return true;
    return false;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode)

Example 50 with ClassNode

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

the class DetermineSUT method determineCalledClasses.

public Set<String> determineCalledClasses(String fullyQualifiedTargetClass, Set<String> targetClasses) throws ClassNotFoundException, NoJUnitClassException {
    Set<String> calledClasses = new HashSet<String>();
    String className = fullyQualifiedTargetClass.replace('.', '/');
    try {
        InputStream is = ClassLoader.getSystemResourceAsStream(className + ".class");
        if (is == null) {
            throw new ClassNotFoundException("Class '" + className + ".class" + "' should be in target project, but could not be found!");
        }
        ClassReader reader = new ClassReader(is);
        ClassNode classNode = new ClassNode();
        reader.accept(classNode, ClassReader.SKIP_FRAMES);
        superClasses = getSuperClasses(classNode);
        if (isJUnitTest(classNode)) {
            handleClassNode(calledClasses, classNode, targetClasses);
        } else {
            throw new NoJUnitClassException();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    calledClasses.remove("java.lang.Object");
    calledClasses.remove(fullyQualifiedTargetClass);
    return calledClasses;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) InputStream(java.io.InputStream) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException) HashSet(java.util.HashSet)

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