Comparison (@NotNull PsiExpression expression, @NotNull PsiVariable loopVariable, boolean isGreater) { PsiMethodCallExpression methodExpression = tryCast(expression, PsiMethodCallExpression.class); if (methodExpression == null) return null; PsiExpression qualifierExpression = methodExpression.getMethodExpression().getQualifierExpression(); if (qualifierExpression == null) return null; PsiMethod method = methodExpression.resolveMethod(); if (method == null) return null; String methodName = method.getName(); PsiType qualifierType = qualifierExpression.getType(); PsiExpressionList argumentList = methodExpression.getArgumentList(); PsiExpression[] arguments = argumentList.getExpressions(); if (arguments.length == 1) { PsiExpression argument = arguments[0]; if (!methodName.equals("compareTo") || !InheritanceUtil.isInheritor(qualifierType, CommonClassNames.JAVA_LANG_COMPARABLE)) { return null; } return extract(argument, qualifierExpression, loopVariable, isGreater, null, false); } else if (arguments.length == 2) { PsiExpression firstArgument = arguments[0]; PsiExpression secondArgument = arguments[1]; if (!methodName.equals("compare") || !InheritanceUtil.isInheritor(qualifierType, CommonClassNames.JAVA_UTIL_COMPARATOR)) { return null; } PsiVariable comparator = resolveVariableReference(qualifierExpression); return extract(secondArgument, firstArgument, loopVariable, isGreater, comparator, true); } return null; }