PsiElementVisitor (@NotNull ProblemsHolder holder, boolean isOnTheFly) { if (!PsiUtil.isLanguageLevel8OrHigher(holder.getFile())) { return PsiElementVisitor.EMPTY_VISITOR; } return new JavaElementVisitor() { @Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) { if (THEN_COMPARING_COMPARATOR.test(call)) { checkThenComparing(call); } if (COMPARATOR_COMPARING.test(call)) { PsiExpression arg = call.getArgumentList().getExpressions()[0]; PsiElement nameElement = Objects.requireNonNull(call.getMethodExpression().getReferenceNameElement()); @NonNls String [] suffixes = new String[]{"Key", "Value"}; for (String suffix : suffixes) { if (FunctionalExpressionUtils.isFunctionalReferenceTo(arg, JAVA_UTIL_MAP_ENTRY, null, "get"+suffix, PsiType.EMPTY_ARRAY)) { String replacementMethod = "comparingBy" + suffix; @NlsSafe String comparator = "Entry." + replacementMethod + "()"; holder.registerProblem(nameElement, JavaBundle.message("inspection.simplifiable.comparator.entry.comparator.message",comparator), new ReplaceWithEntryComparatorFix(replacementMethod)); } } } if (MIN_MAX.test(call)) { PsiExpression arg = ArrayUtil.getLastElement(call.getArgumentList().getExpressions()); if (getPlainComparatorExpressionFromReversed(arg, new CommentTracker()) != null) { PsiElement nameElement = Objects.requireNonNull(call.getMethodExpression().getReferenceNameElement()); String maxOrMin = nameElement.getText(); String replacement = getMaxMinReplacement(maxOrMin); holder.registerProblem(nameElement, JavaBundle.message("inspection.simplifiable.comparator.reversed.message", maxOrMin, replacement), new ReplaceMaxMinFix(replacement)); } } } private void checkThenComparing(@NotNull PsiMethodCallExpression call) { PsiMethodCallExpression comparingCall = tryCast(PsiUtil.skipParenthesizedExprDown(call.getArgumentList().getExpressions()[0]), PsiMethodCallExpression.class); String targetMethod = REPLACEMENTS.mapFirst(comparingCall); if (targetMethod == null) return; PsiExpressionList comparingArgs = comparingCall.getArgumentList(); if (targetMethod.equals("thenComparing") && comparingArgs.getExpressionCount() == 1) { PsiMethodCallExpression copy = (PsiMethodCallExpression)call.copy(); copy.getArgumentList().replace(comparingArgs.copy()); // Call becomes ambiguous after simplification if (!THEN_COMPARING_FUNCTION.matches(copy)) return; } String name = comparingCall.getMethodExpression().getReferenceName(); holder .registerProblem(comparingCall.getMethodExpression(), JavaBundle.message("inspection.simplifiable.comparator.comparing.message", name), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new DeleteComparingCallFix(name, targetMethod)); } }; }