Search in sources :

Example 1 with JSRInlinerClassVisitor

use of org.evosuite.runtime.instrumentation.JSRInlinerClassVisitor 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)

Aggregations

InputStream (java.io.InputStream)1 JSRInlinerClassVisitor (org.evosuite.runtime.instrumentation.JSRInlinerClassVisitor)1 ClassReader (org.objectweb.asm.ClassReader)1 ClassWriter (org.objectweb.asm.ClassWriter)1 ClassNode (org.objectweb.asm.tree.ClassNode)1