Remove redundant fake facade classes
Fixed #KT-35122 #EA-218642 #EA-217640
This commit is contained in:
@@ -173,7 +173,6 @@ object LightClassUtil {
|
|||||||
val classesWithMatchingFqName = JavaElementFinder.getInstance(project).findClasses(fqName.asString(), GlobalSearchScope.allScope(project))
|
val classesWithMatchingFqName = JavaElementFinder.getInstance(project).findClasses(fqName.asString(), GlobalSearchScope.allScope(project))
|
||||||
return classesWithMatchingFqName.singleOrNull() ?:
|
return classesWithMatchingFqName.singleOrNull() ?:
|
||||||
classesWithMatchingFqName.find {
|
classesWithMatchingFqName.find {
|
||||||
// NOTE: for multipart facades this works via FakeLightClassForFileOfPackage
|
|
||||||
it.containingFile?.virtualFile == ktFile.virtualFile
|
it.containingFile?.virtualFile == ktFile.virtualFile
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
-123
@@ -1,123 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.jetbrains.kotlin.asJava.classes;
|
|
||||||
|
|
||||||
import com.intellij.lang.Language;
|
|
||||||
import com.intellij.psi.PsiClass;
|
|
||||||
import com.intellij.psi.PsiElement;
|
|
||||||
import com.intellij.psi.PsiFile;
|
|
||||||
import com.intellij.psi.impl.light.AbstractLightClass;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
import org.jetbrains.kotlin.asJava.elements.KtLightAbstractAnnotation;
|
|
||||||
import org.jetbrains.kotlin.asJava.finder.JavaElementFinder;
|
|
||||||
import org.jetbrains.kotlin.idea.KotlinLanguage;
|
|
||||||
import org.jetbrains.kotlin.load.java.structure.LightClassOriginKind;
|
|
||||||
import org.jetbrains.kotlin.psi.KtClassOrObject;
|
|
||||||
import org.jetbrains.kotlin.psi.KtFile;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class serves as a workaround for usages of {@link JavaElementFinder#findClasses} which eventually only need names of files
|
|
||||||
* containing the class. When queried for a package class (e.g. test/TestPackage), {@code findClasses} along with a
|
|
||||||
* {@link KtLightClassForFacade} would also return multiple instances of this class for each file present in the package. The client
|
|
||||||
* code can make use of every file in the package then, since {@code getContainingFile} of these instances will represent the whole package.
|
|
||||||
* <p/>
|
|
||||||
* See {@link LineBreakpoint#findClassCandidatesInSourceContent} for the primary usage this was introduced
|
|
||||||
*/
|
|
||||||
public class FakeLightClassForFileOfPackage extends AbstractLightClass implements KtLightClass {
|
|
||||||
private final KtLightClassForFacade delegate;
|
|
||||||
private final KtFile file;
|
|
||||||
|
|
||||||
public FakeLightClassForFileOfPackage(@NotNull KtLightClassForFacade delegate, @NotNull KtFile file) {
|
|
||||||
super(delegate.getManager());
|
|
||||||
this.delegate = delegate;
|
|
||||||
this.file = file;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public PsiClass getClsDelegate() {
|
|
||||||
return delegate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public KtClassOrObject getKotlinOrigin() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PsiFile getContainingFile() {
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isValid() {
|
|
||||||
// This is intentionally false to prevent using this as a real class
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public PsiClass getDelegate() {
|
|
||||||
return delegate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public PsiElement copy() {
|
|
||||||
return new FakeLightClassForFileOfPackage(delegate, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getText() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public Language getLanguage() {
|
|
||||||
return KotlinLanguage.INSTANCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (!(obj instanceof FakeLightClassForFileOfPackage)) return false;
|
|
||||||
|
|
||||||
FakeLightClassForFileOfPackage other = (FakeLightClassForFileOfPackage) obj;
|
|
||||||
return file == other.file && delegate.equals(other.delegate);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return file.hashCode() * 31 + delegate.hashCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public LightClassOriginKind getOriginKind() {
|
|
||||||
return LightClassOriginKind.SOURCE;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
@Override
|
|
||||||
public List<KtLightAbstractAnnotation> getGivenAnnotations() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+16
-17
@@ -21,12 +21,12 @@ import com.intellij.openapi.extensions.Extensions
|
|||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.util.Condition
|
import com.intellij.openapi.util.Condition
|
||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
|
import com.intellij.psi.impl.compiled.ClsClassImpl
|
||||||
import com.intellij.psi.search.GlobalSearchScope
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
import com.intellij.psi.util.PsiUtilCore
|
import com.intellij.psi.util.PsiUtilCore
|
||||||
import com.intellij.util.SmartList
|
import com.intellij.util.SmartList
|
||||||
import com.intellij.util.containers.ContainerUtil
|
import com.intellij.util.containers.ContainerUtil
|
||||||
import org.jetbrains.kotlin.asJava.KotlinAsJavaSupport
|
import org.jetbrains.kotlin.asJava.KotlinAsJavaSupport
|
||||||
import org.jetbrains.kotlin.asJava.classes.FakeLightClassForFileOfPackage
|
|
||||||
import org.jetbrains.kotlin.asJava.toLightClass
|
import org.jetbrains.kotlin.asJava.toLightClass
|
||||||
import org.jetbrains.kotlin.load.java.JvmAbi
|
import org.jetbrains.kotlin.load.java.JvmAbi
|
||||||
import org.jetbrains.kotlin.name.FqName
|
import org.jetbrains.kotlin.name.FqName
|
||||||
@@ -58,7 +58,9 @@ class JavaElementFinder(
|
|||||||
answer.addAll(kotlinAsJavaSupport.getFacadeClasses(qualifiedName, scope))
|
answer.addAll(kotlinAsJavaSupport.getFacadeClasses(qualifiedName, scope))
|
||||||
answer.addAll(kotlinAsJavaSupport.getKotlinInternalClasses(qualifiedName, scope))
|
answer.addAll(kotlinAsJavaSupport.getKotlinInternalClasses(qualifiedName, scope))
|
||||||
|
|
||||||
return answer.sortByClasspathPreferringNonFakeFiles(scope).toTypedArray()
|
sortByPreferenceToSourceFile(answer, scope)
|
||||||
|
|
||||||
|
return answer.toTypedArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finds explicitly declared classes and objects, not package classes
|
// Finds explicitly declared classes and objects, not package classes
|
||||||
@@ -141,15 +143,22 @@ class JavaElementFinder(
|
|||||||
answer.add(aClass)
|
answer.add(aClass)
|
||||||
}
|
}
|
||||||
|
|
||||||
return answer.sortByClasspathPreferringNonFakeFiles(scope).toTypedArray()
|
sortByPreferenceToSourceFile(answer, scope)
|
||||||
|
|
||||||
|
return answer.toTypedArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getPackageFiles(psiPackage: PsiPackage, scope: GlobalSearchScope): Array<PsiFile> {
|
private fun sortByPreferenceToSourceFile(list: SmartList<PsiClass>, searchScope: GlobalSearchScope) {
|
||||||
val packageFQN = FqName(psiPackage.qualifiedName)
|
if (list.size < 2) return
|
||||||
// TODO: this does not take into account JvmPackageName annotation
|
// NOTE: this comparator might violate the contract depending on the scope passed
|
||||||
return kotlinAsJavaSupport.findFilesForPackage(packageFQN, scope).toTypedArray()
|
ContainerUtil.quickSort(list, byClasspathComparator(searchScope))
|
||||||
|
list.sortBy { it !is ClsClassImpl }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: this does not take into account JvmPackageName annotation
|
||||||
|
override fun getPackageFiles(psiPackage: PsiPackage, scope: GlobalSearchScope): Array<PsiFile> =
|
||||||
|
kotlinAsJavaSupport.findFilesForPackage(FqName(psiPackage.qualifiedName), scope).toTypedArray()
|
||||||
|
|
||||||
override fun getPackageFilesFilter(psiPackage: PsiPackage, scope: GlobalSearchScope): Condition<PsiFile>? {
|
override fun getPackageFilesFilter(psiPackage: PsiPackage, scope: GlobalSearchScope): Condition<PsiFile>? {
|
||||||
return Condition { input ->
|
return Condition { input ->
|
||||||
if (input !is KtFile) {
|
if (input !is KtFile) {
|
||||||
@@ -184,15 +193,5 @@ class JavaElementFinder(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun List<PsiClass>.sortByClasspathPreferringNonFakeFiles(searchScope: GlobalSearchScope): List<PsiClass> {
|
|
||||||
val result = this.toMutableList()
|
|
||||||
// NOTE: this comparator might violate the contract depending on the scope passed
|
|
||||||
ContainerUtil.quickSort(result, byClasspathComparator(searchScope))
|
|
||||||
result.sortBy {
|
|
||||||
it is FakeLightClassForFileOfPackage
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import com.intellij.openapi.vfs.VirtualFile
|
|||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
import org.jetbrains.kotlin.analyzer.ModuleInfo
|
import org.jetbrains.kotlin.analyzer.ModuleInfo
|
||||||
import org.jetbrains.kotlin.asJava.classes.FakeLightClassForFileOfPackage
|
|
||||||
import org.jetbrains.kotlin.asJava.classes.KtLightClassForFacade
|
import org.jetbrains.kotlin.asJava.classes.KtLightClassForFacade
|
||||||
import org.jetbrains.kotlin.asJava.elements.KtLightElement
|
import org.jetbrains.kotlin.asJava.elements.KtLightElement
|
||||||
import org.jetbrains.kotlin.caches.project.cacheInvalidatingOnRootModifications
|
import org.jetbrains.kotlin.caches.project.cacheInvalidatingOnRootModifications
|
||||||
@@ -214,7 +213,6 @@ private fun <T> KtLightElement<*, *>.processLightElement(c: ModuleInfoCollector<
|
|||||||
}
|
}
|
||||||
|
|
||||||
val element = kotlinOrigin ?: when (this) {
|
val element = kotlinOrigin ?: when (this) {
|
||||||
is FakeLightClassForFileOfPackage -> this.getContainingFile()!!
|
|
||||||
is KtLightClassForFacade -> this.files.first()
|
is KtLightClassForFacade -> this.files.first()
|
||||||
else -> return c.onFailure("Light element without origin is referenced by resolve:\n$this\n${this.clsDelegate.text}")
|
else -> return c.onFailure("Light element without origin is referenced by resolve:\n$this\n${this.clsDelegate.text}")
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-30
@@ -12,6 +12,7 @@ import com.intellij.psi.impl.compiled.ClsClassImpl
|
|||||||
import com.intellij.psi.impl.compiled.ClsFileImpl
|
import com.intellij.psi.impl.compiled.ClsFileImpl
|
||||||
import com.intellij.psi.search.GlobalSearchScope
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
import com.intellij.psi.util.PsiTreeUtil
|
import com.intellij.psi.util.PsiTreeUtil
|
||||||
|
import com.intellij.util.SmartList
|
||||||
import org.jetbrains.kotlin.asJava.KotlinAsJavaSupport
|
import org.jetbrains.kotlin.asJava.KotlinAsJavaSupport
|
||||||
import org.jetbrains.kotlin.asJava.builder.ClsWrapperStubPsiFactory
|
import org.jetbrains.kotlin.asJava.builder.ClsWrapperStubPsiFactory
|
||||||
import org.jetbrains.kotlin.asJava.classes.*
|
import org.jetbrains.kotlin.asJava.classes.*
|
||||||
@@ -34,7 +35,6 @@ import org.jetbrains.kotlin.platform.jvm.isJvm
|
|||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||||
import org.jetbrains.kotlin.utils.sure
|
import org.jetbrains.kotlin.utils.sure
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
class IDEKotlinAsJavaSupport(private val project: Project) : KotlinAsJavaSupport() {
|
class IDEKotlinAsJavaSupport(private val project: Project) : KotlinAsJavaSupport() {
|
||||||
private val psiManager: PsiManager = PsiManager.getInstance(project)
|
private val psiManager: PsiManager = PsiManager.getInstance(project)
|
||||||
@@ -151,19 +151,6 @@ class IDEKotlinAsJavaSupport(private val project: Project) : KotlinAsJavaSupport
|
|||||||
return KtLightClassForScript.create(script)
|
return KtLightClassForScript.create(script)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun withFakeLightClasses(
|
|
||||||
lightClassForFacade: KtLightClassForFacade
|
|
||||||
): List<PsiClass> {
|
|
||||||
val lightClasses = ArrayList<PsiClass>()
|
|
||||||
lightClasses.add(lightClassForFacade)
|
|
||||||
if (lightClassForFacade.files.size > 1) {
|
|
||||||
lightClasses.addAll(lightClassForFacade.files.map {
|
|
||||||
FakeLightClassForFileOfPackage(lightClassForFacade, it)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return lightClasses
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getFacadeClasses(facadeFqName: FqName, scope: GlobalSearchScope): Collection<PsiClass> {
|
override fun getFacadeClasses(facadeFqName: FqName, scope: GlobalSearchScope): Collection<PsiClass> {
|
||||||
val filesByModule = findFilesForFacade(facadeFqName, scope).groupBy(PsiElement::getModuleInfoPreferringJvmPlatform)
|
val filesByModule = findFilesForFacade(facadeFqName, scope).groupBy(PsiElement::getModuleInfoPreferringJvmPlatform)
|
||||||
|
|
||||||
@@ -215,24 +202,20 @@ class IDEKotlinAsJavaSupport(private val project: Project) : KotlinAsJavaSupport
|
|||||||
facadeFqName: FqName,
|
facadeFqName: FqName,
|
||||||
facadeFiles: List<KtFile>,
|
facadeFiles: List<KtFile>,
|
||||||
moduleInfo: IdeaModuleInfo
|
moduleInfo: IdeaModuleInfo
|
||||||
): List<PsiClass> {
|
): List<PsiClass> = SmartList<PsiClass>().apply {
|
||||||
val (clsFiles, _) = facadeFiles.partition { it is KtClsFile }
|
|
||||||
val facadesFromCls = clsFiles.mapNotNull { createLightClassForDecompiledKotlinFile(it as KtClsFile) }
|
tryCreateFacadesForSourceFiles(moduleInfo, facadeFqName)?.let { sourcesFacade ->
|
||||||
val facadesFromSources = createFacadesForSourceFiles(moduleInfo, facadeFqName)
|
add(sourcesFacade)
|
||||||
return facadesFromSources + facadesFromCls
|
}
|
||||||
|
|
||||||
|
facadeFiles.filterIsInstance<KtClsFile>().mapNotNullTo(this) {
|
||||||
|
createLightClassForDecompiledKotlinFile(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createFacadesForSourceFiles(
|
private fun tryCreateFacadesForSourceFiles(moduleInfo: IdeaModuleInfo, facadeFqName: FqName): PsiClass? {
|
||||||
moduleInfo: IdeaModuleInfo,
|
if (moduleInfo !is ModuleSourceInfo && moduleInfo !is PlatformModuleInfo) return null
|
||||||
facadeFqName: FqName
|
return KtLightClassForFacade.createForFacade(psiManager, facadeFqName, moduleInfo.contentScope())
|
||||||
): List<PsiClass> {
|
|
||||||
if (moduleInfo !is ModuleSourceInfo && moduleInfo !is PlatformModuleInfo) return listOf()
|
|
||||||
|
|
||||||
val lightClassForFacade = KtLightClassForFacade.createForFacade(
|
|
||||||
psiManager, facadeFqName, moduleInfo.contentScope()
|
|
||||||
)
|
|
||||||
|
|
||||||
return if (lightClassForFacade !== null) withFakeLightClasses(lightClassForFacade) else emptyList()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun findFilesForFacade(facadeFqName: FqName, scope: GlobalSearchScope): Collection<KtFile> {
|
override fun findFilesForFacade(facadeFqName: FqName, scope: GlobalSearchScope): Collection<KtFile> {
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ class LightClassesClasspathSortingTest : KotlinLightCodeInsightFixtureTestCase()
|
|||||||
val psiClass = JavaPsiFacade.getInstance(project).findClass(fqName, ResolveScopeManager.getElementResolveScope(file))
|
val psiClass = JavaPsiFacade.getInstance(project).findClass(fqName, ResolveScopeManager.getElementResolveScope(file))
|
||||||
|
|
||||||
assertNotNull(psiClass, "Can't find class for $fqName")
|
assertNotNull(psiClass, "Can't find class for $fqName")
|
||||||
psiClass!!
|
|
||||||
assert(psiClass is KtLightClassForSourceDeclaration || psiClass is KtLightClassForFacade) { "Should be an explicit light class, but was $fqName ${psiClass::class.java}" }
|
assert(psiClass is KtLightClassForSourceDeclaration || psiClass is KtLightClassForFacade) { "Should be an explicit light class, but was $fqName ${psiClass::class.java}" }
|
||||||
assert(psiClass !is KtLightClassForDecompiledDeclaration) { "Should not be decompiled light class: $fqName ${psiClass::class.java}" }
|
assert(psiClass !is KtLightClassForDecompiledDeclaration) { "Should not be decompiled light class: $fqName ${psiClass::class.java}" }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user