From b683deeef999a0eeb2cf7c96226ad2f47a7cee73 Mon Sep 17 00:00:00 2001 From: Ting-Yuan Huang Date: Wed, 24 Jul 2024 10:45:28 -0700 Subject: [PATCH] Handle edge cases of KtAnnotated.annotations where candidates can be empty. (cherry picked from commit de0ed97de49a3f59dc38960cde4073ddfcc531a5) --- .../google/devtools/ksp/impl/symbol/kotlin/util.kt | 2 ++ .../com/google/devtools/ksp/test/KSPAATest.kt | 4 ++-- .../{java.kt => annotationValue_java.kt} | 0 .../{kotlin.kt => annotationValue_kt.kt} | 2 ++ .../ksp/processor/AnnotationArgumentProcessor.kt | 13 +++++++++---- 5 files changed, 15 insertions(+), 6 deletions(-) rename kotlin-analysis-api/testData/annotationValue/{java.kt => annotationValue_java.kt} (100%) rename kotlin-analysis-api/testData/annotationValue/{kotlin.kt => annotationValue_kt.kt} (99%) diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/util.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/util.kt index 196e1fe3e7..5ff23c2788 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/util.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/util.kt @@ -323,6 +323,8 @@ internal fun KtAnnotated.annotations( parent: KSNode? = null, candidates: List = kaAnnotated.annotations ): Sequence { + if (candidates.isEmpty()) + return emptySequence() return annotationEntries.filter { !it.isUseSiteTargetAnnotation() }.asSequence().map { annotationEntry -> KSAnnotationImpl.getCached(annotationEntry, parent) { candidates.single { it.psi == annotationEntry } diff --git a/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/test/KSPAATest.kt b/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/test/KSPAATest.kt index 962d75df61..cff122e393 100644 --- a/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/test/KSPAATest.kt +++ b/kotlin-analysis-api/src/test/kotlin/com/google/devtools/ksp/test/KSPAATest.kt @@ -97,13 +97,13 @@ class KSPAATest : AbstractKSPAATest() { @TestMetadata("annotationValue_java.kt") @Test fun testAnnotationValue_java() { - runTest("../kotlin-analysis-api/testData/annotationValue/java.kt") + runTest("../kotlin-analysis-api/testData/annotationValue/annotationValue_java.kt") } @TestMetadata("annotationValue_kt.kt") @Test fun testAnnotationValue_kt() { - runTest("../kotlin-analysis-api/testData/annotationValue/kotlin.kt") + runTest("../kotlin-analysis-api/testData/annotationValue/annotationValue_kt.kt") } @TestMetadata("annotationWithArrayValue.kt") diff --git a/kotlin-analysis-api/testData/annotationValue/java.kt b/kotlin-analysis-api/testData/annotationValue/annotationValue_java.kt similarity index 100% rename from kotlin-analysis-api/testData/annotationValue/java.kt rename to kotlin-analysis-api/testData/annotationValue/annotationValue_java.kt diff --git a/kotlin-analysis-api/testData/annotationValue/kotlin.kt b/kotlin-analysis-api/testData/annotationValue/annotationValue_kt.kt similarity index 99% rename from kotlin-analysis-api/testData/annotationValue/kotlin.kt rename to kotlin-analysis-api/testData/annotationValue/annotationValue_kt.kt index 277f593625..b06d9b8ee0 100644 --- a/kotlin-analysis-api/testData/annotationValue/kotlin.kt +++ b/kotlin-analysis-api/testData/annotationValue/annotationValue_kt.kt @@ -106,6 +106,8 @@ fun Fun() { class Local } +data class DataClass + // FILE: JavaEnum.java enum JavaEnum { ONE, TWO, THREE } diff --git a/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/AnnotationArgumentProcessor.kt b/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/AnnotationArgumentProcessor.kt index 54366407cb..fdb9ad78e6 100644 --- a/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/AnnotationArgumentProcessor.kt +++ b/test-utils/src/main/kotlin/com/google/devtools/ksp/processor/AnnotationArgumentProcessor.kt @@ -19,10 +19,7 @@ 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.KSType -import com.google.devtools.ksp.symbol.KSValueArgument -import com.google.devtools.ksp.symbol.KSVisitorVoid +import com.google.devtools.ksp.symbol.* class AnnotationArgumentProcessor : AbstractTestProcessor() { val results = mutableListOf() @@ -37,6 +34,14 @@ class AnnotationArgumentProcessor : AbstractTestProcessor() { } } + resolver.getClassDeclarationByName("DataClass")?.let { cls -> + cls.declarations.filterIsInstance().single { + it.simpleName.asString() == "copy" + }.annotations.forEach { + it.arguments + } + } + resolver.getSymbolsWithAnnotation("Bar", true).forEach { it.annotations.forEach { it.arguments.forEach { it.accept(visitor, Unit) } } }