diff --git a/compiler/light-classes-base/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierList.kt b/compiler/light-classes-base/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierList.kt new file mode 100644 index 00000000000..6eb55aef3a7 --- /dev/null +++ b/compiler/light-classes-base/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierList.kt @@ -0,0 +1,57 @@ +/* + * Copyright 2010-2022 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.asJava.elements + +import com.intellij.psi.PsiAnnotation +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiModifierList +import com.intellij.psi.PsiModifierListOwner +import com.intellij.util.IncorrectOperationException +import org.jetbrains.kotlin.asJava.classes.lazyPub +import org.jetbrains.kotlin.psi.KtModifierList +import org.jetbrains.kotlin.psi.KtModifierListOwner + +abstract class KtLightModifierList>( + protected val owner: T +) : KtLightElementBase(owner), PsiModifierList, KtLightElement { + private val _annotations by lazyPub { + val annotations = computeAnnotations() + annotationsFilter?.let(annotations::filter) ?: annotations + } + + protected open val annotationsFilter: ((KtLightAbstractAnnotation) -> Boolean)? = null + + override val kotlinOrigin: KtModifierList? + get() = owner.kotlinOrigin?.modifierList + + override fun getParent() = owner + + override fun hasExplicitModifier(name: String) = hasModifierProperty(name) + + private fun throwInvalidOperation(): Nothing = throw IncorrectOperationException() + + override fun setModifierProperty(name: String, value: Boolean): Unit = throwInvalidOperation() + + override fun checkSetModifierProperty(name: String, value: Boolean): Unit = throwInvalidOperation() + + override fun addAnnotation(qualifiedName: String): PsiAnnotation = throwInvalidOperation() + + override fun getApplicableAnnotations(): Array = annotations + + override fun getAnnotations(): Array = _annotations.toTypedArray() + override fun findAnnotation(qualifiedName: String) = _annotations.firstOrNull { it.fqNameMatches(qualifiedName) } + + override fun isEquivalentTo(another: PsiElement?) = + another is KtLightModifierList<*> && owner == another.owner + + override fun isWritable() = false + + override fun toString() = "Light modifier list of $owner" + + open fun nonSourceAnnotationsForAnnotationType(sourceAnnotations: List): List = emptyList() + + abstract fun computeAnnotations(): List +} \ No newline at end of file diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassImpl.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassImpl.kt index 64b7fabeea8..dbf3de5ca3f 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassImpl.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/classes/KtLightClassImpl.kt @@ -10,7 +10,7 @@ import com.intellij.psi.impl.InheritanceImplUtil import com.intellij.psi.scope.PsiScopeProcessor import org.jetbrains.kotlin.asJava.LightClassGenerationSupport import org.jetbrains.kotlin.asJava.elements.FakeFileForLightClass -import org.jetbrains.kotlin.asJava.elements.KtLightModifierList +import org.jetbrains.kotlin.asJava.elements.KtLightModifierListDescriptorBased import org.jetbrains.kotlin.asJava.hasInterfaceDefaultImpls import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap import org.jetbrains.kotlin.config.JvmDefaultMode @@ -22,7 +22,7 @@ import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.resolve.DescriptorUtils private class KtLightClassModifierList(containingClass: KtLightClassForSourceDeclaration, computeModifiers: () -> Set) : - KtLightModifierList(containingClass) { + KtLightModifierListDescriptorBased(containingClass) { private val modifiers by lazyPub { computeModifiers() } diff --git a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierList.kt b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListDescriptorBased.kt similarity index 76% rename from compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierList.kt rename to compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListDescriptorBased.kt index f64377afc1c..e3e7aae0b29 100644 --- a/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierList.kt +++ b/compiler/light-classes/src/org/jetbrains/kotlin/asJava/elements/KtLightModifierListDescriptorBased.kt @@ -3,13 +3,19 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ +@file:JvmName("KtLightModifierListDescriptorBasedKt") + package org.jetbrains.kotlin.asJava.elements -import com.intellij.psi.* -import com.intellij.util.IncorrectOperationException +import com.intellij.psi.CommonClassNames +import com.intellij.psi.PsiAnnotation +import com.intellij.psi.PsiModifierListOwner import org.jetbrains.kotlin.asJava.LightClassGenerationSupport import org.jetbrains.kotlin.asJava.builder.LightMemberOriginForDeclaration -import org.jetbrains.kotlin.asJava.classes.* +import org.jetbrains.kotlin.asJava.classes.KtLightClassForSourceDeclaration +import org.jetbrains.kotlin.asJava.classes.KtUltraLightElementWithNullabilityAnnotation +import org.jetbrains.kotlin.asJava.classes.KtUltraLightNullabilityAnnotation +import org.jetbrains.kotlin.asJava.classes.KtUltraLightSupport import org.jetbrains.kotlin.asJava.fastCheckIsNullabilityApplied import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor @@ -21,46 +27,9 @@ import org.jetbrains.kotlin.psi.psiUtil.isPropertyParameter import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.source.getPsi -abstract class KtLightModifierList>( - protected val owner: T -) : KtLightElementBase(owner), PsiModifierList, KtLightElement { - private val _annotations by lazyPub { - val annotations = computeAnnotations() - annotationsFilter?.let(annotations::filter) ?: annotations - } - - protected open val annotationsFilter: ((KtLightAbstractAnnotation) -> Boolean)? = null - - override val kotlinOrigin: KtModifierList? - get() = owner.kotlinOrigin?.modifierList - - override fun getParent() = owner - - override fun hasExplicitModifier(name: String) = hasModifierProperty(name) - - private fun throwInvalidOperation(): Nothing = throw IncorrectOperationException() - - override fun setModifierProperty(name: String, value: Boolean): Unit = throwInvalidOperation() - - override fun checkSetModifierProperty(name: String, value: Boolean): Unit = throwInvalidOperation() - - override fun addAnnotation(qualifiedName: String): PsiAnnotation = throwInvalidOperation() - - override fun getApplicableAnnotations(): Array = annotations - - override fun getAnnotations(): Array = _annotations.toTypedArray() - override fun findAnnotation(qualifiedName: String) = _annotations.firstOrNull { it.fqNameMatches(qualifiedName) } - - override fun isEquivalentTo(another: PsiElement?) = - another is KtLightModifierList<*> && owner == another.owner - - override fun isWritable() = false - - override fun toString() = "Light modifier list of $owner" - - open fun nonSourceAnnotationsForAnnotationType(sourceAnnotations: List): List = emptyList() - - private fun computeAnnotations(): List { +abstract class KtLightModifierListDescriptorBased>(owner: T) : + KtLightModifierList(owner) { + override fun computeAnnotations(): List { val annotationsForEntries = owner.givenAnnotations ?: lightAnnotationsForEntries(this) //TODO: Hacky way to update wrong parents for annotations annotationsForEntries.forEach { @@ -98,7 +67,7 @@ abstract class KtLightModifierList, private val modifiers: Set, -) : KtUltraLightModifierListBase>(owner) { +) : KtLightModifierListDescriptorBased>(owner) { override fun hasModifierProperty(name: String) = name in modifiers override fun copy() = KtUltraLightSimpleModifierList(owner, modifiers) @@ -107,7 +76,7 @@ class KtUltraLightSimpleModifierList( abstract class KtUltraLightModifierList>( owner: T, protected val support: KtUltraLightSupport -) : KtUltraLightModifierListBase(owner) { +) : KtLightModifierListDescriptorBased(owner) { protected open fun PsiAnnotation.additionalConverter(): KtLightAbstractAnnotation? = null @@ -132,13 +101,9 @@ abstract class KtUltraLightModifierList>( - owner: T -) : KtLightModifierList(owner) - class KtLightSimpleModifierList( owner: KtLightElement, private val modifiers: Set -) : KtLightModifierList>(owner) { +) : KtLightModifierListDescriptorBased>(owner) { override fun hasModifierProperty(name: String) = name in modifiers override fun copy() = KtLightSimpleModifierList(owner, modifiers)