[light classes] drop redundant KtUltraLightSupport from annotations

^KT-53097
This commit is contained in:
Dmitry Gridin
2022-07-27 17:42:08 +02:00
committed by Space
parent 4f18e7091b
commit 9606d49379
8 changed files with 37 additions and 63 deletions
@@ -81,7 +81,7 @@ private class KtUltraLightModifierListForRepeatableAnnotationContainer(
) : KtUltraLightModifierList<KtLightClassForSourceDeclaration>(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 {
@@ -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<KtLightAbstractAnnotation> = annotations.map { it.toLightAnnotation(ultraLightSupport, parent) }
fun DeclarationDescriptor.obtainLightAnnotations(parent: PsiElement): List<KtLightAbstractAnnotation> =
annotations.map { it.toLightAnnotation(parent) }
class KtUltraLightSimpleAnnotation(
private val annotationFqName: String?,
private val argumentsList: List<Pair<String, ConstantValue<*>>>,
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<PsiNameValuePair> 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)
@@ -263,7 +263,7 @@ internal class KtUltraLightMethodForDescriptor(
override fun getThrowsList(): PsiReferenceList = _throwsList
override val givenAnnotations: List<KtLightAbstractAnnotation> by getAndAddLazy {
methodDescriptor.obtainLightAnnotations(support, this)
methodDescriptor.obtainLightAnnotations(this)
}
override val qualifiedNameForNullabilityAnnotation: String? by getAndAddLazy {
@@ -289,7 +289,7 @@ internal class KtUltraLightParameterForDescriptor(
override fun isVarArgs() = _isVarArgs
override val givenAnnotations: List<KtLightAbstractAnnotation> by getAndAddLazy {
descriptor.obtainLightAnnotations(support, this)
descriptor.obtainLightAnnotations(this)
}
private val _parameterType by getAndAddLazy {
@@ -191,12 +191,11 @@ private fun annotateByKotlinType(
psiType: PsiType,
kotlinType: KotlinType,
psiContext: PsiElement,
ultraLightSupport: KtUltraLightSupport
): PsiType {
fun KotlinType.getAnnotationsSequence(): Sequence<List<PsiAnnotation>> =
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)
@@ -82,20 +82,20 @@ abstract class KtUltraLightModifierList<out T : KtLightElement<KtModifierListOwn
override fun nonSourceAnnotationsForAnnotationType(sourceAnnotations: List<PsiAnnotation>): List<KtLightAbstractAnnotation> {
if (sourceAnnotations.isEmpty()) return listOf(createRetentionRuntimeAnnotation(support, this))
if (sourceAnnotations.isEmpty()) return listOf(createRetentionRuntimeAnnotation(this))
return mutableListOf<KtLightAbstractAnnotation>().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))
}
}
}
@@ -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<Pair<String, ConstantValue<*>>>,
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<KtModifierListOwner, PsiModifierListOwner>,
): KtLightAbstractAnnotation? {
internal fun PsiAnnotation.tryConvertAsRepeatable(owner: KtLightElement<KtModifierListOwner, PsiModifierListOwner>): KtLightAbstractAnnotation? {
if (FqNames.repeatable.asString() != qualifiedName) return null
val value = owner.kotlinOrigin
?.safeAs<KtClass>()
@@ -156,7 +147,6 @@ internal fun PsiAnnotation.tryConvertAsRepeatable(
return KtUltraLightSimpleAnnotation(
JAVA_LANG_ANNOTATION_REPEATABLE,
listOfNotNull(value),
support,
parent,
) {
safeAs<KtLightAnnotationForSourceEntry>()?.kotlinOrigin?.safeAs<KtAnnotationEntry>()?.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())
}
@@ -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