diff --git a/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/internal/ClassLoaderCache.kt b/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/internal/ClassLoaderCache.kt deleted file mode 100644 index 7f54e005bdf3..000000000000 --- a/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/internal/ClassLoaderCache.kt +++ /dev/null @@ -1,28 +0,0 @@ -package io.gitlab.arturbosch.detekt.internal - -import org.gradle.api.file.FileCollection -import java.net.URLClassLoader -import java.util.concurrent.ConcurrentHashMap - -fun interface ClassLoaderCache { - - fun getOrCreate(classpath: FileCollection): URLClassLoader -} - -internal class DefaultClassLoaderCache : ClassLoaderCache { - - private val classpathFilesHashWithLoaders = ConcurrentHashMap() - - override fun getOrCreate(classpath: FileCollection): URLClassLoader { - val classpathFiles = classpath.files - val classpathHashCode = HashSet(classpathFiles).hashCode() - return classpathFilesHashWithLoaders.getOrPut(classpathHashCode) { - URLClassLoader( - classpathFiles.map { it.toURI().toURL() }.toTypedArray(), - null /* isolate detekt environment */ - ) - } - } -} - -object GlobalClassLoaderCache : ClassLoaderCache by DefaultClassLoaderCache() diff --git a/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/invoke/DetektInvoker.kt b/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/invoke/DetektInvoker.kt index 6cbc8fc3a239..6af574a176c3 100644 --- a/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/invoke/DetektInvoker.kt +++ b/detekt-gradle-plugin/src/main/kotlin/io/gitlab/arturbosch/detekt/invoke/DetektInvoker.kt @@ -1,7 +1,5 @@ package io.gitlab.arturbosch.detekt.invoke -import io.gitlab.arturbosch.detekt.internal.ClassLoaderCache -import io.gitlab.arturbosch.detekt.internal.GlobalClassLoaderCache import org.gradle.api.GradleException import org.gradle.api.Project import org.gradle.api.file.ConfigurableFileCollection @@ -10,8 +8,6 @@ import org.gradle.api.provider.ListProperty import org.gradle.api.provider.Property import org.gradle.workers.WorkAction import org.gradle.workers.WorkParameters -import java.io.PrintStream -import java.lang.reflect.InvocationTargetException internal interface DetektInvoker { @@ -21,16 +17,6 @@ internal interface DetektInvoker { taskName: String, ignoreFailures: Boolean = false ) - - companion object { - - fun create(isDryRun: Boolean): DetektInvoker = - if (isDryRun) { - DryRunInvoker() - } else { - DefaultCliInvoker() - } - } } private const val DRY_RUN_PROPERTY = "detekt-dry-run" @@ -80,39 +66,6 @@ abstract class DetektWorkAction : WorkAction { msg != null && "Build failed with" in msg && "issues" in msg } -internal class DefaultCliInvoker( - private val classLoaderCache: ClassLoaderCache = GlobalClassLoaderCache -) : DetektInvoker { - - override fun invokeCli( - arguments: List, - classpath: FileCollection, - taskName: String, - ignoreFailures: Boolean - ) { - try { - val loader = classLoaderCache.getOrCreate(classpath) - val clazz = loader.loadClass("io.gitlab.arturbosch.detekt.cli.Main") - val runner = clazz.getMethod( - "buildRunner", - Array::class.java, - PrintStream::class.java, - PrintStream::class.java - ).invoke(null, arguments.toTypedArray(), System.out, System.err) - runner::class.java.getMethod("execute").invoke(runner) - } catch (reflectionWrapper: InvocationTargetException) { - val message = reflectionWrapper.targetException.message - if (message != null && isBuildFailure(message) && ignoreFailures) { - return - } - throw GradleException(message ?: "There was a problem running detekt.", reflectionWrapper) - } - } - - private fun isBuildFailure(msg: String?) = - msg != null && "Build failed with" in msg && "issues" in msg -} - private class DryRunInvoker : DetektInvoker { override fun invokeCli( diff --git a/detekt-gradle-plugin/src/test/kotlin/io/gitlab/arturbosch/detekt/internal/ClassLoaderCacheSpec.kt b/detekt-gradle-plugin/src/test/kotlin/io/gitlab/arturbosch/detekt/internal/ClassLoaderCacheSpec.kt deleted file mode 100644 index d153d8075c24..000000000000 --- a/detekt-gradle-plugin/src/test/kotlin/io/gitlab/arturbosch/detekt/internal/ClassLoaderCacheSpec.kt +++ /dev/null @@ -1,87 +0,0 @@ -package io.gitlab.arturbosch.detekt.internal - -import io.gitlab.arturbosch.detekt.gradle.TestFileCollection -import org.assertj.core.api.Assertions.assertThat -import org.gradle.api.internal.file.AbstractFileCollection -import org.spekframework.spek2.Spek -import org.spekframework.spek2.style.specification.describe -import java.io.File -import java.util.concurrent.CompletableFuture -import java.util.concurrent.CountDownLatch -import java.util.concurrent.Executors -import java.util.concurrent.TimeUnit -import java.util.function.Supplier - -internal class ClassLoaderCacheSpec : Spek({ - - describe("classpath changes") { - - it("same classloader is returned for the same files") { - val cache = DefaultClassLoaderCache() - val initialClassLoader = cache.getOrCreate(TestFileCollection(File("a/b/c"))) - val secondClassLoader = cache.getOrCreate(TestFileCollection(File("a/b/c"))) - - assertThat(initialClassLoader === secondClassLoader).isTrue() - } - - it("different classloaders are returned for different files") { - val cache = DefaultClassLoaderCache() - val firstClassLoader = cache.getOrCreate(TestFileCollection(File("a/b/c"))) - val secondClassLoader = cache.getOrCreate(TestFileCollection(File("c/b/a"))) - - assertThat(firstClassLoader === secondClassLoader).isFalse() - } - - it("same classloader for the same files in different order") { - val cache = DefaultClassLoaderCache() - val firstClassLoader = cache.getOrCreate(TestFileCollection(File("a/b/c"), File("d/e/f"))) - val secondClassLoader = cache.getOrCreate(TestFileCollection(File("d/e/f"), File("a/b/c"))) - - assertThat(firstClassLoader === secondClassLoader).isTrue() - } - - it("resolves files without synchronization") { - val file1 = File("/a/b/c") - val collection1 = CountdownFileCollection(file1) - - val file2 = File("/c/b/a") - val collection2 = TestFileCollection(file2) - - val cache = DefaultClassLoaderCache() - val executor = Executors.newSingleThreadExecutor() - val latch = CountDownLatch(1) - try { - val supplier = Supplier { - latch.countDown() - cache.getOrCreate(collection1) - } - val task = CompletableFuture.supplyAsync(supplier, executor) - @Suppress("UsePropertyAccessSyntax") - assertThat(latch.await(10L, TimeUnit.SECONDS)).isTrue() - // Will call `getOrCreate` next - wait a moment to be sure - Thread.sleep(2000L) - val classpath2 = cache.getOrCreate(collection2) - collection1.latch.countDown() - val classpath1 = task.join() - assertThat(classpath1.urLs).isEqualTo(arrayOf(file1.toURI().toURL())) - assertThat(classpath2.urLs).isEqualTo(arrayOf(file2.toURI().toURL())) - } finally { - val remaining = executor.shutdownNow() - assertThat(remaining).isEmpty() - } - } - } -}) - -private class CountdownFileCollection(private vararg val files: File) : AbstractFileCollection() { - - val latch = CountDownLatch(1) - - override fun getFiles(): MutableSet { - @Suppress("UsePropertyAccessSyntax") - assertThat(latch.await(10L, TimeUnit.SECONDS)).isTrue() - return files.toMutableSet() - } - - override fun getDisplayName(): String = "CountdownFileCollection" -} diff --git a/detekt-gradle-plugin/src/test/kotlin/io/gitlab/arturbosch/detekt/invoke/DefaultCliInvokerSpec.kt b/detekt-gradle-plugin/src/test/kotlin/io/gitlab/arturbosch/detekt/invoke/DefaultCliInvokerSpec.kt deleted file mode 100644 index 041904f23f19..000000000000 --- a/detekt-gradle-plugin/src/test/kotlin/io/gitlab/arturbosch/detekt/invoke/DefaultCliInvokerSpec.kt +++ /dev/null @@ -1,21 +0,0 @@ -package io.gitlab.arturbosch.detekt.invoke - -import io.gitlab.arturbosch.detekt.gradle.TestFileCollection -import io.gitlab.arturbosch.detekt.internal.ClassLoaderCache -import org.assertj.core.api.Assertions.assertThatCode -import org.gradle.api.GradleException -import org.spekframework.spek2.Spek -import java.net.URLClassLoader - -internal class DefaultCliInvokerSpec : Spek({ - - test("catches ClassCastException and fails build") { - val stubbedCache = ClassLoaderCache { URLClassLoader(emptyArray()) } - - assertThatCode { - DefaultCliInvoker(stubbedCache) - .invokeCli(emptyList(), TestFileCollection(), "detekt", ignoreFailures = false) - }.isInstanceOf(GradleException::class.java) - .hasMessageContaining("testing reflection wrapper...") - } -})