use of com.intellij.util.containers.MultiMap in project intellij-community by JetBrains.
the class JavaModuleGraphUtil method findCycles.
// Looks for cycles between Java modules in the project sources.
// Library/JDK modules are excluded - in assumption there can't be any lib -> src dependencies.
// Module references are resolved "globally" (i.e., without taking project dependencies into account).
private static List<Set<PsiJavaModule>> findCycles(Project project) {
Set<PsiJavaModule> projectModules = ContainerUtil.newHashSet();
for (Module module : ModuleManager.getInstance(project).getModules()) {
Collection<VirtualFile> files = FilenameIndex.getVirtualFilesByName(project, MODULE_INFO_FILE, module.getModuleScope(false));
// aborts the process when there are incorrect modules in the project
if (files.size() > 1)
return Collections.emptyList();
Optional.ofNullable(ContainerUtil.getFirstItem(files)).map(PsiManager.getInstance(project)::findFile).map(f -> f instanceof PsiJavaFile ? ((PsiJavaFile) f).getModuleDeclaration() : null).ifPresent(projectModules::add);
}
if (!projectModules.isEmpty()) {
MultiMap<PsiJavaModule, PsiJavaModule> relations = MultiMap.create();
for (PsiJavaModule module : projectModules) {
for (PsiRequiresStatement statement : module.getRequires()) {
PsiJavaModule dependency = PsiJavaModuleReference.resolve(statement, statement.getModuleName(), true);
if (dependency != null && projectModules.contains(dependency)) {
relations.putValue(module, dependency);
}
}
}
if (!relations.isEmpty()) {
Graph<PsiJavaModule> graph = new ChameleonGraph<>(relations, false);
DFSTBuilder<PsiJavaModule> builder = new DFSTBuilder<>(graph);
Collection<Collection<PsiJavaModule>> components = builder.getComponents();
if (!components.isEmpty()) {
return components.stream().map(ContainerUtil::newLinkedHashSet).collect(Collectors.toList());
}
}
}
return Collections.emptyList();
}
use of com.intellij.util.containers.MultiMap in project intellij-community by JetBrains.
the class JavaModuleGraphUtil method buildRequiresGraph.
// Starting from source modules, collects all module dependencies in the project.
// The resulting graph is used for tracing readability and checking package conflicts.
private static RequiresGraph buildRequiresGraph(Project project) {
MultiMap<PsiJavaModule, PsiJavaModule> relations = MultiMap.create();
Set<String> transitiveEdges = ContainerUtil.newTroveSet();
for (Module module : ModuleManager.getInstance(project).getModules()) {
Collection<VirtualFile> files = FilenameIndex.getVirtualFilesByName(project, MODULE_INFO_FILE, module.getModuleScope(false));
Optional.ofNullable(ContainerUtil.getFirstItem(files)).map(PsiManager.getInstance(project)::findFile).map(f -> f instanceof PsiJavaFile ? ((PsiJavaFile) f).getModuleDeclaration() : null).ifPresent(m -> visit(m, relations, transitiveEdges));
}
Graph<PsiJavaModule> graph = GraphGenerator.generate(new ChameleonGraph<>(relations, true));
return new RequiresGraph(graph, transitiveEdges);
}
use of com.intellij.util.containers.MultiMap in project intellij-community by JetBrains.
the class ExtractMethodObjectDialog method doOKAction.
@Override
protected void doOKAction() {
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
if (myCreateInnerClassRb.isSelected()) {
final PsiClass innerClass = myTargetClass.findInnerClassByName(myInnerClassName.getText(), false);
if (innerClass != null) {
conflicts.putValue(innerClass, "Inner class " + myInnerClassName.getText() + " already defined in class " + myTargetClass.getName());
}
}
if (conflicts.size() > 0) {
final ConflictsDialog conflictsDialog = new ConflictsDialog(myProject, conflicts);
if (!conflictsDialog.showAndGet()) {
if (conflictsDialog.isShowConflicts())
close(CANCEL_EXIT_CODE);
return;
}
}
final JCheckBox makeVarargsCb = myCreateInnerClassRb.isSelected() ? myCbMakeVarargs : myCbMakeVarargsAnonymous;
if (makeVarargsCb != null && makeVarargsCb.isSelected()) {
final VariableData data = myInputVariables[myInputVariables.length - 1];
if (data.type instanceof PsiArrayType) {
data.type = new PsiEllipsisType(((PsiArrayType) data.type).getComponentType());
}
}
super.doOKAction();
}
use of com.intellij.util.containers.MultiMap in project intellij-community by JetBrains.
the class InlineConstantFieldProcessor method preprocessUsages.
@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
UsageInfo[] usagesIn = refUsages.get();
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
ReferencedElementsCollector collector = new ReferencedElementsCollector();
PsiExpression initializer = InlineConstantFieldHandler.getInitializer(myField);
LOG.assertTrue(initializer != null);
initializer.accept(collector);
HashSet<PsiMember> referencedWithVisibility = collector.myReferencedMembers;
PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(myField.getProject()).getResolveHelper();
for (UsageInfo info : usagesIn) {
PsiElement element = info.getElement();
if (element instanceof PsiExpression && (!myField.hasModifierProperty(PsiModifier.FINAL) || myInlineThisOnly) && isAccessedForWriting((PsiExpression) element)) {
String message = RefactoringBundle.message("0.is.used.for.writing.in.1", RefactoringUIUtil.getDescription(myField, true), RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(element), true));
conflicts.putValue(element, message);
}
for (PsiMember member : referencedWithVisibility) {
if (!resolveHelper.isAccessible(member, element, null)) {
String message = RefactoringBundle.message("0.will.not.be.accessible.from.1.after.inlining", RefactoringUIUtil.getDescription(member, true), RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(element), true));
conflicts.putValue(member, message);
}
}
}
if (!myInlineThisOnly) {
for (UsageInfo info : usagesIn) {
if (info instanceof UsageFromJavaDoc) {
final PsiElement element = info.getElement();
if (element instanceof PsiDocMethodOrFieldRef && !PsiTreeUtil.isAncestor(myField, element, false)) {
conflicts.putValue(element, "Inlined method is used in javadoc");
}
}
}
}
return showConflicts(conflicts, usagesIn);
}
use of com.intellij.util.containers.MultiMap in project intellij-community by JetBrains.
the class InlineParameterExpressionProcessor method preprocessUsages.
@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
JavaSafeDeleteProcessor.collectMethodConflicts(conflicts, myMethod, myParameter);
final UsageInfo[] usages = refUsages.get();
final InaccessibleExpressionsDetector detector = new InaccessibleExpressionsDetector(conflicts);
myInitializer.accept(detector);
for (UsageInfo usage : usages) {
if (usage instanceof LocalReplacementUsageInfo) {
final PsiElement replacement = ((LocalReplacementUsageInfo) usage).getReplacement();
if (replacement != null) {
replacement.accept(detector);
}
}
}
final Set<PsiVariable> vars = new HashSet<>();
for (UsageInfo usageInfo : usages) {
if (usageInfo instanceof LocalReplacementUsageInfo) {
final PsiVariable var = ((LocalReplacementUsageInfo) usageInfo).getVariable();
if (var != null) {
vars.add(var);
}
}
}
for (PsiVariable var : vars) {
for (PsiReference ref : ReferencesSearch.search(var)) {
final PsiElement element = ref.getElement();
if (element instanceof PsiExpression && isAccessedForWriting((PsiExpression) element)) {
conflicts.putValue(element, "Parameter initializer depends on value which is not available inside method and cannot be inlined");
break;
}
}
}
return showConflicts(conflicts, usages);
}
Aggregations