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
@@ -8,10 +8,12 @@ package org.jetbrains.kotlin.analysis.api.descriptors.utils
import org.jetbrains.kotlin.analysis.api.components.KtDeclarationRendererOptions
import org.jetbrains.kotlin.analysis.api.components.RendererModifier
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisContext
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.*
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.classId
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.isExplicitOverride
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.ktModality
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.ktVisibility
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtConstantValueRenderer
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor
@@ -103,11 +105,6 @@ internal class KtFe10Renderer(
return
}
renderAnnotations(type.annotations) { classId ->
!options.typeRendererOptions.renderFunctionType
|| classId != StandardClassIds.Annotations.ExtensionFunctionType
}
typeRenderer.render(type, this)
}
@@ -510,35 +507,7 @@ internal class KtFe10Renderer(
return
}
for (annotation in annotations) {
val annotationClass = annotation.annotationClass ?: continue
val classId = annotationClass.classId
if (classId != null && !predicate(classId)) {
continue
}
if (annotationClass.fqNameSafe != StandardNames.FqNames.parameterName) {
append('@')
renderType(annotation.type)
val valueArguments = annotation.allValueArguments.entries.sortedBy { it.key.asString() }
renderList(valueArguments, separator = ", ", prefix = "(", postfix = ")", renderWhenEmpty = false) { (name, value) ->
append(name.render())
append(" = ")
when (value) {
is UByteValue -> append(value.value.toUByte().toString())
is UShortValue -> append(value.value.toUShort().toString())
is UIntValue -> append(value.value.toUInt().toString())
is ULongValue -> append(value.value.toULong().toString())
is EnumValue, is AnnotationValue, is KClassValue, is ArrayValue, is ErrorValue -> append("NOT_CONST_EXPRESSION")
else -> append(value.value)
}
}
append(' ')
}
}
renderFe10Annotations(annotations, predicate)
}
private fun KtFe10RendererConsumer.renderModifiers(descriptor: DeclarationDescriptor) {
@@ -44,6 +44,10 @@ internal class KtFe10TypeRenderer(private val options: KtTypeRendererOptions, pr
private fun KtFe10RendererConsumer.renderType(type: KotlinType) {
if (isDebugText) {
renderTypeAnnotationsDebug(type)
} else {
renderFe10Annotations(type.annotations) { classId ->
classId != StandardClassIds.Annotations.ExtensionFunctionType
}
}
when (val unwrappedType = type.unwrap()) {
is FlexibleType -> renderFlexibleType(unwrappedType)
@@ -0,0 +1,40 @@
/*
* 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.descriptors.utils
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.classId
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtConstantValue
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtConstantValueRenderer
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
internal fun KtFe10RendererConsumer.renderFe10Annotations(annotations: Annotations, predicate: (ClassId) -> Boolean = { true }) {
for (annotation in annotations) {
val annotationClass = annotation.annotationClass ?: continue
val classId = annotationClass.classId
if (classId != null && !predicate(classId)) {
continue
}
if (annotationClass.fqNameSafe != StandardNames.FqNames.parameterName) {
append('@')
append(annotation.fqName?.shortName()?.asString() ?: "ERROR")
val valueArguments = annotation.allValueArguments.entries.sortedBy { it.key.asString() }
renderList(valueArguments, separator = ", ", prefix = "(", postfix = ")", renderWhenEmpty = false) { (name, value) ->
append(name.render())
append(" = ")
append(KtConstantValueRenderer.render(value.toKtConstantValue()))
}
append(' ')
}
}
}
@@ -185,4 +185,26 @@ public class KtFe10RendererTestGenerated extends AbstractKtFe10RendererTest {
public void testWhere() throws Exception {
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/where.kt");
}
@Nested
@TestMetadata("analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/types")
@TestDataPath("$PROJECT_ROOT")
public class Types {
@Test
public void testAllFilesPresentInTypes() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/types"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@Test
@TestMetadata("annotaionOnTypes.kt")
public void testAnnotaionOnTypes() throws Exception {
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/types/annotaionOnTypes.kt");
}
@Test
@TestMetadata("annotaionOnTypesWithComplexExpression.kt")
public void testAnnotaionOnTypesWithComplexExpression() throws Exception {
runTest("analysis/analysis-api/testData/components/symbolDeclarationRenderer/renderDeclaration/types/annotaionOnTypesWithComplexExpression.kt");
}
}
}