{"record":{"id":"da1b77e8c5cb873b","repo":"JetBrains/intellij-community","slug":"extract-method-control-flow-analysis-failed","errorCode":"extract.method.control.flow.analysis.failed","errorMessage":"Code contains syntax errors. Cannot perform necessary analysis.","messagePattern":"Code contains syntax errors\\. Cannot perform necessary analysis\\.","errorType":"exception","errorClass":"PrepareFailedException","httpStatus":null,"severity":"error","filePath":"java/java-impl/src/com/intellij/refactoring/extractMethod/ControlFlowWrapper.java","lineNumber":62,"sourceCode":"public final class ControlFlowWrapper {\n  private static final Logger LOG = Logger.getInstance(ControlFlowWrapper.class);\n\n  private final ControlFlow myControlFlow;\n  private final int myFlowStart;\n\n  private final int myFlowEnd;\n  private boolean myGenerateConditionalExit;\n  private Collection<PsiStatement> myExitStatements;\n  private PsiStatement myFirstExitStatementCopy;\n  private IntList myExitPoints;\n\n  public ControlFlowWrapper(PsiElement codeFragment, PsiElement[] elements) throws PrepareFailedException {\n    try {\n      myControlFlow = ControlFlowFactory.getControlFlow(codeFragment, new LocalsControlFlowPolicy(codeFragment),\n                                          ControlFlowOptions.NO_CONST_EVALUATE);\n    }\n    catch (AnalysisCanceledException e) {\n      throw new PrepareFailedException(JavaRefactoringBundle.message(\"extract.method.control.flow.analysis.failed\"), e.getErrorElement());\n    }\n\n    if (LOG.isDebugEnabled()) {\n      LOG.debug(myControlFlow.toString());\n    }\n\n    int flowStart = -1;\n    int index = 0;\n    while (index < elements.length) {\n      flowStart = myControlFlow.getStartOffset(elements[index]);\n      if (flowStart >= 0) break;\n      index++;\n    }\n    int flowEnd;\n    if (flowStart < 0) {\n      // no executable code\n      flowStart = 0;\n      flowEnd = 0;","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-impl/src/com/intellij/refactoring/extractMethod/ControlFlowWrapper.java#L44-L80","documentation":"Thrown by the ControlFlowWrapper constructor (used by Extract Method) when ControlFlowFactory.getControlFlow fails with AnalysisCanceledException, which for data-flow analysis almost always means the containing file has syntax errors and the CFG cannot be built. The PrepareFailedException carries the error element and is surfaced by the refactoring as 'Code contains syntax errors. Cannot perform necessary analysis.'","triggerScenarios":"Invoking Extract Method on code inside a file that currently does not parse: missing semicolon, unbalanced braces, unresolved/errored fragments, or a PSI file in a transient broken state mid-edit; the DFA control flow builder throws AnalysisCanceledException at the erroneous element.","commonSituations":"Extracting while the editor shows red syntax errors; files broken by a merge conflict or partial paste; generated files with invalid syntax; running the refactoring programmatically on uncommitted/broken PSI during indexing or before a document commit.","solutions":["Fix the syntax errors highlighted in the editor (red_code markers) and re-run Extract Method.","Run 'Code > Reformat' / inspect braces if the error location is unclear, or use the Problems view to jump to parse errors.","For plugin authors: call CodeInsightUtilBase.forcePsiPostRestoreAndCommitFile / ensure the file is committed and error-free (ErrorUtil.hasSyntaxErrors) before invoking the refactoring."],"exampleFix":"// before\nList<String> items = new ArrayList<>()  // missing ';' -> Extract Method fails\nprocess(items);\n// after\nList<String> items = new ArrayList<>();\nprocess(items);  // Extract Method now works","handlingStrategy":"validation","validationCode":"// plugin code: ensure the file is committed and error-free before Extract Method\nif (ReadAction.compute(() -> PsiErrorElementUtil.hasErrors(project, file.getVirtualFile()))) {\n  // abort with 'fix syntax errors first' instead of invoking the refactoring\n}","typeGuard":null,"tryCatchPattern":"try { new ExtractMethodProcessor(project, editor, elements, ...).run(); } catch (PrepareFailedException e) { /* e.getErrorMessage() points at the broken element; fix syntax and retry */ }","preventionTips":["Keep the file compiling before invoking Extract Method; check the editor for red code first.","After messy merges/pastes, run Parse/inspect the file before refactorings.","For plugin automation, commit documents (PsiDocumentManager.commitDocument) and check PsiErrorElementUtil.hasErrors before refactoring."],"tags":["java","refactoring","extract-method","syntax-errors","control-flow"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}