int (@NotNull PsiExpression e) { if (e instanceof PsiLiteralExpression) { return CONSTANT; } if (e instanceof PsiInstanceOfExpression || e instanceof PsiTypeCastExpression) { return IDENTIFIER; } if (e instanceof PsiClassObjectAccessExpression) { return QUALIFIER; } if (e instanceof PsiQualifiedExpression qualifiedExpression) { return qualifiedExpression.getQualifier() != null ? QUALIFIER : 0; } if (e instanceof PsiParenthesizedExpression parens) { return getComplexity(parens.getExpression()); } if (e instanceof PsiUnaryExpression unary) { int c = NEGATIONS.contains(unary.getOperationTokenType()) ? SIMPLE_OPERATOR : OPERATOR; return getComplexity(unary.getOperand()) + c; } if (e instanceof PsiBinaryExpression binary) { IElementType token = binary.getOperationTokenType(); PsiExpression left = binary.getLOperand(); PsiExpression right = binary.getROperand(); int c = BOOLEAN_OPERATION_TOKENS.contains(token) || JavaTokenType.PLUS.equals(token) && (isLiteral(left, "1") || isLiteral(right, "1")) || JavaTokenType.MINUS.equals(token) && isLiteral(right, "1") || JavaTokenType.ASTERISK.equals(token) && (isLiteral(left, "2") || isLiteral(right, "2")) || JavaTokenType.DIV.equals(token) && isLiteral(right, "2") ? SIMPLE_OPERATOR : OPERATOR; return c + getComplexity(left) + getComplexity(right); } if (e instanceof PsiPolyadicExpression polyadic) { int c = BOOLEAN_OPERATION_TOKENS.contains(polyadic.getOperationTokenType()) ? SIMPLE_OPERATOR : OPERATOR; for (PsiExpression operand : polyadic.getOperands()) { c += getComplexity(operand); } return c; } if (e instanceof PsiConditionalExpression conditional) { return TERNARY_OPERATOR + getComplexity(conditional.getCondition()) + getComplexity(conditional.getThenExpression()) + getComplexity(conditional.getElseExpression()); } if (e instanceof PsiMethodCallExpression call) { if (PATH_CONSTRUCTION_CALL.test(call)) return PATH_CONSTRUCTION; PsiReferenceExpression ref = call.getMethodExpression(); return METHOD_CALL + getComplexity(ref.getQualifierExpression()) + calculateArgumentsComplexity(call.getArgumentList()); } if (e instanceof PsiReferenceExpression ref) { PsiElement resolved = ref.resolve(); if (resolved instanceof PsiClass) { return QUALIFIER; } if (resolved instanceof PsiPackage) { return 0; } int w = REFERENCE; if (PsiUtil.isJvmLocalVariable(resolved)) { w = IDENTIFIER; } else if (resolved instanceof PsiField field && field.hasModifierProperty(PsiModifier.FINAL)) { w = field.hasModifierProperty(PsiModifier.STATIC) ? CONSTANT : IDENTIFIER; } return getComplexity(ref.getQualifierExpression()) + w; } if (e instanceof PsiArrayAccessExpression access) { return SLICE + getComplexity(access.getArrayExpression()) + getComplexity(access.getIndexExpression()); } if (e instanceof PsiLambdaExpression lambda) { PsiElement body = lambda.getBody(); int c = LAMBDA + PARAMETER * lambda.getParameterList().getParametersCount(); PsiExpression bodyExpr = LambdaUtil.extractSingleExpressionFromBody(body); if (bodyExpr != null) { return c + getComplexity(bodyExpr); } if(body instanceof PsiCodeBlock block) { return c + UNKNOWN * block.getStatementCount(); } return c + UNKNOWN; } if (e instanceof PsiArrayInitializerExpression arrayInitializer) { PsiExpression[] initializers = arrayInitializer.getInitializers(); int c = 0; for (PsiExpression initializer : initializers) { c += PARAMETER + getComplexity(initializer); } return c; } if (e instanceof PsiNewExpression newExpr) { return NEW_EXPR + getComplexity(newExpr.getQualifier()) + getComplexity(newExpr.getArrayInitializer()) + calculateArgumentsComplexity(newExpr.getArgumentList()) + (newExpr.getAnonymousClass() != null ? UNKNOWN : 0); } return UNKNOWN; }