<commit_before>package weaver.plugin.task

import javassist.ClassPool
import javassist.CtClass
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import weaver.plugin.internal.processor.ProcessingEnvironmentImp
import weaver.plugin.internal.processor.ProcessorLoader
import weaver.processor.ProcessingEnvironment
import weaver.processor.WeaverProcessor

/**
 * @author <NAME> (<EMAIL>)
 */
public class TransformerTask extends DefaultTask {

    /**
     * Contains all .jar files and classes folder of source set related to this project.
     */
    private FileCollection classpath
    /**
     * A folder which .class files live there.
     */
    private File classesDir
    /**
     * Snapshot of manipulated classes.
     */
    private File outputDir

    @TaskAction
    def initTask() {
        logger.quiet("Start weaving (classes directory:$classesDir.name  output directory:$outputDir.name")
        //TODO ignore if classes dir is empty
        int time = System.currentTimeMillis()
        weaving()
        int duration = System.currentTimeMillis() - time
        logger.quiet("$name : Weaving takes $duration")
    }

    def weaving() {
        logger.debug("$name : Start weaving")
        def weaverProcessors = getProcessors()
        ProcessingEnvironment env = getProcessingEnvironment()
        //init processors
        weaverProcessors.forEach {
            it.init(env)
        }
        final ClassPool pool = createPool();
        getClasses().forEach {
            CtClass ctClass = loadClassFile(pool, it)
            weaverProcessors.forEach {
                if (it.filter(ctClass)) {
                    ctClass.defrost()
                    it.apply(ctClass)
                    ctClass.writeFile(outputDir.path)
                }
            }
        }

    }

    ProcessingEnvironment getProcessingEnvironment() {
        return new ProcessingEnvironmentImp(project)
    }

    ClassPool createPool() {
        ClassPool pool = new ClassPool(true)
        if (classpath) {
            classpath.forEach {
                pool.appendClassPath(it.toString())
            }
        }
        return pool
    }

    static CtClass loadClassFile(ClassPool pool, File classFile) throws IOException {
        InputStream stream = new DataInputStream(new BufferedInputStream(new FileInputStream(classFile)));
        CtClass clazz = pool.makeClass(stream);
        stream.close();
        return clazz;
    }

    ArrayList<WeaverProcessor> getProcessors() {
        return new ProcessorLoader(project, project.configurations.weaver.files).getProcessors()
    }

    /**
     * @return Returns all .class files from build directory.
     */
    Set<File> getClasses() {
        return project.fileTree(classesDir).matching {
            include '**/*.class'
        }.files<commit_msg>append classes directory to pool classpath<commit_after>package weaver.plugin.task

import javassist.ClassPool
import javassist.CtClass
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import weaver.plugin.internal.processor.ProcessingEnvironmentImp
import weaver.plugin.internal.processor.ProcessorLoader
import weaver.processor.ProcessingEnvironment
import weaver.processor.WeaverProcessor

/**
 * @author <NAME> (<EMAIL>)
 */
public class TransformerTask extends DefaultTask {

    /**
     * Contains all .jar files and classes folder of source set related to this project.
     */
    private FileCollection classpath
    /**
     * A folder which .class files live there.
     */
    private File classesDir
    /**
     * Snapshot of manipulated classes.
     */
    private File outputDir

    @TaskAction
    def initTask() {
        logger.quiet("Start weaving (classes directory:$classesDir.name  output directory:$outputDir.name")
        //TODO ignore if classes dir is empty
        int time = System.currentTimeMillis()
        weaving()
        int duration = System.currentTimeMillis() - time
        logger.quiet("$name : Weaving takes $duration")
    }

    def weaving() {
        logger.debug("$name : Start weaving")
        def weaverProcessors = getProcessors()
        ProcessingEnvironment env = getProcessingEnvironment()
        //init processors
        weaverProcessors.forEach {
            it.init(env)
        }
        final ClassPool pool = createPool();
        getClasses().forEach {
            CtClass ctClass = loadClassFile(pool, it)
            weaverProcessors.forEach {
                if (it.filter(ctClass)) {
                    ctClass.defrost()
                    it.apply(ctClass)
                    ctClass.writeFile(outputDir.path)
                }
            }
        }

    }

    ProcessingEnvironment getProcessingEnvironment() {
        return new ProcessingEnvironmentImp(project)
    }

    ClassPool createPool() {
        ClassPool pool = new ClassPool(true)
        if (classpath) {
            classpath.forEach {
                pool.appendClassPath(it.toString())
            }
        }
        pool.appendClassPath(classesDir.toString())
        return pool
    }

    static CtClass loadClassFile(ClassPool pool, File classFile) throws IOException {
        InputStream stream = new DataInputStream(new BufferedInputStream(new FileInputStream(classFile)));
        CtClass clazz = pool.makeClass(stream);
        stream.close();
        return clazz;
    }

    ArrayList<WeaverProcessor> getProcessors() {
        return new ProcessorLoader(project, project.configurations.weaver.files).getProcessors()
    }

    /**
     * @return Returns all .class files from build directory.
     */
    Set<File> getClasses() {
        return project.fileTree(classesDir).matching {
            include '**/*.class'
        }.files
{'fraction_non_alphanumeric': 0.08878733474783744, 'fraction_numerical': 0.0, 'mean_word_length': 2.4562887760857306, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '377066', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1545, 'n_words': 377}