From 9606d493793738d862b501511cd334663775e463 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Wed, 27 Jul 2022 17:42:08 +0200 Subject: [PATCH] [light classes] drop redundant KtUltraLightSupport from annotations ^KT-53097 --- ...htClassForRepeatableAnnotationContainer.kt | 2 +- .../asJava/classes/ultraLightAnnotations.kt | 43 +++++++------------ .../asJava/classes/ultraLightMethods.kt | 2 +- .../asJava/classes/ultraLightParameters.kt | 2 +- .../kotlin/asJava/classes/ultraLightUtils.kt | 5 +-- .../KtLightModifierListDescriptorBased.kt | 10 ++--- .../elements/KtToJvmAnnotationsConverter.kt | 34 +++++---------- .../asJava/elements/lightAnnotations.kt | 2 +- 8 files changed, 37 insertions(+), 63 deletions(-) diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtUltraLightClassForRepeatableAnnotationContainer.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtUltraLightClassForRepeatableAnnotationContainer.kt index fb4a81c467c..c5de095ddbf 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtUltraLightClassForRepeatableAnnotationContainer.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtUltraLightClassForRepeatableAnnotationContainer.kt @@ -81,7 +81,7 @@ private class KtUltraLightModifierListForRepeatableAnnotationContainer( ) : KtUltraLightModifierList(containingClass, support) { override fun hasModifierProperty(name: String): Boolean = name in modifiers override fun copy() = KtUltraLightModifierListForRepeatableAnnotationContainer(containingClass, support) - override fun PsiAnnotation.additionalConverter(): KtLightAbstractAnnotation? = tryConvertAsRepeatableContainer(support) + override fun PsiAnnotation.additionalConverter(): KtLightAbstractAnnotation? = tryConvertAsRepeatableContainer() override val annotationsFilter: ((KtLightAbstractAnnotation) -> Boolean) = { it.qualifiedName in allowedAnnotations } companion object { diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightAnnotations.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightAnnotations.kt index c31d273fd9e..dcce4109302 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightAnnotations.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightAnnotations.kt @@ -28,23 +28,18 @@ class KtUltraLightNullabilityAnnotation( override fun getQualifiedName(): String? = member.qualifiedNameForNullabilityAnnotation } -fun AnnotationDescriptor.toLightAnnotation(ultraLightSupport: KtUltraLightSupport, parent: PsiElement) = - KtUltraLightSimpleAnnotation( - fqName?.asString(), - allValueArguments.map { it.key.asString() to it.value }, - ultraLightSupport, - parent - ) +fun AnnotationDescriptor.toLightAnnotation(parent: PsiElement) = KtUltraLightSimpleAnnotation( + fqName?.asString(), + allValueArguments.map { it.key.asString() to it.value }, + parent, +) -fun DeclarationDescriptor.obtainLightAnnotations( - ultraLightSupport: KtUltraLightSupport, - parent: PsiElement -): List = annotations.map { it.toLightAnnotation(ultraLightSupport, parent) } +fun DeclarationDescriptor.obtainLightAnnotations(parent: PsiElement): List = + annotations.map { it.toLightAnnotation(parent) } class KtUltraLightSimpleAnnotation( private val annotationFqName: String?, private val argumentsList: List>>, - private val ultraLightSupport: KtUltraLightSupport, parent: PsiElement, private val nameReferenceElementProvider: (() -> PsiJavaCodeReferenceElement?)? = null, ) : KtLightAbstractAnnotation(parent) { @@ -73,7 +68,7 @@ class KtUltraLightSimpleAnnotation( private inner class ParameterListImpl : KtLightElementBase(this@KtUltraLightSimpleAnnotation), PsiAnnotationParameterList { private val _attributes: Array by lazyPub { argumentsList.map { - PsiNameValuePairForAnnotationArgument(it.first, it.second, ultraLightSupport, this) + PsiNameValuePairForAnnotationArgument(it.first, it.second, this) }.toTypedArray() } @@ -88,16 +83,15 @@ class KtUltraLightSimpleAnnotation( private class PsiNameValuePairForAnnotationArgument( private val _name: String = "", private val constantValue: ConstantValue<*>, - private val ultraLightSupport: KtUltraLightSupport, - parent: PsiElement + parent: PsiElement, ) : KtLightElementBase(parent), PsiNameValuePair { override val kotlinOrigin: KtElement? get() = null private val _value by lazyPub { - constantValue.toAnnotationMemberValue(this, ultraLightSupport) + constantValue.toAnnotationMemberValue(this) } - override fun setValue(p0: PsiAnnotationMemberValue) = cannotModify() + override fun setValue(newValue: PsiAnnotationMemberValue) = cannotModify() override fun getNameIdentifier() = LightIdentifier(parent.manager, _name) @@ -108,16 +102,11 @@ private class PsiNameValuePairForAnnotationArgument( override fun getName() = _name } -private fun ConstantValue<*>.toAnnotationMemberValue( - parent: PsiElement, ultraLightSupport: KtUltraLightSupport -): PsiAnnotationMemberValue? = when (this) { - - is AnnotationValue -> value.toLightAnnotation(ultraLightSupport, parent) - - is ArrayValue -> - KtUltraLightPsiArrayInitializerMemberValue(lightParent = parent) { arrayLiteralParent -> - this.value.mapNotNull { element -> element.toAnnotationMemberValue(arrayLiteralParent, ultraLightSupport) } - } +private fun ConstantValue<*>.toAnnotationMemberValue(parent: PsiElement): PsiAnnotationMemberValue? = when (this) { + is AnnotationValue -> value.toLightAnnotation(parent) + is ArrayValue -> KtUltraLightPsiArrayInitializerMemberValue(lightParent = parent) { arrayLiteralParent -> + this.value.mapNotNull { element -> element.toAnnotationMemberValue(arrayLiteralParent) } + } is ErrorValue -> null else -> createPsiLiteral(parent) diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMethods.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMethods.kt index dcfbf4e574b..dee595e655b 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMethods.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightMethods.kt @@ -263,7 +263,7 @@ internal class KtUltraLightMethodForDescriptor( override fun getThrowsList(): PsiReferenceList = _throwsList override val givenAnnotations: List by getAndAddLazy { - methodDescriptor.obtainLightAnnotations(support, this) + methodDescriptor.obtainLightAnnotations(this) } override val qualifiedNameForNullabilityAnnotation: String? by getAndAddLazy { diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightParameters.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightParameters.kt index 5d0632d7f77..ee6bfb5f8b3 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightParameters.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightParameters.kt @@ -289,7 +289,7 @@ internal class KtUltraLightParameterForDescriptor( override fun isVarArgs() = _isVarArgs override val givenAnnotations: List by getAndAddLazy { - descriptor.obtainLightAnnotations(support, this) + descriptor.obtainLightAnnotations(this) } private val _parameterType by getAndAddLazy { diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightUtils.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightUtils.kt index cdd42fa3bc8..10f94d150f9 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightUtils.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/ultraLightUtils.kt @@ -191,12 +191,11 @@ private fun annotateByKotlinType( psiType: PsiType, kotlinType: KotlinType, psiContext: PsiElement, - ultraLightSupport: KtUltraLightSupport ): PsiType { fun KotlinType.getAnnotationsSequence(): Sequence> = sequence { - yield(annotations.mapNotNull { it.toLightAnnotation(ultraLightSupport, psiContext) }) + yield(annotations.mapNotNull { it.toLightAnnotation(psiContext) }) for (argument in arguments) { yieldAll(argument.type.getAnnotationsSequence()) } @@ -257,7 +256,7 @@ private fun KtUltraLightSupport.createTypeFromCanonicalText( val typeText = TypeInfo.createTypeText(typeInfo) ?: return PsiType.NULL val typeElement = ClsTypeElementImpl(psiContext, typeText, '\u0000') - val type = if (kotlinType != null) annotateByKotlinType(typeElement.type, kotlinType, typeElement, this) else typeElement.type + val type = if (kotlinType != null) annotateByKotlinType(typeElement.type, kotlinType, typeElement) else typeElement.type if (type is PsiArrayType && psiContext is KtUltraLightParameter && psiContext.isVarArgs) { return PsiEllipsisType(type.componentType, type.annotationProvider) diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListDescriptorBased.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListDescriptorBased.kt index e3e7aae0b29..02a2d0686c7 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListDescriptorBased.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListDescriptorBased.kt @@ -82,20 +82,20 @@ abstract class KtUltraLightModifierList): List { - if (sourceAnnotations.isEmpty()) return listOf(createRetentionRuntimeAnnotation(support, this)) + if (sourceAnnotations.isEmpty()) return listOf(createRetentionRuntimeAnnotation(this)) return mutableListOf().also { result -> sourceAnnotations.mapNotNullTo(result) { sourceAnnotation -> sourceAnnotation.additionalConverter() ?: sourceAnnotation.tryConvertAsTarget(support) - ?: sourceAnnotation.tryConvertAsRetention(support) - ?: sourceAnnotation.tryConvertAsRepeatable(support, owner) - ?: sourceAnnotation.tryConvertAsMustBeDocumented(support) + ?: sourceAnnotation.tryConvertAsRetention() + ?: sourceAnnotation.tryConvertAsRepeatable(owner) + ?: sourceAnnotation.tryConvertAsMustBeDocumented() } if (!result.any { it.qualifiedName == CommonClassNames.JAVA_LANG_ANNOTATION_RETENTION }) { - result.add(createRetentionRuntimeAnnotation(support, this)) + result.add(createRetentionRuntimeAnnotation(this)) } } } diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtToJvmAnnotationsConverter.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtToJvmAnnotationsConverter.kt index 88e36e05db8..c29b7f41c65 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtToJvmAnnotationsConverter.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtToJvmAnnotationsConverter.kt @@ -95,7 +95,6 @@ internal fun PsiAnnotation.tryConvertAsTarget(support: KtUltraLightSupport): KtL return asUltraLightSimpleAnnotation( JAVA_LANG_ANNOTATION_TARGET, listOf(targetAttributes), - support, ) } @@ -107,45 +106,37 @@ private val retentionMapping = hashMapOf( ) -internal fun createRetentionRuntimeAnnotation(support: KtUltraLightSupport, parent: PsiElement) = KtUltraLightSimpleAnnotation( +internal fun createRetentionRuntimeAnnotation(parent: PsiElement) = KtUltraLightSimpleAnnotation( JAVA_LANG_ANNOTATION_RETENTION, listOf("value" to retentionMapping["kotlin.annotation.AnnotationRetention.RUNTIME"]!!), - support, parent, ) internal fun PsiAnnotation.asUltraLightSimpleAnnotation( qualifier: String, argumentsList: List>>, - ultraLightSupport: KtUltraLightSupport, ): KtLightAbstractAnnotation = KtUltraLightSimpleAnnotation( annotationFqName = qualifier, argumentsList = argumentsList, - ultraLightSupport = ultraLightSupport, parent = parent, - nameReferenceElementProvider = { nameReferenceElement } + nameReferenceElementProvider = { nameReferenceElement }, ) -internal fun PsiAnnotation.tryConvertAsRetention(support: KtUltraLightSupport): KtLightAbstractAnnotation? { +internal fun PsiAnnotation.tryConvertAsRetention(): KtLightAbstractAnnotation? { if (FqNames.retention.asString() != qualifiedName) return null val convertedValue = extractAnnotationFqName("value")?.let { retentionMapping[it] } ?: return null return asUltraLightSimpleAnnotation( JAVA_LANG_ANNOTATION_RETENTION, listOf("value" to convertedValue), - support, ) } -internal fun PsiAnnotation.tryConvertAsMustBeDocumented(support: KtUltraLightSupport): KtLightAbstractAnnotation? = tryConvertAs( - support, +internal fun PsiAnnotation.tryConvertAsMustBeDocumented(): KtLightAbstractAnnotation? = tryConvertAs( FqNames.mustBeDocumented, JvmAnnotationNames.DOCUMENTED_ANNOTATION.asString(), ) -internal fun PsiAnnotation.tryConvertAsRepeatable( - support: KtUltraLightSupport, - owner: KtLightElement, -): KtLightAbstractAnnotation? { +internal fun PsiAnnotation.tryConvertAsRepeatable(owner: KtLightElement): KtLightAbstractAnnotation? { if (FqNames.repeatable.asString() != qualifiedName) return null val value = owner.kotlinOrigin ?.safeAs() @@ -156,7 +147,6 @@ internal fun PsiAnnotation.tryConvertAsRepeatable( return KtUltraLightSimpleAnnotation( JAVA_LANG_ANNOTATION_REPEATABLE, listOfNotNull(value), - support, parent, ) { safeAs()?.kotlinOrigin?.safeAs()?.let { @@ -171,16 +161,12 @@ internal fun PsiAnnotation.tryConvertAsRepeatable( internal val JAVA_LANG_ANNOTATION_REPEATABLE_SHORT_NAME: String get() = FqNames.repeatable.shortName().asString() -internal fun PsiAnnotation.tryConvertAsRepeatableContainer(support: KtUltraLightSupport): KtLightAbstractAnnotation? = tryConvertAs( - support, +internal fun PsiAnnotation.tryConvertAsRepeatableContainer(): KtLightAbstractAnnotation? = tryConvertAs( FqNames.repeatable, KOTLIN_JVM_INTERNAL_REPEATABLE_CONTAINER, ) -private fun PsiAnnotation.tryConvertAs( - support: KtUltraLightSupport, - from: FqName, - to: String, -): KtLightAbstractAnnotation? = takeIf { from.asString() == qualifiedName }?.let { - asUltraLightSimpleAnnotation(to, emptyList(), support) -} +private fun PsiAnnotation.tryConvertAs(from: FqName, to: String): KtLightAbstractAnnotation? = + takeIf { from.asString() == qualifiedName }?.let { + asUltraLightSimpleAnnotation(to, emptyList()) + } diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt index e6f3a057743..a27ae583e8f 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/lightAnnotations.kt @@ -87,7 +87,7 @@ class KtLightAnnotationForSourceEntry( return null } - override fun getNameReferenceElement(): PsiJavaCodeReferenceElement? = KtLightPsiJavaCodeReferenceElement( + override fun getNameReferenceElement(): PsiJavaCodeReferenceElement = KtLightPsiJavaCodeReferenceElement( kotlinOrigin.navigationElement, { (kotlinOrigin as? KtAnnotationEntry)?.typeReference?.reference