Skip to content

Commit

Permalink
Handle edge cases of KtAnnotated.annotations
Browse files Browse the repository at this point in the history
where candidates can be empty.

(cherry picked from commit de0ed97)
  • Loading branch information
ting-yuan authored and KSP Auto Pick committed Jul 25, 2024
1 parent 27653c6 commit b683dee
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ internal fun KtAnnotated.annotations(
parent: KSNode? = null,
candidates: List<KaAnnotation> = kaAnnotated.annotations
): Sequence<KSAnnotation> {
if (candidates.isEmpty())
return emptySequence()
return annotationEntries.filter { !it.isUseSiteTargetAnnotation() }.asSequence().map { annotationEntry ->
KSAnnotationImpl.getCached(annotationEntry, parent) {
candidates.single { it.psi == annotationEntry }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ fun Fun() {
class Local
}

data class DataClass

// FILE: JavaEnum.java

enum JavaEnum { ONE, TWO, THREE }
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>()
Expand All @@ -37,6 +34,14 @@ class AnnotationArgumentProcessor : AbstractTestProcessor() {
}
}

resolver.getClassDeclarationByName("DataClass")?.let { cls ->
cls.declarations.filterIsInstance<KSFunctionDeclaration>().single {
it.simpleName.asString() == "copy"
}.annotations.forEach {
it.arguments
}
}

resolver.getSymbolsWithAnnotation("Bar", true).forEach {
it.annotations.forEach { it.arguments.forEach { it.accept(visitor, Unit) } }
}
Expand Down

0 comments on commit b683dee

Please sign in to comment.