<commit_before>package com.netflix.nebula.lint.rule.dependency

import com.netflix.nebula.interop.GradleKt
import groovy.transform.Memoized
import groovyx.gpars.GParsPool
import org.gradle.api.Project
import org.gradle.api.artifacts.*
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.artifacts.DefaultModuleIdentifier
import org.gradle.api.internal.artifacts.DefaultModuleVersionIdentifier
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.SourceSet
import org.gradle.util.VersionNumber
import org.objectweb.asm.ClassReader
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes
import java.util.jar.JarEntry
import java.util.jar.JarFile
import java.util.zip.ZipException

class DependencyService {
    private static final String EXTENSION_NAME = "gradleLintDependencyService"
    private static final Collection<String> DEFAULT_METHOD_REFERENCE_IGNORED_PACKAGES = ["java/lang", "java/util", "java/net", "java/io", "java/nio", "java/swing"] //ignore java packages by default for method references

    static synchronized DependencyService forProject(Project project) {
        def extension = project.extensions.findByType(DependencyServiceExtension)
        if (!extension) {
            extension = project.extensions.create(EXTENSION_NAME, DependencyServiceExtension)
        }
        if (extension.dependencyService == null) {
            extension.dependencyService = new DependencyService(project)
        }
        return extension.dependencyService
    }

    static synchronized void removeForProject(Project project) {
        def extension = project.extensions.findByType(DependencyServiceExtension)
        if (extension) {
            extension.dependencyService = null
        }
    }

    static class DependencyServiceExtension {
        DependencyService dependencyService
    }

    private final Logger logger = LoggerFactory.getLogger(DependencyService)

    Project project

    private DependencyService(Project project) {
        this.project = project
    }

    Map<String, Collection<ResolvedArtifact>> artifactsByClass(String conf) {
        artifactsByClass(project.configurations.getByName(conf))
    }

    /**
     * @return a map of fully qualified class names to the resolved artifacts that contain them
     */
    @Memoized
    Map<String, Collection<ResolvedArtifact>> artifactsByClass(Configuration conf) {
        // we go up the configuration hierarchy here, because we want to identify dependencies that were defined in
        // higher configurations but belong here (e.g. defined in runtime, but in use in compile)
        List<Configuration> terminalConfs = []
        def extendingConfs
        extendingConfs = { Configuration c ->
            def subconfs = resolvableConfigurations().findAll { it.extendsFrom.any { it == c } }
            for (Configuration subconf in subconfs)
                extendingConfs(subconf)
            if (subconfs.isEmpty()) {
                terminalConfs.add(c)
            }
        }
        extendingConfs(conf)

        def artifactsByClass = [:].withDefault { [] }
        def artifactsToScan = terminalConfs.collect { Configuration terminalConf ->
            if(isResolvable(terminalConf)) {
               return terminalConf.resolvedConfiguration.resolvedArtifacts
            } else {
                Configuration parent = terminalConf.extendsFrom.find { isResolvable(it) }
                return findAndReplaceDeprecatedConfiguration(parent).resolvedConfiguration.resolvedArtifacts
            }
        }.flatten()

        GParsPool.withPool {
            artifactsToScan
                    .findAll {
                it.file.name.endsWith('.jar') &&
                        !it.file.name.endsWith('-sources.jar') &&
                        !it.file.name.endsWith('-javadoc.jar')
            }
            .eachParallel { ResolvedArtifact artifact ->
                jarContents(artifact.file).classes.each { clazz ->
                    synchronized (artifactsByClass) {
                        artifactsByClass[clazz] += artifact
                    }
                }
            }
        }

        return artifactsByClass
    }

    Configuration findResolvableConfiguration(String confName) {
        return findAndReplaceDeprecatedConfiguration(getResolvableConfigurationOrParent(confName))
    }

    /**
     * Replaces compile configuration for compileClasspath so we find proper usage of dependencies
     * @param configuration
     * @return
     */
    private Configuration findAndReplaceDeprecatedConfiguration(Configuration configuration) {
        if(configuration.name == 'compile') {
            return project.configurations.findByName('compileClasspath')
        } else if(configuration.name.endsWith('Compile') && configuration.name != 'providedCompile') {
            return project.configurations.findByName(configuration.name + 'Classpath')
        } else {
            return configuration
        }<commit_msg>Make sure we can handle modern configurations without breaking compile that don't have compileClasspath<commit_after>package com.netflix.nebula.lint.rule.dependency

import com.netflix.nebula.interop.GradleKt
import groovy.transform.Memoized
import groovyx.gpars.GParsPool
import org.gradle.api.Project
import org.gradle.api.artifacts.*
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.artifacts.DefaultModuleIdentifier
import org.gradle.api.internal.artifacts.DefaultModuleVersionIdentifier
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.SourceSet
import org.gradle.util.VersionNumber
import org.objectweb.asm.ClassReader
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes
import java.util.jar.JarEntry
import java.util.jar.JarFile
import java.util.zip.ZipException

class DependencyService {
    private static final String EXTENSION_NAME = "gradleLintDependencyService"
    private static final Collection<String> DEFAULT_METHOD_REFERENCE_IGNORED_PACKAGES = ["java/lang", "java/util", "java/net", "java/io", "java/nio", "java/swing"] //ignore java packages by default for method references

    static synchronized DependencyService forProject(Project project) {
        def extension = project.extensions.findByType(DependencyServiceExtension)
        if (!extension) {
            extension = project.extensions.create(EXTENSION_NAME, DependencyServiceExtension)
        }
        if (extension.dependencyService == null) {
            extension.dependencyService = new DependencyService(project)
        }
        return extension.dependencyService
    }

    static synchronized void removeForProject(Project project) {
        def extension = project.extensions.findByType(DependencyServiceExtension)
        if (extension) {
            extension.dependencyService = null
        }
    }

    static class DependencyServiceExtension {
        DependencyService dependencyService
    }

    private final Logger logger = LoggerFactory.getLogger(DependencyService)

    Project project

    private DependencyService(Project project) {
        this.project = project
    }

    Map<String, Collection<ResolvedArtifact>> artifactsByClass(String conf) {
        artifactsByClass(project.configurations.getByName(conf))
    }

    /**
     * @return a map of fully qualified class names to the resolved artifacts that contain them
     */
    @Memoized
    Map<String, Collection<ResolvedArtifact>> artifactsByClass(Configuration conf) {
        // we go up the configuration hierarchy here, because we want to identify dependencies that were defined in
        // higher configurations but belong here (e.g. defined in runtime, but in use in compile)
        List<Configuration> terminalConfs = []
        def extendingConfs
        extendingConfs = { Configuration c ->
            def subconfs = resolvableConfigurations().findAll { it.extendsFrom.any { it == c } }
            for (Configuration subconf in subconfs)
                extendingConfs(subconf)
            if (subconfs.isEmpty()) {
                terminalConfs.add(c)
            }
        }
        extendingConfs(conf)

        def artifactsByClass = [:].withDefault { [] }
        def artifactsToScan = terminalConfs.collect { Configuration terminalConf ->
            if(isResolvable(terminalConf)) {
               return terminalConf.resolvedConfiguration.resolvedArtifacts
            } else {
                Configuration parent = terminalConf.extendsFrom.find { isResolvable(it) }
                return findAndReplaceDeprecatedConfiguration(parent).resolvedConfiguration.resolvedArtifacts
            }
        }.flatten()

        GParsPool.withPool {
            artifactsToScan
                    .findAll {
                it.file.name.endsWith('.jar') &&
                        !it.file.name.endsWith('-sources.jar') &&
                        !it.file.name.endsWith('-javadoc.jar')
            }
            .eachParallel { ResolvedArtifact artifact ->
                jarContents(artifact.file).classes.each { clazz ->
                    synchronized (artifactsByClass) {
                        artifactsByClass[clazz] += artifact
                    }
                }
            }
        }

        return artifactsByClass
    }

    Configuration findResolvableConfiguration(String confName) {
        return findAndReplaceDeprecatedConfiguration(getResolvableConfigurationOrParent(confName))
    }

    /**
     * Replaces compile configuration for compileClasspath so we find proper usage of dependencies
     * @param configuration
     * @return
     */
    private Configuration findAndReplaceDeprecatedConfiguration(Configuration configuration) {
        if(configuration.name == 'compile') {
            return project.configurations.findByName('compileClasspath')
        } else if(configuration.name.endsWith('Compile') && configuration.name != 'providedCompile') {
            Configuration modernConfiguration = project.configurations.findByName(configuration.name + 'Classpath')
            if(!modernConfiguration) {
                return configuration
            }
            return project.configurations.findByName(configuration.name + 'Classpath')
        } else {
            return configuration
        }
{'fraction_non_alphanumeric': 0.07960526315789473, 'fraction_numerical': 0.00037593984962406017, 'mean_word_length': 2.792230933713471, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, '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': '1894144', 'n_tokens_mistral': 2715, 'n_tokens_neox': 2579, 'n_words': 572}