MethodReferenceCandidate (PsiElement body) { final PsiExpression expression = PsiUtil.skipParenthesizedExprDown(LambdaUtil.extractSingleExpressionFromBody(body)); if (expression == null) { return null; } if (expression instanceof PsiNewExpression newExpression) { if (newExpression.getQualifier() != null || newExpression.getAnonymousClass() != null || newExpression.getArrayInitializer() != null) { return null; } PsiExpressionList argumentList = newExpression.getArgumentList(); if (argumentList != null) { for (PsiExpression arg : argumentList.getExpressions()) { if (!isMethodReferenceArgCandidate(arg)) return null; } } return new MethodReferenceCandidate(expression, true, true); } else if (expression instanceof PsiMethodCallExpression call) { for (PsiExpression arg : call.getArgumentList().getExpressions()) { if (!isMethodReferenceArgCandidate(arg)) return null; } return new MethodReferenceCandidate(expression, checkQualifier(call.getMethodExpression().getQualifier()), true); } JavaCodeStyleSettings javaSettings = JavaCodeStyleSettings.getInstance(expression.getContainingFile()); if (expression instanceof PsiInstanceOfExpression instanceOfExpression) { if (!isMethodReferenceArgCandidate(instanceOfExpression.getOperand())) return null; return new MethodReferenceCandidate(expression, true, javaSettings.REPLACE_INSTANCEOF_AND_CAST); } else if (expression instanceof PsiBinaryExpression binOp) { PsiExpression comparedWithNull = PsiUtil.skipParenthesizedExprDown(ExpressionUtils.getValueComparedWithNull(binOp)); if (isMethodReferenceArgCandidate(comparedWithNull)) { return new MethodReferenceCandidate(expression, true, javaSettings.REPLACE_NULL_CHECK); } if (binOp.getOperationTokenType().equals(JavaTokenType.PLUS) && isMethodReferenceArgCandidate(binOp.getLOperand()) && isMethodReferenceArgCandidate(binOp.getROperand())) { return new MethodReferenceCandidate(expression, true, javaSettings.REPLACE_SUM); } } else if (expression instanceof PsiTypeCastExpression cast) { if (!isMethodReferenceArgCandidate(cast.getOperand())) return null; PsiTypeElement typeElement = cast.getCastType(); if (typeElement != null) { PsiJavaCodeReferenceElement refs = typeElement.getInnermostComponentReferenceElement(); if (refs != null && refs.getParameterList() != null && refs.getParameterList().getTypeParameterElements().length != 0) { return null; } PsiType type = typeElement.getType(); if (type instanceof PsiPrimitiveType || PsiUtil.resolveClassInType(type) instanceof PsiTypeParameter) return null; if (type instanceof PsiIntersectionType) return null; return new MethodReferenceCandidate(expression, true, javaSettings.REPLACE_INSTANCEOF_AND_CAST); } } return null; }