Java: Store Java modules in RefJavaManager for the inspection "Unused 'requires' statement in module-info" (IDEA-163139)

This commit is contained in:
Pavel Dolgov
2016-11-22 14:10:31 +03:00
parent 24e6a97f60
commit ea457179df
4 changed files with 36 additions and 2 deletions
@@ -54,6 +54,8 @@ public abstract class RefJavaManager implements RefManagerExtension<RefJavaManag
*/
public abstract RefPackage getPackage(String packageName);
public abstract RefEntity getRefJavaModule(String name);
/**
* Creates (if necessary) and returns the reference graph node for the specified PSI parameter.
*
@@ -121,9 +121,12 @@ public class Java9UnusedRequiresStatementInspection extends GlobalJavaBatchInspe
@Nullable
private static JavaModuleInfo getJavaModuleInfo(@NotNull RefModule refModule, boolean addIfMissing) {
Optional<JavaModuleInfo> optionalInfo = refModule.getUserData(JAVA_MODULE_INFO);
if (!addIfMissing) {
return optionalInfo != null ? optionalInfo.orElse(null) : null;
}
if (optionalInfo == null) {
optionalInfo = Optional
.ofNullable(addIfMissing ? refModule : null)
.of(refModule)
.map(RefModule::getModule)
.map(JavaModuleGraphUtil::findDescriptorByModule)
.map(JavaModuleInfo::new);
@@ -242,7 +245,7 @@ public class Java9UnusedRequiresStatementInspection extends GlobalJavaBatchInspe
}
}
private static class JavaModuleInfo {
private static class JavaModuleInfo { // TODO: remove this
private final PsiJavaModule javaModule;
private final Set<String> importedPackageNames;
@@ -28,6 +28,7 @@ import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.impl.file.impl.JavaFileManager;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocTag;
import com.intellij.psi.search.GlobalSearchScope;
@@ -40,6 +41,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.Collection;
/**
@@ -55,6 +57,7 @@ public class RefJavaManagerImpl extends RefJavaManager {
private PsiClass myServlet;
private RefPackage myDefaultPackage;
private THashMap<String, RefPackage> myPackages;
private THashMap<String, RefJavaModule> myJavaModules;
private final RefManagerImpl myRefManager;
private PsiElementVisitor myProjectIterator;
private EntryPointsManager myEntryPointsManager;
@@ -109,6 +112,24 @@ public class RefJavaManagerImpl extends RefJavaManager {
return refPackage;
}
@Override
public RefEntity getRefJavaModule(String name) {
if (myJavaModules == null) {
myJavaModules = new THashMap<>();
}
RefJavaModule refJavaModule = myJavaModules.get(name);
if (refJavaModule == null) {
Project project = myRefManager.getProject();
GlobalSearchScope scope = GlobalSearchScope.projectScope(project);
Collection<PsiJavaModule> javaModules = JavaFileManager.SERVICE.getInstance(project).findModules(name, scope);
if (javaModules.size() == 1) {
PsiJavaModule javaModule = javaModules.iterator().next();
refJavaModule = new RefJavaModuleImpl(javaModule, myRefManager);
myJavaModules.put(name, refJavaModule);
}
}
return refJavaModule;
}
public boolean isEntryPoint(final RefElement element) {
UnusedDeclarationInspectionBase tool = getDeadCodeTool(element);
@@ -279,6 +300,9 @@ public class RefJavaManagerImpl extends RefJavaManager {
else if (PACKAGE.equals(type)) {
return RefPackageImpl.packageFromFQName(myRefManager, fqName);
}
else if (JAVA_MODULE.equals(type)) {
return RefJavaModuleImpl.javaModuleFromExternalName(myRefManager, fqName);
}
return null;
}
@@ -117,6 +117,7 @@ public class RefJavaModuleImpl extends RefElementImpl implements RefJavaModule {
}
}
}
((RefModuleImpl)myRefModule).add(this);
getRefManager().fireBuildReferences(this);
}
}
@@ -139,4 +140,8 @@ public class RefJavaModuleImpl extends RefElementImpl implements RefJavaModule {
}
return resolvedElements.size() == 1 ? resolvedElements.get(0) : null;
}
static RefEntity javaModuleFromExternalName(RefManagerImpl manager, String name) {
return manager.getExtension(RefJavaManager.MANAGER).getRefJavaModule(name);
}
}