Analysis API: introduce annotation value for KClass

This commit is contained in:
Ilya Kirillov
2021-11-24 12:38:03 +01:00
parent f722a54c78
commit 673459580c
16 changed files with 86 additions and 17 deletions
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.analysis.api.annotations
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.jetbrains.kotlin.psi.KtElement
/**
@@ -47,6 +48,22 @@ public class KtAnnotationApplicationValue(
}
public sealed class KtKClassAnnotationValue : KtAnnotationValue()
public class KtNonLocalKClassAnnotationValue(
public val classId: ClassId,
override val sourcePsi: KtElement?,
) : KtKClassAnnotationValue()
public class KtLocalKClassAnnotationValue(
public val ktClass: KtClassOrObject,
override val sourcePsi: KtElement?,
) : KtKClassAnnotationValue()
public class KtErrorClassAnnotationValue(
override val sourcePsi: KtElement?,
) : KtKClassAnnotationValue()
public class KtEnumEntryAnnotationValue(
public val callableId: CallableId?,
override val sourcePsi: KtElement?,
@@ -5,6 +5,8 @@
package org.jetbrains.kotlin.analysis.api.annotations
import org.jetbrains.kotlin.renderer.render
public object KtAnnotationValueRenderer {
public fun render(value: KtAnnotationValue): String = buildString {
renderConstantValue(value)
@@ -25,11 +27,23 @@ public object KtAnnotationValueRenderer {
renderConstantAnnotationValue(value)
}
KtUnsupportedAnnotationValue -> {
append("KtUnsupportedConstantValue")
append("error(\"non-annotation value\")")
}
is KtKClassAnnotationValue -> {
renderKClassAnnotationValue(value)
}
}
}
private fun StringBuilder.renderKClassAnnotationValue(value: KtKClassAnnotationValue) {
when (value) {
is KtErrorClassAnnotationValue -> append("UNRESOLVED_CLASS")
is KtLocalKClassAnnotationValue -> append(value.ktClass.nameAsName?.render())
is KtNonLocalKClassAnnotationValue -> append(value.classId.asSingleFqName().render())
}
append("::class")
}
private fun StringBuilder.renderConstantAnnotationValue(value: KtConstantAnnotationValue) {
append(KtConstantValueRenderer.render(value.constantValue))
}
@@ -1,5 +1,5 @@
KtDeclaration: KtClass Foo
annotations: [
A(a = 1, c = KtUnsupportedConstantValue)
A(a = 1, c = kotlin.Int::class)
psi: KtAnnotationEntry
]
@@ -1,5 +1,5 @@
KtDeclaration: KtNamedFunction foo
annotations: [
A(a = 1, c = KtUnsupportedConstantValue)
A(a = 1, c = kotlin.Int::class)
psi: KtAnnotationEntry
]
@@ -1,5 +1,5 @@
KtDeclaration: KtProperty foo
annotations: [
A(a = 1, c = KtUnsupportedConstantValue)
A(a = 1, c = kotlin.Int::class)
psi: KtAnnotationEntry
]
@@ -1,5 +1,5 @@
KtDeclaration: KtTypeAlias Foo
annotations: [
A(a = 1, c = KtUnsupportedConstantValue)
A(a = 1, c = kotlin.Int::class)
psi: KtAnnotationEntry
]
@@ -1,5 +1,5 @@
KtDeclaration: KtPropertyAccessor null
annotations: [
A(a = 1, c = KtUnsupportedConstantValue)
A(a = 1, c = kotlin.Int::class)
psi: KtAnnotationEntry
]
@@ -1,5 +1,5 @@
KtDeclaration: KtParameter bar
annotations: [
A(a = 1, c = KtUnsupportedConstantValue)
A(a = 1, c = kotlin.Int::class)
psi: KtAnnotationEntry
]
@@ -1,5 +1,5 @@
KtDeclaration: KtProperty foo
annotations: [
A(a = 1, c = KtUnsupportedConstantValue)
A(a = 1, c = kotlin.Int::class)
psi: KtAnnotationEntry
]
@@ -1,5 +1,5 @@
KtDeclaration: KtPropertyAccessor null
annotations: [
A(a = 1, c = KtUnsupportedConstantValue)
A(a = 1, c = kotlin.Int::class)
psi: KtAnnotationEntry
]
@@ -1,3 +1,3 @@
expression: 42u
constant: 42
constantValueKind: Long
constantValueKind: UInt