[AA] KtAnnotationApplication: add 'index' parameter

it is required for lazy annotations resolve

^KT-56046
This commit is contained in:
Dmitrii Gridin
2023-01-24 14:04:05 +01:00
committed by Space Team
parent 13fd281562
commit 4b955944ff
8 changed files with 59 additions and 43 deletions
@@ -29,9 +29,9 @@ internal class KtFe10AnnotationsList private constructor(
override val annotations: List<KtAnnotationApplication>
get() = withValidityAssertion {
fe10Annotations.mapNotNull { annotation ->
fe10Annotations.mapIndexedNotNull { index, annotation ->
if (annotation.annotationClass.classId in annotationsToIgnore) null
else annotation.toKtAnnotationApplication(analysisContext)
else annotation.toKtAnnotationApplication(analysisContext, index)
}
}
@@ -61,9 +61,10 @@ internal class KtFe10AnnotationsList private constructor(
override fun annotationsByClassId(classId: ClassId): List<KtAnnotationApplication> = withValidityAssertion {
if (classId in annotationsToIgnore) return@withValidityAssertion emptyList()
fe10Annotations.mapNotNull { annotation ->
if (annotation.annotationClass?.maybeLocalClassId != classId) return@mapNotNull null
annotation.toKtAnnotationApplication(analysisContext)
fe10Annotations.mapIndexedNotNull { index, annotation ->
if (annotation.annotationClass?.maybeLocalClassId != classId) return@mapIndexedNotNull null
annotation.toKtAnnotationApplication(analysisContext, index)
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2023 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.
*/
@@ -33,7 +33,10 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.impl.*
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.load.java.descriptors.*
import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
import org.jetbrains.kotlin.load.java.descriptors.JavaForKotlinOverridePropertyDescriptor
import org.jetbrains.kotlin.load.java.descriptors.JavaPropertyDescriptor
import org.jetbrains.kotlin.load.java.sources.JavaSourceElement
import org.jetbrains.kotlin.load.kotlin.toSourceElement
import org.jetbrains.kotlin.name.CallableId
@@ -422,19 +425,21 @@ internal tailrec fun KotlinBuiltIns.areSameArrayTypeIgnoringProjections(left: Ko
}
internal fun List<ConstantValue<*>>.expandArrayAnnotationValue(containingArrayType: KotlinType, analysisContext: Fe10AnalysisContext)
: List<KtAnnotationValue> = flatMap { constantValue ->
internal fun List<ConstantValue<*>>.expandArrayAnnotationValue(
containingArrayType: KotlinType,
analysisContext: Fe10AnalysisContext,
): List<KtAnnotationValue> = flatMapIndexed { index: Int, constantValue: ConstantValue<*> ->
val constantType = constantValue.getType(analysisContext.resolveSession.moduleDescriptor)
if (analysisContext.builtIns.areSameArrayTypeIgnoringProjections(containingArrayType, constantType)) {
// If an element in the array has the same type as the containing array, it's a spread component that needs
// to be expanded here. (It should have the array element type instead.)
(constantValue as ArrayValue).value.expandArrayAnnotationValue(containingArrayType, analysisContext)
} else {
listOf(constantValue.toKtAnnotationValue(analysisContext))
listOf(constantValue.toKtAnnotationValue(analysisContext, index))
}
}
internal fun ConstantValue<*>.toKtAnnotationValue(analysisContext: Fe10AnalysisContext): KtAnnotationValue {
internal fun ConstantValue<*>.toKtAnnotationValue(analysisContext: Fe10AnalysisContext, index: Int): KtAnnotationValue {
return when (this) {
is ArrayValue -> {
val arrayType = getType(analysisContext.resolveSession.moduleDescriptor)
@@ -456,6 +461,7 @@ internal fun ConstantValue<*>.toKtAnnotationValue(analysisContext: Fe10AnalysisC
psi = null,
useSiteTarget = null,
arguments = value.getKtNamedAnnotationArguments(analysisContext),
index = index,
)
)
}
@@ -602,18 +608,19 @@ internal fun createKtInitializerValue(
return KtNonConstantInitializerValue(initializer)
}
internal fun AnnotationDescriptor.toKtAnnotationApplication(analysisContext: Fe10AnalysisContext): KtAnnotationApplication {
internal fun AnnotationDescriptor.toKtAnnotationApplication(analysisContext: Fe10AnalysisContext, index: Int): KtAnnotationApplication {
return KtAnnotationApplication(
annotationClass?.maybeLocalClassId,
(source as? PsiSourceElement)?.psi as? KtCallElement,
(this as? LazyAnnotationDescriptor)?.annotationEntry?.useSiteTarget?.getAnnotationUseSiteTarget(),
getKtNamedAnnotationArguments(analysisContext),
index,
)
}
internal fun AnnotationDescriptor.getKtNamedAnnotationArguments(analysisContext: Fe10AnalysisContext) =
internal fun AnnotationDescriptor.getKtNamedAnnotationArguments(analysisContext: Fe10AnalysisContext): List<KtNamedAnnotationValue> =
allValueArguments.map { (name, value) ->
KtNamedAnnotationValue(name, value.toKtAnnotationValue(analysisContext))
KtNamedAnnotationValue(name, value.toKtAnnotationValue(analysisContext, index = -1))
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2023 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.
*/
@@ -41,7 +41,7 @@ internal fun PrettyPrinter.renderFe10Annotations(
printCollectionIfNotEmpty(valueArguments, separator = ", ", prefix = "(", postfix = ")") { (name, value) ->
append(name.render())
append(" = ")
append(value.toKtAnnotationValue(analysisContext).renderAsSourceCode())
append(value.toKtAnnotationValue(analysisContext, -1).renderAsSourceCode())
}
append(separator)