<commit_before>package ru.vyarus.gradle.plugin.animalsniffer.report

import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import org.slf4j.Logger
import ru.vyarus.gradle.plugin.animalsniffer.util.FormatUtils

import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method

/**
 * Intercept ant task output and re-format it.
 * Writes result to console and report file.
 * In essence, this is {@link org.apache.tools.ant.BuildListener} proxy implementation.
 *
 * @author <NAME>
 * @since 16.12.2015
 */
@CompileStatic
class ReportCollector implements InvocationHandler {

    // it should be org.gradle.api.internal.project.ant.AntLoggingAdapter
    Object originalListener

    List<ReportMessage> report = []
    Set<String> affectedFiles = []
    Set<File> roots

    ReportCollector(Set<File> roots) {
        this.roots = roots
    }

    /**
     * Used for ant's BuildListener to intercept logged messages.
     *
     * @param proxy proxy
     * @param method method
     * @param args args
     * @return null
     * @throws Throwable on errors
     */
    @Override
    @CompileStatic(TypeCheckingMode.SKIP)
    Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object event = args[0] // org.apache.tools.ant.BuildEvent
        // it should not be possible that other ant task will use this listener, but
        // such case was reported (#3). Use ant task name to filter output
        if (event.task?.taskName != 'animalsniffer') {
            if (originalListener) {
                // redirect to original listener
                method.invoke(originalListener, args)
            }
            return null
        }
        if (method.name == 'messageLogged' && event.priority < 2) {
            ReportMessage msg = FormatUtils.parse(event.message, roots)
            affectedFiles.add(msg.source)
            report.add(msg)
        }
        return null
    }

    /**
     * @return detected errors count
     */
    int errorsCnt() {
        report.size()
    }

    /**<commit_msg>bypass not intercepted animalsniffer messages (debug logs)<commit_after>package ru.vyarus.gradle.plugin.animalsniffer.report

import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import org.slf4j.Logger
import ru.vyarus.gradle.plugin.animalsniffer.util.FormatUtils

import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method

/**
 * Intercept ant task output and re-format it.
 * Writes result to console and report file.
 * In essence, this is {@link org.apache.tools.ant.BuildListener} proxy implementation.
 *
 * @author <NAME>
 * @since 16.12.2015
 */
@CompileStatic
class ReportCollector implements InvocationHandler {

    // it should be org.gradle.api.internal.project.ant.AntLoggingAdapter
    Object originalListener

    List<ReportMessage> report = []
    Set<String> affectedFiles = []
    Set<File> roots

    ReportCollector(Set<File> roots) {
        this.roots = roots
    }

    /**
     * Used for ant's BuildListener to intercept logged messages.
     *
     * @param proxy proxy
     * @param method method
     * @param args args
     * @return null
     * @throws Throwable on errors
     */
    @Override
    @CompileStatic(TypeCheckingMode.SKIP)
    Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object event = args[0] // org.apache.tools.ant.BuildEvent
        // it should not be possible that other ant task will use this listener, but
        // such case was reported (#3). Use ant task name to filter output
        if (event.task?.taskName != 'animalsniffer') {
            if (originalListener) {
                // redirect to original listener
                method.invoke(originalListener, args)
            }
            return null
        }
        if (method.name == 'messageLogged' && event.priority < 2) {
            ReportMessage msg = FormatUtils.parse(event.message, roots)
            affectedFiles.add(msg.source)
            report.add(msg)
        } else {
            if (originalListener) {
                // redirect to original listener
                method.invoke(originalListener, args)
            }
        }
        return null
    }

    /**
     * @return detected errors count
     */
    int errorsCnt() {
        report.size()
    }

    /**
{'fraction_non_alphanumeric': 0.09438717067583047, 'fraction_numerical': 0.005498281786941581, 'mean_word_length': 2.4109375, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '6820103', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1164, 'n_words': 331}