From 84e0dfef810808e5adc69608e21f6c69ed0873a3 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Fri, 20 May 2022 17:05:09 -0400 Subject: [PATCH] Use reflection for KotlinCompile.source() invocations This allows it to safely support Kotlin 1.7.x, where the signature of this function changed the return type. Resolves #2271 --- .../com/squareup/wire/gradle/WirePlugin.kt | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/wire-library/wire-gradle-plugin/src/main/kotlin/com/squareup/wire/gradle/WirePlugin.kt b/wire-library/wire-gradle-plugin/src/main/kotlin/com/squareup/wire/gradle/WirePlugin.kt index b3bb893413..c71e852ade 100644 --- a/wire-library/wire-gradle-plugin/src/main/kotlin/com/squareup/wire/gradle/WirePlugin.kt +++ b/wire-library/wire-gradle-plugin/src/main/kotlin/com/squareup/wire/gradle/WirePlugin.kt @@ -32,6 +32,7 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSet import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import java.io.File +import java.lang.reflect.Array as JavaArray import java.util.concurrent.atomic.AtomicBoolean class WirePlugin : Plugin { @@ -159,17 +160,22 @@ class WirePlugin : Plugin { // Even though we add the Wire output directories into the corresponding sourceSets, the // compilation tasks won't know about them so we fix that here. if (hasJavaOutput) { - project.tasks.matching { it.name == "compileJava" }.configureEach { - (it as JavaCompile).source(generatedSourcesDirectories) - } + project.tasks + .withType(JavaCompile::class.java) + .matching { it.name == "compileJava" } + .configureEach { + it.source(generatedSourcesDirectories) + } } if (hasJavaOutput || hasKotlinOutput) { - project.tasks.matching { - it.name == "compileKotlin" || it.name == "compile${source.name.capitalize()}Kotlin" - }.configureEach { - // Note that [KotlinCompile.source] will process files but will ignore strings. - (it as KotlinCompile).source(generatedSourcesDirectories) - } + project.tasks + .withType(KotlinCompile::class.java) + .matching { + it.name == "compileKotlin" || it.name == "compile${source.name.capitalize()}Kotlin" + }.configureEach { + // Note that [KotlinCompile.source] will process files but will ignore strings. + SOURCE_FUNCTION.invoke(it, arrayOf(generatedSourcesDirectories)) + } } // TODO: pair up generatedSourceDirectories with their targets so we can be precise. @@ -276,5 +282,11 @@ class WirePlugin : Plugin { internal companion object { const val PARENT_TASK = "generateProtos" const val GROUP = "wire" + // The signature of this function changed in Kotlin 1.7, so we invoke it reflectively + // to support both. + // 1.6.x: `fun source(vararg sources: Any): SourceTask + // 1.7.x: `fun source(vararg sources: Any) + private val SOURCE_FUNCTION = KotlinCompile::class.java + .getMethod("source", JavaArray.newInstance(Any::class.java, 0).javaClass) } }