Refactor light elements

KtLightElement#delegate -> clsDelegate, KtLightElement#origin -> kotlinOrigin and make them properties
KtLightClassForDecompiledDeclaration stores KtClsFile
KtLightField stores LightMemberOrigin
This commit is contained in:
Pavel V. Talanov
2016-03-29 18:48:30 +03:00
parent 02543295f9
commit f40a04c5a2
46 changed files with 247 additions and 254 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -86,7 +86,7 @@ class KotlinIconProvider : IconProvider(), DumbAware {
is KtPackageDirective -> PlatformIcons.PACKAGE_ICON
is KtLightClassForFacade -> KotlinIcons.FILE
is KtLightClassForDecompiledDeclaration -> {
val origin = getOrigin()
val origin = kotlinOrigin
//TODO (light classes for decompiled files): correct presentation
if (origin != null) origin.getBaseIcon() else KotlinIcons.CLASS
}
@@ -185,7 +185,7 @@ class IDELightClassGenerationSupport(private val project: Project) : LightClassG
return withFakeLightClasses(lightClassForFacade, facadeFiles)
}
else {
return facadeFiles.filter { it.isCompiled }.mapNotNull { createLightClassForDecompiledKotlinFile(it) }
return facadeFiles.filterIsInstance<KtClsFile>().mapNotNull { createLightClassForDecompiledKotlinFile(it) }
}
}
@@ -303,7 +303,7 @@ class IDELightClassGenerationSupport(private val project: Project) : LightClassG
return getClassRelativeName(parent).child(name)
}
fun createLightClassForDecompiledKotlinFile(file: KtFile): KtLightClassForDecompiledDeclaration? {
fun createLightClassForDecompiledKotlinFile(file: KtClsFile): KtLightClassForDecompiledDeclaration? {
val virtualFile = file.virtualFile ?: return null
val classOrObject = file.declarations.filterIsInstance<KtClassOrObject>().singleOrNull()
@@ -313,7 +313,7 @@ class IDELightClassGenerationSupport(private val project: Project) : LightClassG
correspondingClassOrObject = classOrObject
) ?: return null
return KtLightClassForDecompiledDeclaration(javaClsClass, classOrObject)
return KtLightClassForDecompiledDeclaration(javaClsClass, classOrObject, file)
}
private fun createClsJavaClassFromVirtualFile(
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -87,7 +87,7 @@ fun PsiClass.resolveToDescriptor(
declarationTranslator: (KtClassOrObject) -> KtClassOrObject? = { it }
): ClassDescriptor? {
return if (this is KtLightClass && this !is KtLightClassForDecompiledDeclaration) {
val origin = this.getOrigin() ?: return null
val origin = this.kotlinOrigin ?: return null
val declaration = declarationTranslator(origin) ?: return null
resolutionFacade.resolveToDescriptor(declaration)
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,34 +19,32 @@ package org.jetbrains.kotlin.idea.caches.resolve
import com.intellij.psi.PsiClass
import com.intellij.psi.impl.compiled.ClsClassImpl
import org.jetbrains.kotlin.asJava.KtWrappingLightClass
import org.jetbrains.kotlin.idea.decompiler.classFile.KtClsFile
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtClassOrObject
class KtLightClassForDecompiledDeclaration(
private val clsClass: ClsClassImpl,
private val origin: KtClassOrObject?
) : KtWrappingLightClass(clsClass.manager) {
private val fqName = origin?.fqName ?: FqName(clsClass.qualifiedName)
override val clsDelegate: ClsClassImpl,
override val kotlinOrigin: KtClassOrObject?,
private val file: KtClsFile
) : KtWrappingLightClass(clsDelegate.manager) {
private val fqName = kotlinOrigin?.fqName ?: FqName(clsDelegate.qualifiedName)
override fun copy() = this
override fun getOwnInnerClasses(): List<PsiClass> {
val nestedClasses = origin?.declarations?.filterIsInstance<KtClassOrObject>() ?: emptyList()
return clsClass.ownInnerClasses.map { innerClsClass ->
val nestedClasses = kotlinOrigin?.declarations?.filterIsInstance<KtClassOrObject>() ?: emptyList()
return clsDelegate.ownInnerClasses.map { innerClsClass ->
KtLightClassForDecompiledDeclaration(innerClsClass as ClsClassImpl,
nestedClasses.firstOrNull { innerClsClass.name == it.name })
nestedClasses.firstOrNull { innerClsClass.name == it.name }, file)
}
}
override fun getNavigationElement() = origin?.navigationElement ?: super.getNavigationElement()
override fun getDelegate() = clsClass
override fun getOrigin() = origin
override fun getNavigationElement() = kotlinOrigin?.navigationElement ?: file
override fun getFqName() = fqName
override fun getParent() = clsClass.parent
override fun getParent() = clsDelegate.parent
override fun equals(other: Any?): Boolean =
other is KtLightClassForDecompiledDeclaration &&
@@ -55,3 +53,4 @@ class KtLightClassForDecompiledDeclaration(
override fun hashCode(): Int =
getFqName().hashCode()
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -130,10 +130,10 @@ private fun KtLightElement<*, *>.getModuleInfoForLightElement(onFailure: (String
false
)
}
val element = getOrigin() ?: when (this) {
val element = kotlinOrigin ?: when (this) {
is FakeLightClassForFileOfPackage -> this.getContainingFile()!!
is KtLightClassForFacade -> this.files.first()
else -> return onFailure("Light element without origin is referenced by resolve:\n$this\n${this.getDelegate().text}")
else -> return onFailure("Light element without origin is referenced by resolve:\n$this\n${this.clsDelegate.text}")
}
return element.getModuleInfo()
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,7 +39,7 @@ class KotlinReadWriteAccessDetector : ReadWriteAccessDetector() {
val refTarget = reference.resolve()
if (refTarget is KtLightMethod) {
val origin = refTarget.getOrigin()
val origin = refTarget.kotlinOrigin
val declaration: KtNamedDeclaration = when (origin) {
is KtPropertyAccessor -> origin.getNonStrictParentOfType<KtProperty>()
is KtProperty, is KtParameter -> origin as KtNamedDeclaration
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -197,7 +197,7 @@ class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, ReferencesSearc
originLightClass.fields.map { it as? KtLightField }
for (declaration in element.declarations) {
val lightDeclaration = lightDeclarations.find { it?.getOrigin() == declaration }
val lightDeclaration = lightDeclarations.find { it?.kotlinOrigin == declaration }
if (lightDeclaration != null) {
searchNamedElement(queryParameters, lightDeclaration)
}
@@ -218,7 +218,7 @@ class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, ReferencesSearc
val originClass = originObject.getStrictParentOfType<KtClass>()
val originLightClass = originClass?.toLightClass()
val allMethods = originLightClass?.allMethods
return allMethods?.find { it is KtLightMethod && it.getOrigin() == function }
return allMethods?.find { it is KtLightMethod && it.kotlinOrigin == function }
}
return null
}
@@ -275,7 +275,7 @@ class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, ReferencesSearc
}
is KtLightMethod -> {
val declaration = element.getOrigin()
val declaration = element.kotlinOrigin
if (declaration is KtProperty || (declaration is KtParameter && declaration.hasValOrVar())) {
searchNamedElement(queryParameters, declaration as PsiNamedElement)
}
@@ -292,7 +292,7 @@ class KotlinReferencesSearcher : QueryExecutorBase<PsiReference, ReferencesSearc
}
is KtLightParameter -> {
val origin = element.getOrigin() ?: return
val origin = element.kotlinOrigin ?: return
runReadAction {
val componentFunctionDescriptor = origin.dataClassComponentFunction()
if (componentFunctionDescriptor != null) {
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -134,7 +134,7 @@ private fun PsiElement.processDelegationCallKotlinConstructorUsages(scope: Searc
private fun PsiElement.processDelegationCallJavaConstructorUsages(scope: SearchScope, process: (KtCallElement) -> Boolean): Boolean {
if (this is KtLightElement<*, *>) return true
// TODO: Temporary hack to avoid NPE while KotlinNoOriginLightMethod is around
if (this is KtLightMethod && this.getOrigin() == null) return true
if (this is KtLightMethod && this.kotlinOrigin == null) return true
if (!(this is PsiMethod && isConstructor)) return true
val klass = containingClass ?: return true
val descriptor = getJavaMethodDescriptor() as? ConstructorDescriptor ?: return true