[light classes] drop old light classes and backend: iteration #2

drop javaFileStub and fix tests

^KT-48773
This commit is contained in:
Dmitry Gridin
2022-06-21 20:06:19 +02:00
committed by Space
parent 3cb2df9360
commit 97ce502cbe
42 changed files with 79 additions and 103 deletions
@@ -5,11 +5,9 @@
package org.jetbrains.kotlin.asJava.builder
import com.intellij.psi.impl.java.stubs.PsiJavaFileStub
import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics
interface LightClassDataHolder {
val javaFileStub: PsiJavaFileStub
val extraDiagnostics: Diagnostics
interface ForClass : LightClassDataHolder
@@ -18,16 +16,11 @@ interface LightClassDataHolder {
}
object InvalidLightClassDataHolder : LightClassDataHolder.ForClass {
override val javaFileStub: PsiJavaFileStub
get() = shouldNotBeCalled()
override val extraDiagnostics: Diagnostics
get() = shouldNotBeCalled()
override val extraDiagnostics: Diagnostics get() = shouldNotBeCalled()
private fun shouldNotBeCalled(): Nothing = throw UnsupportedOperationException("Should not be called")
}
class LightClassDataHolderImpl(
override val javaFileStub: PsiJavaFileStub,
override val extraDiagnostics: Diagnostics
) : LightClassDataHolder.ForClass, LightClassDataHolder.ForFacade, LightClassDataHolder.ForScript
@@ -47,7 +47,6 @@ abstract class KtLightClassForFacadeImpl constructor(
FakeFileForLightClass(
firstFileInFacade,
lightClass = { this },
stub = { null },
packageFqName = facadeClassFqName.parent()
)
}
@@ -43,7 +43,6 @@ abstract class KtLightClassForScript(val script: KtScript) : KtLazyLightClass(sc
FakeFileForLightClass(
script.containingKtFile,
lightClass = { this },
stub = { null },
packageFqName = fqName.parent(),
)
}
@@ -12,7 +12,6 @@ import com.intellij.openapi.util.Key
import com.intellij.openapi.util.TextRange
import com.intellij.psi.*
import com.intellij.psi.impl.InheritanceImplUtil
import com.intellij.psi.impl.java.stubs.PsiJavaFileStub
import com.intellij.psi.impl.source.PsiImmediateClassType
import com.intellij.psi.impl.source.tree.TreeUtil
import com.intellij.psi.scope.PsiScopeProcessor
@@ -44,7 +43,6 @@ import org.jetbrains.kotlin.load.java.structure.LightClassOriginKind
import org.jetbrains.kotlin.name.FqNameUnsafe
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.debugText.getDebugText
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.psi.stubs.KotlinClassOrObjectStub
@@ -99,24 +97,13 @@ abstract class KtLightClassForSourceDeclaration(
abstract override fun getParent(): PsiElement?
abstract override fun getQualifiedName(): String?
private fun getJavaFileStub(): PsiJavaFileStub = getLightClassDataHolder().javaFileStub
fun getDescriptor() =
LightClassGenerationSupport.getInstance(project).resolveToDescriptor(classOrObject) as? ClassDescriptor
protected fun getLightClassDataHolder(): LightClassDataHolder.ForClass {
val lightClassData = getLightClassDataHolder(classOrObject)
if (lightClassData is InvalidLightClassDataHolder) {
LOG.error("Invalid light class data for existing light class:\n$lightClassData\n${classOrObject.getElementTextWithContext()}")
}
return lightClassData
}
private val _containingFile: PsiFile by lazyPub {
object : FakeFileForLightClass(
classOrObject.containingKtFile,
{ if (classOrObject.isTopLevel()) this else create(getOutermostClassOrObject(classOrObject))!! },
{ getJavaFileStub() }
) {
override fun findReferenceAt(offset: Int) = ktFile.findReferenceAt(offset)
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.asJava.classes
import com.intellij.psi.*
import com.intellij.psi.impl.PsiImplUtil
import com.intellij.psi.impl.compiled.ClsJavaCodeReferenceElementImpl
import com.intellij.psi.impl.light.LightIdentifier
import org.jetbrains.kotlin.asJava.elements.KtLightAbstractAnnotation
import org.jetbrains.kotlin.asJava.elements.KtLightElementBase
@@ -47,7 +48,11 @@ class KtUltraLightSimpleAnnotation(
parent: PsiElement,
private val nameReferenceElementProvider: (() -> PsiJavaCodeReferenceElement?)? = null,
) : KtLightAbstractAnnotation(parent) {
override fun getNameReferenceElement(): PsiJavaCodeReferenceElement? = nameReferenceElementProvider?.invoke()
private val _nameReferenceElement: PsiJavaCodeReferenceElement? by lazyPub {
nameReferenceElementProvider?.invoke() ?: annotationFqName?.let { ClsJavaCodeReferenceElementImpl(parent, it) }
}
override fun getNameReferenceElement(): PsiJavaCodeReferenceElement? = _nameReferenceElement
private val parameterList = ParameterListImpl()
@@ -69,9 +69,6 @@ internal class KtUltraLightFieldForSourceDeclaration(
private val lightIdentifier = KtLightIdentifier(this, declaration)
override fun getNameIdentifier(): PsiIdentifier = lightIdentifier
override fun getText(): String? = kotlinOrigin.text
override fun getTextRange(): TextRange = kotlinOrigin.textRange
override fun getTextOffset(): Int = kotlinOrigin.textOffset
override fun getStartOffsetInParent(): Int = kotlinOrigin.startOffsetInParent
override fun isWritable(): Boolean = kotlinOrigin.isWritable
override fun getNavigationElement(): PsiElement = kotlinOrigin.navigationElement ?: this
@@ -106,6 +103,10 @@ internal open class KtUltraLightFieldImpl protected constructor(
override fun getLanguage(): Language = KotlinLanguage.INSTANCE
override fun getText(): String? = kotlinOrigin.text
override fun getTextRange(): TextRange? = kotlinOrigin.textRange
override fun getTextOffset(): Int = kotlinOrigin.textOffset
private val variableDescriptor: VariableDescriptor?
get() = declaration.resolve()
?.let { it as? PropertyDescriptor ?: it as? ValueParameterDescriptor }
@@ -116,11 +117,14 @@ internal open class KtUltraLightFieldImpl protected constructor(
declaration.delegateExpression?.let {
LightClassGenerationSupport.getInstance(project).analyze(it).getType(it)
}
declaration is KtObjectDeclaration ->
(declaration.resolve() as? ClassDescriptor)?.defaultType
declaration is KtEnumEntry -> {
(containingClass.kotlinOrigin?.resolve() as? ClassDescriptor)?.defaultType
}
else -> {
declaration.getKotlinType()
}
@@ -145,6 +149,7 @@ internal open class KtUltraLightFieldImpl protected constructor(
kotlinType?.asPsiType(support, TypeMappingMode.DEFAULT, this)
?.let(TypeConversionUtil::erasure)
?: nonExistent()
else -> {
val kotlinType = declaration.getKotlinType() ?: return@lazyPub PsiType.NULL
val descriptor = variableDescriptor ?: return@lazyPub PsiType.NULL
@@ -90,8 +90,9 @@ internal abstract class KtUltraLightParameter(
ultraLightMethod,
ultraLightMethod.language
), KtUltraLightElementWithNullabilityAnnotation<KtParameter, PsiParameter>, KtLightParameter {
override fun isEquivalentTo(another: PsiElement?): Boolean = kotlinOrigin == another
override fun isEquivalentTo(another: PsiElement?): Boolean {
return another is KtParameter && kotlinOrigin?.isEquivalentTo(another) == true || this == another
}
private val lightModifierList by lazyPub { KtLightSimpleModifierList(this, emptySet()) }
@@ -100,6 +101,10 @@ internal abstract class KtUltraLightParameter(
override fun getNavigationElement(): PsiElement = kotlinOrigin ?: method.navigationElement
override fun getUseScope(): SearchScope = kotlinOrigin?.useScope ?: LocalSearchScope(this)
override fun getText(): String? = kotlinOrigin?.text.orEmpty()
override fun getTextRange(): TextRange? = kotlinOrigin?.textRange
override fun getTextOffset(): Int = kotlinOrigin?.textOffset ?: super.getTextOffset()
override fun isValid() = parent.isValid
override fun computeQualifiedNameForNullabilityAnnotation(kotlinType: KotlinType?): String? {
@@ -198,9 +203,6 @@ internal class KtUltraLightParameterForSource(
}
}
override fun getText(): String? = kotlinOrigin.text
override fun getTextRange(): TextRange = kotlinOrigin.textRange
override fun getTextOffset(): Int = kotlinOrigin.textOffset
override fun getStartOffsetInParent(): Int = kotlinOrigin.startOffsetInParent
override fun isWritable(): Boolean = kotlinOrigin.isWritable
override fun getNavigationElement(): PsiElement = kotlinOrigin.navigationElement
@@ -19,7 +19,6 @@ import com.intellij.psi.impl.java.stubs.impl.PsiJavaFileStubImpl
import com.intellij.psi.impl.source.PsiFileImpl
import com.intellij.psi.impl.source.SourceTreeToPsiMap
import com.intellij.psi.impl.source.tree.TreeElement
import com.intellij.psi.stubs.PsiClassHolderFileStub
import com.intellij.psi.util.PsiUtil
import com.intellij.reference.SoftReference
import com.intellij.util.AstLoadingFilter
@@ -33,8 +32,7 @@ import java.lang.ref.Reference
open class FakeFileForLightClass(
val ktFile: KtFile,
private val lightClass: () -> KtLightClass,
private val stub: () -> PsiClassHolderFileStub<*>?,
private val packageFqName: FqName = ktFile.packageFqName
private val packageFqName: FqName = ktFile.packageFqName,
) : ClsFileImpl(ktFile.viewProvider) {
override fun getVirtualFile(): VirtualFile =
@@ -49,7 +47,7 @@ open class FakeFileForLightClass(
return javaFileStub
}
override fun getStub() = stub() ?: createFakeJavaFileStub()
override fun getStub() = createFakeJavaFileStub()
override fun getClasses() = arrayOf(lightClass())
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtModifierListOwner
import org.jetbrains.kotlin.resolve.constants.ArrayValue
import org.jetbrains.kotlin.resolve.constants.ConstantValue
import org.jetbrains.kotlin.resolve.constants.EnumValue
import org.jetbrains.kotlin.resolve.constants.KClassValue
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -91,11 +92,10 @@ internal fun PsiAnnotation.tryConvertAsTarget(support: KtUltraLightSupport): KtL
val targetAttributes = "value" to ArrayValue(convertedValues) { module -> module.builtIns.array.defaultType }
return KtUltraLightSimpleAnnotation(
return asUltraLightSimpleAnnotation(
JAVA_LANG_ANNOTATION_TARGET,
listOf(targetAttributes),
support,
parent
)
}
@@ -111,17 +111,28 @@ internal fun createRetentionRuntimeAnnotation(support: KtUltraLightSupport, pare
JAVA_LANG_ANNOTATION_RETENTION,
listOf("value" to retentionMapping["kotlin.annotation.AnnotationRetention.RUNTIME"]!!),
support,
parent
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 }
)
internal fun PsiAnnotation.tryConvertAsRetention(support: KtUltraLightSupport): KtLightAbstractAnnotation? {
if (FqNames.retention.asString() != qualifiedName) return null
val convertedValue = extractAnnotationFqName("value")?.let { retentionMapping[it] } ?: return null
return KtUltraLightSimpleAnnotation(
return asUltraLightSimpleAnnotation(
JAVA_LANG_ANNOTATION_RETENTION,
listOf("value" to convertedValue),
support,
parent
)
}
@@ -171,5 +182,5 @@ private fun PsiAnnotation.tryConvertAs(
from: FqName,
to: String,
): KtLightAbstractAnnotation? = takeIf { from.asString() == qualifiedName }?.let {
KtUltraLightSimpleAnnotation(to, emptyList(), support, parent)
asUltraLightSimpleAnnotation(to, emptyList(), support)
}