String (PsiExpression expression) { if (expression == null) return null; String text = expression.getText(); expression = skipParenthesizedExprDown(expression); if (expression == null) return null; if (expression instanceof PsiFunctionalExpression) return text; if (expression instanceof PsiConditionalExpression ternary) { String thenBranch = adaptToPredicate(ternary.getThenExpression()); String elseBranch = adaptToPredicate(ternary.getElseExpression()); if (thenBranch == null || elseBranch == null) return null; PsiElementFactory factory = JavaPsiFacade.getElementFactory(expression.getProject()); PsiConditionalExpression copy = (PsiConditionalExpression)factory.createExpressionFromText(text, expression); Objects.requireNonNull(copy.getThenExpression()).replace(factory.createExpressionFromText(thenBranch, expression)); Objects.requireNonNull(copy.getElseExpression()).replace(factory.createExpressionFromText(elseBranch, expression)); return copy.getText(); } String adapted = ParenthesesUtils.getText(expression, ParenthesesUtils.POSTFIX_PRECEDENCE) + "::apply"; PsiClassType type = tryCast(expression.getType(), PsiClassType.class); if (type == null) return null; if (PsiTypesUtil.classNameEquals(type, JAVA_UTIL_FUNCTION_FUNCTION)) return adapted; PsiClass typeClass = type.resolve(); // Disable inspection if type of expression is some subtype which defines its own 'apply' methods // to avoid possible resolution clashes if (typeClass == null) return null; PsiMethod[] methods = typeClass.findMethodsByName("apply", true); if (methods.length != 1 || methods[0].getContainingClass() == null || !JAVA_UTIL_FUNCTION_FUNCTION.equals(methods[0].getContainingClass().getQualifiedName())) { return null; } return adapted; }