String (PsiLambdaExpression lambda, PsiParameter leftVar, PsiParameter rightVar) { PsiExpression body = PsiUtil.skipParenthesizedExprDown(LambdaUtil.extractSingleExpressionFromBody(lambda.getBody())); PsiExpression left; @NonNls String methodName = null; if (body instanceof PsiMethodCallExpression methodCall) { if (MethodCallUtils.isCompareToCall(methodCall)) { left = methodCall.getMethodExpression().getQualifierExpression(); PsiExpression right = ArrayUtil.getFirstElement(methodCall.getArgumentList().getExpressions()); if (ExpressionUtils.isReferenceTo(left, leftVar) && ExpressionUtils.isReferenceTo(right, rightVar)) { methodName = "naturalOrder"; } else if (ExpressionUtils.isReferenceTo(right, leftVar) && ExpressionUtils.isReferenceTo(left, rightVar)) { methodName = "reverseOrder"; } else if (areEquivalent(leftVar, left, rightVar, right)) { methodName = "comparing"; } } else { PrimitiveComparison comparison = PrimitiveComparison.from(methodCall); if (comparison == null) return null; PsiExpression[] args = methodCall.getArgumentList().getExpressions(); if (!areEquivalent(leftVar, args[0], rightVar, args[1])) return null; methodName = comparison.getMethodName(); left = comparison.getKeyExtractor(); } } else if (body instanceof PsiBinaryExpression binOp) { if (!binOp.getOperationTokenType().equals(JavaTokenType.MINUS)) return null; left = binOp.getLOperand(); PsiType type = left.getType(); if (type == null) return null; if (areEquivalent(leftVar, left, rightVar, binOp.getROperand())) { methodName = getComparingMethodName(type.getCanonicalText()); } } else { return null; } if (methodName == null) return null; @NonNls String text; if (!methodName.startsWith("comparing")) { text = "java.util.Comparator." + methodName + "()"; } else { String parameterName = leftVar.getName(); PsiTypeElement typeElement = leftVar.getTypeElement(); if (typeElement != null && PsiUtilCore.hasErrorElementChild(typeElement)) return null; String typeText = typeElement == null ? GenericsUtil.getVariableTypeByExpressionType(leftVar.getType()).getCanonicalText() : typeElement.getText(); String parameterDeclaration = "(" + typeText + " " + parameterName + ")"; text = "java.util.Comparator." + methodName + "(" + (parameterDeclaration + " -> " + left.getText()) + ")"; } return text; }