JetBrains/intellij-community · error · PrepareFailedException
extract.method.control.flow.analysis.failed
extract.method.control.flow.analysis.failed
Error message
Code contains syntax errors. Cannot perform necessary analysis.
What it means
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.'
Source
Thrown at java/java-impl/src/com/intellij/refactoring/extractMethod/ControlFlowWrapper.java:62
public final class ControlFlowWrapper {
private static final Logger LOG = Logger.getInstance(ControlFlowWrapper.class);
private final ControlFlow myControlFlow;
private final int myFlowStart;
private final int myFlowEnd;
private boolean myGenerateConditionalExit;
private Collection<PsiStatement> myExitStatements;
private PsiStatement myFirstExitStatementCopy;
private IntList myExitPoints;
public ControlFlowWrapper(PsiElement codeFragment, PsiElement[] elements) throws PrepareFailedException {
try {
myControlFlow = ControlFlowFactory.getControlFlow(codeFragment, new LocalsControlFlowPolicy(codeFragment),
ControlFlowOptions.NO_CONST_EVALUATE);
}
catch (AnalysisCanceledException e) {
throw new PrepareFailedException(JavaRefactoringBundle.message("extract.method.control.flow.analysis.failed"), e.getErrorElement());
}
if (LOG.isDebugEnabled()) {
LOG.debug(myControlFlow.toString());
}
int flowStart = -1;
int index = 0;
while (index < elements.length) {
flowStart = myControlFlow.getStartOffset(elements[index]);
if (flowStart >= 0) break;
index++;
}
int flowEnd;
if (flowStart < 0) {
// no executable code
flowStart = 0;
flowEnd = 0;View on GitHub (pinned to be881553f2)
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.
Example fix
// before List<String> items = new ArrayList<>() // missing ';' -> Extract Method fails process(items); // after List<String> items = new ArrayList<>(); process(items); // Extract Method now works
Defensive patterns
Strategy: validation
Validate before calling
// plugin code: ensure the file is committed and error-free before Extract Method
if (ReadAction.compute(() -> PsiErrorElementUtil.hasErrors(project, file.getVirtualFile()))) {
// abort with 'fix syntax errors first' instead of invoking the refactoring
} Try / catch
try { new ExtractMethodProcessor(project, editor, elements, ...).run(); } catch (PrepareFailedException e) { /* e.getErrorMessage() points at the broken element; fix syntax and retry */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Unable to extract method from annotation value
- Cannot extract method because the selected code fragment use
- Cannot extract method because the selected code fragment def
- Cannot extract method because the selected code fragment def
- Cannot bind to {element}
AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14).
Data as JSON: /api/errors/da1b77e8c5cb873b.
Report an issue: GitHub.