CollectTerminal (PsiMethodCallExpression streamChain) { if(streamChain.getMethodExpression().getQualifierExpression() == null) return null; PsiMethodCallExpression collector = tryCast(PsiUtil.skipParenthesizedExprDown(ArrayUtil.getFirstElement(streamChain.getArgumentList().getExpressions())), PsiMethodCallExpression.class); PsiLocalVariable var = tryCast(streamChain.getParent(), PsiLocalVariable.class); CollectTerminal terminal; PsiElement nextElement; if (var == null) { terminal = new StreamCollectChainNoVar(streamChain, collector); nextElement = CommonJavaRefactoringUtil.getParentStatement(streamChain, false); } else { PsiDeclarationStatement declaration = tryCast(var.getParent(), PsiDeclarationStatement.class); if (declaration == null || declaration.getDeclaredElements().length != 1) return null; terminal = new StreamCollectChain(var, streamChain, collector); nextElement = PsiTreeUtil.skipWhitespacesAndCommentsForward(declaration); } CollectTerminal newTerminal = CollectMigration.includePostStatements(terminal, nextElement); if (newTerminal == terminal) return null; return newTerminal; }