Add test for getting default annotation values in KMP (cherry picked from commit a99862e9f152ee544e4a4236a9c9d81e5746c833)
diff --git a/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/processor/MultiplatformAnnotationDefaultValueProcessor.kt b/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/processor/MultiplatformAnnotationDefaultValueProcessor.kt new file mode 100644 index 0000000..a1a7ba9 --- /dev/null +++ b/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/processor/MultiplatformAnnotationDefaultValueProcessor.kt
@@ -0,0 +1,56 @@ +/* + * Copyright 2026 Google LLC + * Copyright 2010-2026 JetBrains s.r.o. and Kotlin Programming Language contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.devtools.ksp.processor + +import com.google.devtools.ksp.getClassDeclarationByName +import com.google.devtools.ksp.processing.Resolver +import com.google.devtools.ksp.symbol.KSAnnotated +import com.google.devtools.ksp.symbol.KSAnnotation +import com.google.devtools.ksp.symbol.KSDeclaration +import com.google.devtools.ksp.symbol.KSValueArgument + +class MultiplatformAnnotationDefaultValueProcessor( + val annotationNames: List<String>, + override val enableNewFeatures: Boolean, +) : AbstractTestProcessor() { + private val result = mutableListOf<String>() + + override fun toResult(): List<String> = result + + override fun process(resolver: Resolver): List<KSAnnotated> { + annotationNames.forEach { annotationName -> + resolver.getSymbolsWithAnnotation(annotationName).forEach { annotated -> + annotated.annotations.forEach { annotation -> + val defaultArg = annotation.defaultArguments.single() + val arg = annotation.arguments.single() + result.add("Default argument: ${renderAnnotation(annotated, annotation)}: ${renderArg(defaultArg)}") + result.add("Call-site argument: ${renderAnnotation(annotated, annotation)}: ${renderArg(arg)}") + } + } + } + return emptyList() + } + + private fun renderAnnotation(annotated: KSAnnotated, annotation: KSAnnotation): String = when (annotated) { + is KSDeclaration -> "@${annotation.shortName.asString()} ${annotated.simpleName.asString()}" + else -> error("$annotated is not a KSDeclaration") + } + + private fun renderArg(argument: KSValueArgument): String = + "${argument.name?.asString()} = ${argument.value}" +}
diff --git a/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/test/KSPUnitTestSuite.kt b/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/test/KSPUnitTestSuite.kt index 5556b9f..0689c87 100644 --- a/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/test/KSPUnitTestSuite.kt +++ b/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/test/KSPUnitTestSuite.kt
@@ -1039,6 +1039,13 @@ runTest("$AA_PATH/native/packageDeclarationsMultiRound.kt") } + @Bug("https://github.com/google/ksp/issues/2356", BugState.OPEN) + @TestMetadata("native/annotationDefaultValue.kt") + @Test + fun testNativeAnnotationDefaultValue() { + runThrowingTest("$AA_PATH/native/annotationDefaultValue.kt", java.util.NoSuchElementException::class) + } + @Bug( "https://github.com/google/ksp/issues/2396", BugState.FIXED,
diff --git a/kotlin-analysis-api/testData/native/annotationDefaultValue.kt b/kotlin-analysis-api/testData/native/annotationDefaultValue.kt new file mode 100644 index 0000000..dff5edc --- /dev/null +++ b/kotlin-analysis-api/testData/native/annotationDefaultValue.kt
@@ -0,0 +1,39 @@ +/* + * Copyright 2026 Google LLC + * Copyright 2010-2026 JetBrains s.r.o. and Kotlin Programming Language contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// TARGET_BACKEND: NATIVE +// WITH_FIXED_TARGET: linux_x64 +// TEST PROCESSOR: MultiplatformAnnotationDefaultValueProcessor +// PROCESSOR INPUT: MyAnnotation +// EXPECTED: +// Default argument: @MyAnnotation Foo: withDefaultValue = the default value +// Call-site argument: @MyAnnotation Foo: withDefaultValue = the default value +// Default argument: @MyAnnotation Bar: withDefaultValue = the default value +// Call-site argument: @MyAnnotation Bar: withDefaultValue = some other value +// END + +// MODULE: lib +// FILE: Lib.kt +annotation class MyAnnotation(val withDefaultValue: String = "the default value") + +// MODULE: main(lib) +// FILE: Main.kt +@MyAnnotation +class Foo + +@MyAnnotation("some other value") +class Bar