Skip to content

Commit

Permalink
Resolve PsiAnnotation before getting its name
Browse files Browse the repository at this point in the history
(cherry picked from commit 69060b2)
  • Loading branch information
ting-yuan authored and KSP Auto Pick committed Oct 6, 2024
1 parent a42aae2 commit 3b2b3f5
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import org.jetbrains.kotlin.analysis.api.symbols.KaClassSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolOrigin
import org.jetbrains.kotlin.analysis.api.types.KaType
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName

class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation, override val parent: KSNode?) :
KSAnnotation {
Expand All @@ -56,9 +55,18 @@ class KSAnnotationJavaImpl private constructor(private val psi: PsiAnnotation, o
}

private val type: KaType by lazy {
// TODO: local class annotations?
fun PsiClass.fqn(): String? {
val parent = containingClass?.fqn()
?: return qualifiedName?.replace('.', '/')
if (name == null)
return null
return "$parent.$name"
}
analyze {
buildClassType(ClassId.topLevel(FqName(psi.qualifiedName!!)))
val resolved = psi.resolveAnnotationType()
val fqn = resolved?.fqn() ?: "__KSP_unresolved_${psi.qualifiedName}"
val classId = ClassId.fromString(fqn)
buildClassType(classId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
// WITH_RUNTIME
// TEST PROCESSOR: AnnotationArgumentProcessor
// EXPECTED:
// MyClass: MyAnnotation
// MyClass: MyAnnotation: stringParam = 2
// MyClass: MyAnnotation: stringParam2 = 1
// MyClass: MyAnnotation: stringArrayParam = [3, 5, 7]
// MyClass: MyAnnotation: Containing.Nested
// MyClass: Containing.Nested
// MyClassInLib: MyAnnotation
// MyClassInLib: MyAnnotation: stringParam = 2
// MyClassInLib: MyAnnotation: stringParam2 = 1
// MyClassInLib: MyAnnotation: stringArrayParam = [3, 5, 7]
// MyClassInLib: MyAnnotation: Containing.Nested
// MyClassInLib: Containing.Nested
// Str
// 42
// Foo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
// WITH_RUNTIME
// TEST PROCESSOR: AnnotationArgumentProcessor
// EXPECTED:
// MyClass: MyAnnotation
// MyClass: MyAnnotation: stringParam = 2
// MyClass: MyAnnotation: stringParam2 = 1
// MyClass: MyAnnotation: stringArrayParam = [3, 5, 7]
// MyClass: MyAnnotationInLib
// MyClass: MyAnnotationInLib: stringParam = 2
// MyClass: MyAnnotationInLib: stringParam2 = 1
// MyClass: MyAnnotationInLib: stringArrayParam = [3, 5, 7]
// MyClassInLib: MyAnnotationInLib
// MyClassInLib: MyAnnotationInLib: stringParam = 2
// MyClassInLib: MyAnnotationInLib: stringParam2 = 1
// MyClassInLib: MyAnnotationInLib: stringArrayParam = [3, 5, 7]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class AnnotationArgumentProcessor : AbstractTestProcessor() {
listOf("MyClass", "MyClassInLib").forEach { clsName ->
resolver.getClassDeclarationByName(clsName)?.let { cls ->
cls.annotations.forEach() { annotation ->
results.add(
"$clsName: ${annotation.annotationType.resolve().declaration.qualifiedName?.asString()}"
)
annotation.arguments.forEach {
results.add(
"$clsName: ${annotation.shortName.asString()}: ${it.name!!.asString()} = ${it.value}"
Expand Down
6 changes: 4 additions & 2 deletions test-utils/testData/api/annotationValue_java.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
// WITH_RUNTIME
// TEST PROCESSOR: AnnotationArgumentProcessor
// EXPECTED:
// MyClass: MyAnnotation
// MyClass: MyAnnotation: stringParam = 2
// MyClass: MyAnnotation: stringParam2 = 1
// MyClass: MyAnnotation: stringArrayParam = [3, 5, 7]
// MyClass: MyAnnotation: Containing.Nested
// MyClass: Containing.Nested
// MyClassInLib: MyAnnotation
// MyClassInLib: MyAnnotation: stringParam = 2
// MyClassInLib: MyAnnotation: stringParam2 = 1
// MyClassInLib: MyAnnotation: stringArrayParam = [3, 5, 7]
// MyClassInLib: MyAnnotation: Containing.Nested
// MyClassInLib: Containing.Nested
// Str
// 42
// Foo
Expand Down
3 changes: 3 additions & 0 deletions test-utils/testData/api/annotationValue_java2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
// WITH_RUNTIME
// TEST PROCESSOR: AnnotationArgumentProcessor
// EXPECTED:
// MyClass: MyAnnotation
// MyClass: MyAnnotation: stringParam = 2
// MyClass: MyAnnotation: stringParam2 = 1
// MyClass: MyAnnotation: stringArrayParam = [3, 5, 7]
// MyClass: MyAnnotationInLib
// MyClass: MyAnnotationInLib: stringParam = 2
// MyClass: MyAnnotationInLib: stringParam2 = 1
// MyClass: MyAnnotationInLib: stringArrayParam = [3, 5, 7]
// MyClassInLib: MyAnnotationInLib
// MyClassInLib: MyAnnotationInLib: stringParam = 2
// MyClassInLib: MyAnnotationInLib: stringParam2 = 1
// MyClassInLib: MyAnnotationInLib: stringArrayParam = [3, 5, 7]
Expand Down
2 changes: 1 addition & 1 deletion test-utils/testData/api/deferredJavaSymbols.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// END

// FILE: J.java
annotation class Defer
public @interface Defer {}

@Defer
class K<@Defer T> {
Expand Down

0 comments on commit 3b2b3f5

Please sign in to comment.