BufferedReaderLines (PsiLoopStatement loopStatement) { PsiForStatement forLoop = tryCast(loopStatement, PsiForStatement.class); if (forLoop == null) return null; PsiDeclarationStatement declarationStatement = tryCast(forLoop.getInitialization(), PsiDeclarationStatement.class); if (declarationStatement == null) return null; PsiElement[] declarations = declarationStatement.getDeclaredElements(); if (declarations.length != 1) return null; PsiLocalVariable lineVar = tryCast(declarations[0], PsiLocalVariable.class); if (lineVar == null) return null; if (ReferencesSearch.search(lineVar).anyMatch(ref -> !PsiTreeUtil.isAncestor(forLoop, ref.getElement(), true))) { return null; } PsiMethodCallExpression maybeReadLines = tryCast(PsiUtil.skipParenthesizedExprDown(lineVar.getInitializer()), PsiMethodCallExpression.class); if (!BUFFERED_READER_READ_LINE.test(maybeReadLines)) return null; PsiExpression reader = PsiUtil.skipParenthesizedExprDown(maybeReadLines.getMethodExpression().getQualifierExpression()); PsiReferenceExpression readerRef = tryCast(reader, PsiReferenceExpression.class); if (readerRef == null) return null; PsiVariable readerVar = tryCast(readerRef.resolve(), PsiVariable.class); if (readerVar == null) return null; PsiBinaryExpression binOp = tryCast(PsiUtil.skipParenthesizedExprDown(forLoop.getCondition()), PsiBinaryExpression.class); if (binOp == null) return null; if (!JavaTokenType.NE.equals(binOp.getOperationTokenType())) return null; PsiExpression lineExpr = ExpressionUtils.getValueComparedWithNull(binOp); if (!ExpressionUtils.isReferenceTo(lineExpr, lineVar)) return null; PsiExpressionStatement updateStmt = tryCast(forLoop.getUpdate(), PsiExpressionStatement.class); if (updateStmt == null) return null; PsiExpression readNewLineExpr = ExpressionUtils.getAssignmentTo(updateStmt.getExpression(), lineVar); PsiMethodCallExpression readNewLineCall = tryCast(PsiUtil.skipParenthesizedExprDown(readNewLineExpr), PsiMethodCallExpression.class); if (!BUFFERED_READER_READ_LINE.test(readNewLineCall)) return null; if (!ExpressionUtils.isReferenceTo(readNewLineCall.getMethodExpression().getQualifierExpression(), readerVar)) return null; return new BufferedReaderLines(forLoop, lineVar, reader, false); }