diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/comparatorCanBeSimplified/afterGenericArgs.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/comparatorCanBeSimplified/afterGenericArgs.java new file mode 100644 index 000000000000..b36c092611b0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/comparatorCanBeSimplified/afterGenericArgs.java @@ -0,0 +1,14 @@ +// "Replace with 'Entry.comparingByKey()'" "true" + +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +class X { + private static List> sortFrequencies(Map freq) { + return freq.entrySet().stream() + .sorted(Map.Entry.comparingByKey().thenComparing(Map.Entry::getValue)) + .collect(Collectors.toList()); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/comparatorCanBeSimplified/beforeGenericArgs.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/comparatorCanBeSimplified/beforeGenericArgs.java new file mode 100644 index 000000000000..0a6165327939 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/comparatorCanBeSimplified/beforeGenericArgs.java @@ -0,0 +1,14 @@ +// "Replace with 'Entry.comparingByKey()'" "true" + +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +class X { + private static List> sortFrequencies(Map freq) { + return freq.entrySet().stream() + .sorted(Comparator., String>comparing(Map.Entry::getKey).thenComparing(Map.Entry::getValue)) + .collect(Collectors.toList()); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/intellij/codeInspection/RedundantComparatorComparingInspection.java b/plugins/InspectionGadgets/src/com/intellij/codeInspection/RedundantComparatorComparingInspection.java index 122f2827a5b6..4831e27d5c29 100644 --- a/plugins/InspectionGadgets/src/com/intellij/codeInspection/RedundantComparatorComparingInspection.java +++ b/plugins/InspectionGadgets/src/com/intellij/codeInspection/RedundantComparatorComparingInspection.java @@ -243,10 +243,25 @@ public class RedundantComparatorComparingInspection extends AbstractBaseJavaLoca public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PsiMethodCallExpression call = PsiTreeUtil.getParentOfType(descriptor.getStartElement(), PsiMethodCallExpression.class); if (call == null) return; + String params = getGenericParameters(call); PsiExpression[] args = call.getArgumentList().getExpressions(); CommentTracker ct = new CommentTracker(); - String replacement = JAVA_UTIL_MAP_ENTRY + "." + myReplacementMethod + "(" + (args.length == 2 ? ct.text(args[1]) : "") + ")"; - ct.replaceAndRestoreComments(call, replacement); + String replacement = JAVA_UTIL_MAP_ENTRY + "." + params + myReplacementMethod + "(" + (args.length == 2 ? ct.text(args[1]) : "") + ")"; + PsiElement result = ct.replaceAndRestoreComments(call, replacement); + RemoveRedundantTypeArgumentsUtil.removeRedundantTypeArguments(result); + } + + @NotNull + private static String getGenericParameters(PsiMethodCallExpression call) { + PsiClassType callType = tryCast(call.getType(), PsiClassType.class); + if (callType == null || !callType.rawType().equalsToText(JAVA_UTIL_COMPARATOR)) return ""; + PsiType[] parameters = callType.getParameters(); + if (parameters.length != 1) return ""; + PsiClassType entryClass = tryCast(parameters[0], PsiClassType.class); + if (entryClass == null || !entryClass.rawType().equalsToText(JAVA_UTIL_MAP_ENTRY)) return ""; + PsiType[] entryClassParameters = entryClass.getParameters(); + if (entryClassParameters.length != 2) return ""; + return "<" + entryClassParameters[0].getCanonicalText() + "," + entryClassParameters[1].getCanonicalText() + ">"; } } }