[java] check library sources for module-info.java if element is not from library classes (IDEA-281019)

GitOrigin-RevId: ace08bb17933f65ef9f9010aaba723fef8b0906a
This commit is contained in:
Anna Kozlova
2021-10-27 13:39:56 +02:00
committed by intellij-monorepo-bot
parent a471e820c2
commit ac69d18933
6 changed files with 25 additions and 5 deletions

View File

@@ -88,6 +88,16 @@ public final class JavaModuleGraphUtil {
});
}
}
else {
root = index.getSourceRootForFile(file);
if (root != null) {
VirtualFile moduleDescriptor = root.findChild(PsiJavaModule.MODULE_INFO_FILE);
PsiFile psiFile = moduleDescriptor != null ? PsiManager.getInstance(project).findFile(moduleDescriptor) : null;
if (psiFile instanceof PsiJavaFile) {
return ((PsiJavaFile)psiFile).getModuleDeclaration();
}
}
}
return null;
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.codeInsight.completion
import com.intellij.java.testFramework.fixtures.LightJava9ModulesCodeInsightFixtureTestCase
@@ -39,12 +39,12 @@ class ModuleCompletionTest : LightJava9ModulesCodeInsightFixtureTestCase() {
fun testRequiresBare() =
variants("module M { requires <caret>",
"transitive", "static", "M2", "java.base", "java.non.root", "java.se", "java.xml.bind", "java.xml.ws",
"lib.multi.release", "lib.named", "lib.auto", "lib.claimed", "all.fours")
"lib.multi.release", "lib.named", "lib.auto", "lib.claimed", "all.fours", "lib.with.module.info")
fun testRequiresTransitive() = complete("module M { requires tr<caret> }", "module M { requires transitive <caret> }")
@NeedsIndex.Full
fun testRequiresSimpleName() = complete("module M { requires M<caret> }", "module M { requires M2;<caret> }")
@NeedsIndex.ForStandardLibrary
fun testRequiresQualifiedName() = complete("module M { requires lib.m<caret> }", "module M { requires lib.multi.release;<caret> }")
fun testRequiresQualifiedName() = complete("module M { requires lib.mult<caret> }", "module M { requires lib.multi.release;<caret> }")
fun testExportsBare() = variants("module M { exports <caret> }", "pkg")
fun testExportsPrefixed() = complete("module M { exports p<caret> }", "module M { exports pkg.<caret> }")
@@ -54,7 +54,7 @@ class ModuleCompletionTest : LightJava9ModulesCodeInsightFixtureTestCase() {
@NeedsIndex.Full
fun testExportsToList() =
variants("module M { exports pkg.other to <caret> }",
"M2", "java.base", "java.non.root", "java.se", "java.xml.bind", "java.xml.ws", "lib.multi.release", "lib.named")
"M2", "java.base", "java.non.root", "java.se", "java.xml.bind", "java.xml.ws", "lib.multi.release", "lib.named", "lib.with.module.info")
@NeedsIndex.Full
fun testExportsToUnambiguous() = complete("module M { exports pkg.other to M<caret> }", "module M { exports pkg.other to M2<caret> }")

View File

@@ -12,8 +12,11 @@ import com.intellij.java.testFramework.fixtures.MultiModuleJava9ProjectDescripto
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiManager
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.ProjectScope
import com.intellij.psi.util.PsiUtilCore
import org.assertj.core.api.Assertions.assertThat
import org.junit.Assert
import java.util.jar.JarFile
class ModuleHighlightingTest : LightJava9ModulesCodeInsightFixtureTestCase() {
@@ -495,6 +498,12 @@ class ModuleHighlightingTest : LightJava9ModulesCodeInsightFixtureTestCase() {
assertNotSame(libModule, JavaModuleGraphUtil.findDescriptorByElement(libClass)!!)
}
fun testModuleInSources() {
val classInLibrary = myFixture.javaFacade.findClass("lib.named.C", GlobalSearchScope.allScope(project))!!
val elementInSources = classInLibrary.navigationElement
Assert.assertNotNull(JavaModuleGraphUtil.findDescriptorByFile (PsiUtilCore.getVirtualFile(elementInSources), project))
}
//<editor-fold desc="Helpers.">
private fun highlight(text: String) = highlight("module-info.java", text)

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.testFramework.fixtures
import com.intellij.openapi.application.ex.PathManagerEx
@@ -94,6 +94,7 @@ object MultiModuleJava9ProjectDescriptor : DefaultLightProjectDescriptor() {
ModuleRootModificationUtil.addModuleLibrary(m2, "${libDir}/lib-auto-1.0.jar!/")
ModuleRootModificationUtil.addModuleLibrary(m4, "${libDir}/lib-auto-2.0.jar!/")
ModuleRootModificationUtil.addModuleLibrary(m6, "${libDir}/lib-named-2.0.jar!/")
ModuleRootModificationUtil.addModuleLibrary(m8, "lib-with-module-info", listOf("${libDir}/lib-with-module-info.jar!/"), listOf("${libDir}/lib-with-module-info-sources.zip!/src"))
}
}