Analysis API: fix annotation rendering for types

This commit is contained in:
Ilya Kirillov
2021-11-18 19:09:21 +01:00
parent 3a5e503f29
commit 193df3e3c4
18 changed files with 262 additions and 75 deletions
@@ -35,7 +35,12 @@ public data class KtTypeRendererOptions(
/**
* Whether to render type arguments.
*/
public val renderTypeArguments: Boolean = true
public val renderTypeArguments: Boolean = true,
/**
* Should annotations on types be rendered.
*/
public val renderTypeAnnotations: Boolean = true
) {
public companion object {
public val DEFAULT: KtTypeRendererOptions = KtTypeRendererOptions()
@@ -0,0 +1,103 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.api.symbols.markers
import org.jetbrains.kotlin.analysis.api.annotations.KtNamedConstantValue
import org.jetbrains.kotlin.types.ConstantValueKind
public object KtConstantValueRenderer {
public fun render(value: KtConstantValue): String = buildString {
renderConstantValue(value)
}
private fun StringBuilder.renderConstantValue(value: KtConstantValue) {
when (value) {
is KtAnnotationConstantValue -> {
renderAnnotationConstantValue(value)
}
is KtArrayConstantValue -> {
renderArrayConstantValue(value)
}
is KtEnumEntryConstantValue -> {
renderEnumEntryConstantValue(value)
}
is KtErrorValue -> {
append("ERROR")
}
is KtLiteralConstantValue<*> -> {
renderLiteralConstantValue(value)
}
KtUnsupportedConstantValue -> {
append("KtUnsupportedConstantValue")
}
}
}
private fun StringBuilder.renderLiteralConstantValue(value: KtLiteralConstantValue<*>) {
when (value.constantValueKind) {
ConstantValueKind.String -> {
append('"')
append(value.value)
append('"')
}
ConstantValueKind.Char -> {
append("'")
append(value.value)
append("'")
}
else -> {
append(value.value)
}
}
}
private fun StringBuilder.renderEnumEntryConstantValue(value: KtEnumEntryConstantValue) {
append(value.callableId?.asSingleFqName()?.asString())
}
private fun StringBuilder.renderAnnotationConstantValue(value: KtAnnotationConstantValue) {
append(value.classId)
if (value.arguments.isNotEmpty()) {
append("(")
renderNamedConstantValueList(value.arguments)
append(")")
}
}
private fun StringBuilder.renderArrayConstantValue(value: KtArrayConstantValue) {
append("[")
renderConstantValueList(value.values)
append("]")
}
private fun StringBuilder.renderConstantValueList(list: Collection<KtConstantValue>) {
renderWithSeparator(list, ", ") { constantValue ->
renderConstantValue(constantValue)
}
}
private fun StringBuilder.renderNamedConstantValueList(list: Collection<KtNamedConstantValue>) {
renderWithSeparator(list, ", ") { namedValue ->
append(namedValue.name)
append(" = ")
renderConstantValue(namedValue.expression)
append(", ")
}
}
private inline fun <E> StringBuilder.renderWithSeparator(
collection: Collection<E>,
separator: String,
render: StringBuilder.(E) -> Unit
) {
collection.forEachIndexed { index, element ->
render(element)
if (index != collection.size - 1) {
append(separator)
}
}
}
}
@@ -1,4 +1,4 @@
@Target(allowedTargets = NOT_CONST_EXPRESSION) annotation class base
@Target(allowedTargets = [kotlin.annotation.AnnotationTarget.ANNOTATION_CLASS]) annotation class base
@base annotation class derived
@base class correct {
constructor(x: Int)
@@ -1,4 +1,4 @@
@Target(allowedTargets = NOT_CONST_EXPRESSION) annotation class base
@Target(allowedTargets = [kotlin.annotation.AnnotationTarget.ANNOTATION_CLASS]) annotation class base
@base annotation class derived
@base class correct {
constructor(@base x: Int)
@@ -0,0 +1,9 @@
@Target(AnnotationTarget.TYPE)
annotation class A1
@Target(AnnotationTarget.TYPE)
annotation class A2(val value: String)
fun x(): @A1 @A2("LIST") List<@A2("INT") Int> {
TODO()
}
@@ -0,0 +1,6 @@
@Target(allowedTargets = [kotlin.annotation.AnnotationTarget.TYPE]) annotation class A1
@Target(allowedTargets = [kotlin.annotation.AnnotationTarget.TYPE]) annotation class A2 {
constructor(value: String)
val value: String
}
fun x(): @A1 @A2(value = "LIST") List<@A2(value = "INT") Int>
@@ -0,0 +1,6 @@
@Target(AnnotationTarget.TYPE)
annotation class A(val value: Int)
fun x(): @A(1 + 2) Int {
TODO()
}
@@ -0,0 +1,5 @@
@Target(allowedTargets = [kotlin.annotation.AnnotationTarget.TYPE]) annotation class A {
constructor(value: Int)
val value: Int
}
fun x(): @A(value = 3) Int
@@ -0,0 +1,3 @@
@Target(allowedTargets = [kotlin.annotation.AnnotationTarget.ANNOTATION_CLASS, kotlin.annotation.AnnotationTarget.VALUE_PARAMETER, kotlin.annotation.AnnotationTarget.TYPE]) annotation class base
fun foo1(vararg ints: Int)
fun foo2(@base vararg ints: @base Int)
@@ -1,3 +1,3 @@
@Target(allowedTargets = NOT_CONST_EXPRESSION) annotation class base
@Target(allowedTargets = [kotlin.annotation.AnnotationTarget.ANNOTATION_CLASS, kotlin.annotation.AnnotationTarget.VALUE_PARAMETER, kotlin.annotation.AnnotationTarget.TYPE]) annotation class base
fun foo1(vararg ints: Int)
fun foo2(@base vararg ints: @base Int)
fun foo2(@base vararg ints: Int)