From b3f2bc4843d5bf553d4534f9d9f9395bb168f424 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Mon, 11 Jan 2016 18:21:16 +0300 Subject: [PATCH] type migration: failed migration usages know about root (required for type migration failed usages ui) IDEA-150181 --- .../typeMigration/TypeMigrationLabeler.java | 51 +++++++++++++------ .../typeMigration/ui/MigrationPanel.java | 2 +- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeMigrationLabeler.java b/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeMigrationLabeler.java index 6e3f3d593e55..bfb4363104ed 100644 --- a/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeMigrationLabeler.java +++ b/java/java-impl/src/com/intellij/refactoring/typeMigration/TypeMigrationLabeler.java @@ -15,6 +15,8 @@ */ package com.intellij.refactoring.typeMigration; +import com.google.common.collect.LinkedHashMultimap; +import com.google.common.collect.Multimap; import com.intellij.codeInsight.generation.GenerateMembersUtil; import com.intellij.codeInsight.generation.GetterSetterPrototypeProvider; import com.intellij.lang.java.JavaLanguage; @@ -74,7 +76,7 @@ public class TypeMigrationLabeler { private final Function myMigrationRootTypeFunction; private TypeEvaluator myTypeEvaluator; private final LinkedHashMap myConversions; - private final HashSet, PsiType>> myFailedConversions; + private final Map, PsiType>, TypeMigrationUsageInfo> myFailedConversions; private LinkedList> myMigrationRoots; private final LinkedHashMap myNewExpressionTypeChange; private final LinkedHashMap myClassTypeArgumentsChange; @@ -96,7 +98,7 @@ public class TypeMigrationLabeler { myMigrationRootTypeFunction = migrationRootTypeFunction; myConversions = new LinkedHashMap(); - myFailedConversions = new HashSet, PsiType>>(); + myFailedConversions = new LinkedHashMap, PsiType>, TypeMigrationUsageInfo>(); myNewExpressionTypeChange = new LinkedHashMap(); myClassTypeArgumentsChange = new LinkedHashMap(); } @@ -113,7 +115,7 @@ public class TypeMigrationLabeler { final String[] report = new String[myFailedConversions.size()]; int j = 0; - for (final Pair, PsiType> p : myFailedConversions) { + for (final Pair, PsiType> p : myFailedConversions.keySet()) { final PsiExpression element = p.getFirst().getElement(); LOG.assertTrue(element != null); final PsiType type = element.getType(); @@ -128,24 +130,39 @@ public class TypeMigrationLabeler { return report; } + public UsageInfo[] getFailedUsages(final TypeMigrationUsageInfo root) { + return map2Usages(ContainerUtil.mapNotNull(myFailedConversions.entrySet(), + new Function, PsiType>, TypeMigrationUsageInfo>, Pair, PsiType>>() { + @Override + public Pair, PsiType> fun(Map.Entry, PsiType>, TypeMigrationUsageInfo> entry) { + return entry.getValue().equals(root) ? entry.getKey() : null; + } + })); + } + public UsageInfo[] getFailedUsages() { - final List usages = new ArrayList(myFailedConversions.size()); - for (final Pair, PsiType> p : myFailedConversions) { - final PsiExpression expr = p.getFirst().getElement(); - if (expr != null) { - usages.add(new UsageInfo(expr) { + return map2Usages(myFailedConversions.keySet()); + } + + @NotNull + private static UsageInfo[] map2Usages(Collection, PsiType>> usages) { + return ContainerUtil + .map2Array(usages, new UsageInfo[usages.size()], new Function, PsiType>, UsageInfo>() { + @Override + public UsageInfo fun(final Pair, PsiType> pair) { + final PsiExpression expr = pair.getFirst().getElement(); + LOG.assertTrue(expr != null); + return new UsageInfo(expr) { @Nullable public String getTooltipText() { final PsiType type = expr.isValid() ? expr.getType() : null; if (type == null) return null; return "Cannot convert type of the expression from " + - type.getCanonicalText() + " to " + p.getSecond().getCanonicalText(); + type.getCanonicalText() + " to " + pair.getSecond().getCanonicalText(); } - }); + }; } - } - - return usages.toArray(new UsageInfo[usages.size()]); + }); } public TypeMigrationUsageInfo[] getMigratedUsages() { @@ -804,7 +821,11 @@ public class TypeMigrationLabeler { void markFailedConversion(final Pair typePair, final PsiExpression expression) { LOG.assertTrue(typePair.getSecond() != null); - myFailedConversions.add(Pair.create(SmartPointerManager.getInstance(expression.getProject()).createSmartPsiElementPointer(expression), typePair.getSecond())); + final Pair, PsiType> key = + Pair.create(SmartPointerManager.getInstance(expression.getProject()).createSmartPsiElementPointer(expression), typePair.getSecond()); + if (!myFailedConversions.containsKey(key)) { + myFailedConversions.put(key, getCurrentRoot()); + } } void setConversionMapping(final PsiExpression expression, final Object obj) { @@ -1101,7 +1122,7 @@ public class TypeMigrationLabeler { buffer.append("Fails:\n"); final ArrayList, PsiType>> - failsList = new ArrayList, PsiType>>(myFailedConversions); + failsList = new ArrayList, PsiType>>(myFailedConversions.keySet()); Collections.sort(failsList, new Comparator, PsiType>>() { public int compare(final Pair, PsiType> o1, final Pair, PsiType> o2) { final PsiElement element1 = o1.getFirst().getElement(); diff --git a/java/java-impl/src/com/intellij/refactoring/typeMigration/ui/MigrationPanel.java b/java/java-impl/src/com/intellij/refactoring/typeMigration/ui/MigrationPanel.java index 63a3b41bbb9a..a9c6f81f4e17 100644 --- a/java/java-impl/src/com/intellij/refactoring/typeMigration/ui/MigrationPanel.java +++ b/java/java-impl/src/com/intellij/refactoring/typeMigration/ui/MigrationPanel.java @@ -161,7 +161,7 @@ public class MigrationPanel extends JPanel implements Disposable { final Object userObject = migrationNodes[0].getUserObject(); if (userObject instanceof MigrationNode) { final MigrationNode migrationNode = (MigrationNode)userObject; - final UsageInfo[] failedUsages = myLabeler.getFailedUsages(); + final UsageInfo[] failedUsages = myLabeler.getFailedUsages(migrationNode.getInfo()); if (failedUsages.length > 0) { myConflictsPanel.showUsages(PsiElement.EMPTY_ARRAY, failedUsages); }