From 73b18ba2cdc5144c87d5757423d78b834051f8f8 Mon Sep 17 00:00:00 2001 From: Ralf Wondratschek Date: Fri, 15 Nov 2024 10:36:27 -0800 Subject: [PATCH] Copy required annotations to generated code Copy all annotations from `expect fun` functions annotated with `@CreateComponent` to the generated `actual fun` functions to avoid a Kotlin compiler warning. Fixes #72 --- .../kotlin/inject/anvil/gradle/KotlinPlugin.kt | 10 +++++++--- .../anvil/processor/CreateComponentProcessor.kt | 13 +++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/build-logic/src/main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/gradle/KotlinPlugin.kt b/build-logic/src/main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/gradle/KotlinPlugin.kt index e99e475..3870c2f 100644 --- a/build-logic/src/main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/gradle/KotlinPlugin.kt +++ b/build-logic/src/main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/gradle/KotlinPlugin.kt @@ -12,6 +12,7 @@ import org.gradle.api.tasks.SourceTask import org.gradle.api.tasks.compile.JavaCompile import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension +import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile import org.jlleitschuh.gradle.ktlint.KtlintExtension import java.io.File @@ -33,9 +34,12 @@ internal open class KotlinPlugin : Plugin { } private fun Project.configureKotlinCompile() { - tasks.withType(KotlinJvmCompile::class.java).configureEach { - it.compilerOptions.jvmTarget.set(JvmTarget.fromTarget(javaVersion.toString())) - it.compilerOptions.allWarningsAsErrors.set(ci) + tasks.withType(KotlinCompilationTask::class.java).configureEach { + // TODO: ci + it.compilerOptions.allWarningsAsErrors.set(true) + if (it is KotlinJvmCompile) { + it.compilerOptions.jvmTarget.set(JvmTarget.fromTarget(javaVersion.toString())) + } } } diff --git a/compiler/src/main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/processor/CreateComponentProcessor.kt b/compiler/src/main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/processor/CreateComponentProcessor.kt index 047e3ec..e22a77a 100644 --- a/compiler/src/main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/processor/CreateComponentProcessor.kt +++ b/compiler/src/main/kotlin/software/amazon/lastmile/kotlin/inject/anvil/processor/CreateComponentProcessor.kt @@ -19,6 +19,7 @@ import com.squareup.kotlinpoet.KModifier.ACTUAL import com.squareup.kotlinpoet.ParameterSpec import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.asTypeName +import com.squareup.kotlinpoet.ksp.toAnnotationSpec import com.squareup.kotlinpoet.ksp.toClassName import com.squareup.kotlinpoet.ksp.toTypeName import com.squareup.kotlinpoet.ksp.writeTo @@ -61,6 +62,15 @@ internal class CreateComponentProcessor( resolver .getSymbolsWithAnnotation(MergeComponent.CreateComponent::class) .filterIsInstance() + .filter { + // We copy all annotations from the expect function, including @CreateComponent, + // to the actual functions. Without doing so the Kotlin compiler would print a + // warning. + // + // Without this filter we'd try to process the generated actual function, which + // leads to errors. + Modifier.ACTUAL !in it.modifiers + } .onEach { function -> checkIsPublic(function) { "Factory functions for components annotated with `@CreateComponent` must be public." @@ -82,8 +92,6 @@ internal class CreateComponentProcessor( .toClassName() val generatedComponent = component.peerClass("KotlinInject${component.simpleName}") - function.requireContainingFile() - val parametersAsSpec = function.parameters.map { ParameterSpec .builder( @@ -109,6 +117,7 @@ internal class CreateComponentProcessor( ) } } + .addAnnotations(function.annotations.map { it.toAnnotationSpec() }.toList()) .addModifiers(ACTUAL) .addParameters(parametersAsSpec) .returns(component)