diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/getModuleInfo.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/getModuleInfo.kt index 80e3100ae13..2973b84d9d0 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/getModuleInfo.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/resolve/getModuleInfo.kt @@ -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(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()!! diff --git a/idea/tests/org/jetbrains/kotlin/idea/caches/resolve/CustomModuleInfoTest.kt b/idea/tests/org/jetbrains/kotlin/idea/caches/resolve/CustomModuleInfoTest.kt new file mode 100644 index 00000000000..07c210b5a11 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/caches/resolve/CustomModuleInfoTest.kt @@ -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()) + } + } +}