[java] excludes module-info files in library sources from accessibility checks (IDEA-187890)

This fixes the issue with duplicated results in multi-resolve, but causes (false) package accessibility errors in library sources. To compensate, JPMS implementation skips library sources from these checks (it doesn't make much sense anyway).
This commit is contained in:
Roman Shevchenko
2018-04-24 19:44:06 +02:00
parent f0beeeed03
commit 04326806fd
3 changed files with 33 additions and 38 deletions
@@ -58,8 +58,8 @@ public class ModuleHighlightUtil {
ProjectFileIndex index = ProjectFileIndex.SERVICE.getInstance(project);
if (index.isInLibrary(file)) {
VirtualFile root;
if ((root = index.getClassRootForFile(file)) != null) {
VirtualFile root = index.getClassRootForFile(file);
if (root != null) {
VirtualFile descriptorFile = root.findChild(PsiJavaModule.MODULE_INFO_CLS_FILE);
if (descriptorFile == null) {
VirtualFile alt = root.findFileByRelativePath("META-INF/versions/9/" + PsiJavaModule.MODULE_INFO_CLS_FILE);
@@ -77,15 +77,6 @@ public class ModuleHighlightUtil {
return LightJavaModule.getModule(PsiManager.getInstance(project), root);
}
}
else if ((root = index.getSourceRootForFile(file)) != null) {
VirtualFile descriptorFile = root.findChild(PsiJavaModule.MODULE_INFO_FILE);
if (descriptorFile != null) {
PsiFile psiFile = PsiManager.getInstance(project).findFile(descriptorFile);
if (psiFile instanceof PsiJavaFile) {
return ((PsiJavaFile)psiFile).getModuleDeclaration();
}
}
}
}
else {
Module module = index.getModuleForFile(file);
@@ -38,16 +38,15 @@ class JavaPlatformModuleSystem : JavaModuleSystemEx {
private fun checkAccess(targetPackageName: String, targetFile: PsiFile?, place: PsiElement, quick: Boolean): ErrorWithFixes? {
val useFile = place.containingFile?.originalFile
if (useFile != null && PsiUtil.isLanguageLevel9OrHigher(useFile)) {
if (targetFile != null && targetFile.isPhysical) {
return checkAccess(targetFile, useFile, targetPackageName, quick)
}
else {
val project = useFile.project
val target = JavaPsiFacade.getInstance(project).findPackage(targetPackageName)
if (target != null) {
val useVFile = useFile.virtualFile
if (useVFile != null) {
val index = ProjectFileIndex.getInstance(useFile.project)
val useVFile = useFile.virtualFile
val index = ProjectFileIndex.getInstance(useFile.project)
if (useVFile == null || !index.isInLibrarySource(useVFile)) {
if (targetFile != null && targetFile.isPhysical) {
return checkAccess(targetFile, useFile, targetPackageName, quick)
}
else if (useVFile != null) {
val target = JavaPsiFacade.getInstance(useFile.project).findPackage(targetPackageName)
if (target != null) {
val module = index.getModuleForFile(useVFile)
if (module != null) {
val test = index.isInTestSourceContent(useVFile)
@@ -74,8 +73,8 @@ class JavaPlatformModuleSystem : JavaModuleSystemEx {
private val ERR = ErrorWithFixes("-")
private fun checkAccess(target: PsiFileSystemItem, place: PsiFileSystemItem, packageName: String, quick: Boolean): ErrorWithFixes? {
val targetModule = JavaModuleGraphUtil.findDescriptorByElement(target)?.originalElement as PsiJavaModule?
val useModule = JavaModuleGraphUtil.findDescriptorByElement(place)?.originalElement as PsiJavaModule?
val targetModule = JavaModuleGraphUtil.findDescriptorByElement(target)
val useModule = JavaModuleGraphUtil.findDescriptorByElement(place)
if (targetModule != null) {
if (targetModule == useModule) {
@@ -1,18 +1,4 @@
/*
* Copyright 2000-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.
*/
// 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.
package com.intellij.psi.impl.file.impl;
import com.intellij.ProjectTopics;
@@ -32,6 +18,7 @@ import com.intellij.psi.impl.java.stubs.index.JavaAutoModuleNameIndex;
import com.intellij.psi.impl.java.stubs.index.JavaFullClassNameIndex;
import com.intellij.psi.impl.java.stubs.index.JavaModuleNameIndex;
import com.intellij.psi.impl.light.LightJavaModule;
import com.intellij.psi.search.DelegatingGlobalSearchScope;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.Query;
import com.intellij.util.containers.ContainerUtil;
@@ -42,6 +29,8 @@ import org.jetbrains.jps.model.java.JavaModuleSourceRootTypes;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.Objects.requireNonNull;
/**
* @author dmitry lomov
*/
@@ -183,6 +172,8 @@ public class JavaFileManagerImpl implements JavaFileManager, Disposable {
@NotNull
@Override
public Collection<PsiJavaModule> findModules(@NotNull String moduleName, @NotNull GlobalSearchScope scope) {
scope = new LibSrcExcludingScope(scope);
Collection<PsiJavaModule> named = JavaModuleNameIndex.getInstance().get(moduleName, myManager.getProject(), scope);
if (!named.isEmpty()) {
return named;
@@ -198,4 +189,18 @@ public class JavaFileManagerImpl implements JavaFileManager, Disposable {
return Collections.emptyList();
}
private static class LibSrcExcludingScope extends DelegatingGlobalSearchScope {
private final ProjectFileIndex myIndex;
private LibSrcExcludingScope(@NotNull GlobalSearchScope baseScope) {
super(baseScope);
myIndex = ProjectFileIndex.getInstance(requireNonNull(baseScope.getProject()));
}
@Override
public boolean contains(@NotNull VirtualFile file) {
return super.contains(file) && !myIndex.isInLibrarySource(file);
}
}
}