getModuleInfo: Correct module info for members of light classes for decompiled Kotlin classes

This commit is contained in:
Pavel V. Talanov
2015-12-01 12:45:19 +03:00
parent dc60c025c3
commit 5e35be347c
2 changed files with 48 additions and 2 deletions
@@ -28,6 +28,8 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.roots.ModuleRootManager
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.utils.sure
fun PsiElement.getModuleInfo(): IdeaModuleInfo {
fun logAndReturnDefault(message: String): IdeaModuleInfo {
@@ -115,8 +117,13 @@ private fun getModuleInfoByVirtualFile(project: Project, virtualFile: VirtualFil
}
private fun KtLightElement<*, *>.getModuleInfoForLightElement(): IdeaModuleInfo {
if (this is KtLightClassForDecompiledDeclaration) {
return getModuleInfoByVirtualFile(getProject(), getContainingFile().getVirtualFile(), false)
val decompiledClass = this.getParentOfType<KtLightClassForDecompiledDeclaration>(strict = false)
if (decompiledClass != null) {
return getModuleInfoByVirtualFile(
project,
containingFile.virtualFile.sure { "Decompiled class should be build from physical file" },
false
)
}
val element = getOrigin() ?: when (this) {
is FakeLightClassForFileOfPackage -> this.getContainingFile()!!
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2015 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.idea.caches.resolve
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.idea.test.KotlinLightCodeInsightFixtureTestCase
import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor
import org.junit.Assert
public class CustomModuleInfoTest : KotlinLightCodeInsightFixtureTestCase() {
override fun getProjectDescriptor() = KotlinWithJdkAndRuntimeLightProjectDescriptor.INSTANCE
fun testModuleInfoForMembersOfLightClassForDecompiledFile() {
//NOTE: any class with methods from stdlib will do
val collectionsKtClass = JavaPsiFacade.getInstance(project).findClass("kotlin.CollectionsKt", GlobalSearchScope.allScope(project))!!
val classModuleInfo = collectionsKtClass.getModuleInfo()
Assert.assertTrue(classModuleInfo is LibraryInfo)
val methods = collectionsKtClass.methods
Assert.assertTrue(methods.isNotEmpty())
methods.forEach {
Assert.assertEquals("Members of decompiled class should have the same module info", classModuleInfo, it.getModuleInfo())
}
}
}