PsiExpression (PsiVariable @NotNull [] parameters, @Nullable PsiType functionalInterfaceType, @Nullable PsiElement context, final PsiExpression methodRefCandidate) { if (functionalInterfaceType == null) return null; // Do not suggest for annotated lambda, as annotation will be lost during the conversion if (ContainerUtil.or(parameters, LambdaCanBeMethodReferenceInspection::hasAnnotation)) return null; if (methodRefCandidate instanceof PsiNewExpression newExpression) { if (newExpression.getAnonymousClass() != null || newExpression.getArrayInitializer() != null) { return null; } } final String methodReferenceText = createMethodReferenceText(methodRefCandidate, functionalInterfaceType, parameters); if (methodReferenceText != null) { LOG.assertTrue(methodRefCandidate != null); if (!(methodRefCandidate instanceof PsiCallExpression callExpression)) return methodRefCandidate; final PsiMethod method = callExpression.resolveMethod(); if (method != null) { if (!isSimpleCall(parameters, callExpression, method)) { return null; } } else { LOG.assertTrue(callExpression instanceof PsiNewExpression); if (((PsiNewExpression)callExpression).getQualifier() != null) { return null; } final PsiExpression[] dims = ((PsiNewExpression)callExpression).getArrayDimensions(); if (dims.length == 1 && parameters.length == 1){ if (!ExpressionUtils.isReferenceTo(dims[0], parameters[0])) { return null; } } else if (dims.length > 0) { return null; } if (callExpression.getTypeArguments().length > 0) { return null; } } final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(callExpression.getProject()); PsiMethodReferenceExpression methodReferenceExpression; try { methodReferenceExpression = (PsiMethodReferenceExpression)elementFactory.createExpressionFromText(methodReferenceText, context != null ? context : callExpression); } catch (IncorrectOperationException | ClassCastException e) { LOG.error(callExpression.getText(), e); return null; } return LambdaUtil.performWithTargetType(methodReferenceExpression, functionalInterfaceType, () -> { final JavaResolveResult result = methodReferenceExpression.advancedResolve(false); final PsiElement element = result.getElement(); if (element != null && result.isAccessible() && !(result instanceof MethodCandidateInfo && !((MethodCandidateInfo)result).isApplicable())) { if (!(element instanceof PsiMethod)) { return callExpression; } return method != null && MethodSignatureUtil.areSignaturesEqual((PsiMethod)element, method) ? callExpression : null; } return null; }); } return null; }