diff --git a/java/java-impl/src/com/intellij/refactoring/inline/InlineLocalHandler.java b/java/java-impl/src/com/intellij/refactoring/inline/InlineLocalHandler.java index 13bcdb5d6217..c8ab95ee76c6 100644 --- a/java/java-impl/src/com/intellij/refactoring/inline/InlineLocalHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/inline/InlineLocalHandler.java @@ -141,23 +141,17 @@ public class InlineLocalHandler extends JavaInlineActionHandler { } } catch (RuntimeException e) { - Throwable cause = e.getCause(); - if (cause instanceof AnalysisCanceledException) { - CommonRefactoringUtil.showErrorHint(project, editor, RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("extract.method.control.flow.analysis.failed")), - REFACTORING_NAME, HelpID.INLINE_VARIABLE); - return; - } - throw e; + processWrappedAnalysisCanceledException(project, editor, e); + return; } List refsToInlineList = new ArrayList<>(); try { - Collections.addAll(refsToInlineList, DefUseUtil.getVariableRefs(containerBlock, local, defToInline)); + Collections.addAll(refsToInlineList, DefUseUtil.getRefs(containerBlock, local, defToInline)); } - catch (AnalysisCanceledException e) { - String message = RefactoringBundle.message("extract.method.control.flow.analysis.failed", localName); - CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.INLINE_VARIABLE); - return; + catch (RuntimeException e) { + processWrappedAnalysisCanceledException(project, editor, e); + return; } for (PsiElement innerClassUsage : innerClassUsages) { if (!refsToInlineList.contains(innerClassUsage)) { @@ -315,6 +309,19 @@ public class InlineLocalHandler extends JavaInlineActionHandler { CommandProcessor.getInstance().executeCommand(project, () -> PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(runnable), RefactoringBundle.message("inline.command", localName), null); } + private static void processWrappedAnalysisCanceledException(@NotNull Project project, + Editor editor, + RuntimeException e) { + Throwable cause = e.getCause(); + if (cause instanceof AnalysisCanceledException) { + CommonRefactoringUtil.showErrorHint(project, editor, + RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("extract.method.control.flow.analysis.failed")), + REFACTORING_NAME, HelpID.INLINE_VARIABLE); + return; + } + throw e; + } + private static void deleteInitializer(@NotNull PsiExpression defToInline) { PsiElement parent = defToInline.getParent(); if (parent instanceof PsiAssignmentExpression) { diff --git a/java/java-psi-impl/src/com/intellij/psi/controlFlow/DefUseUtil.java b/java/java-psi-impl/src/com/intellij/psi/controlFlow/DefUseUtil.java index f34e4b8ca75f..0788c34fa7e1 100644 --- a/java/java-psi-impl/src/com/intellij/psi/controlFlow/DefUseUtil.java +++ b/java/java-psi-impl/src/com/intellij/psi/controlFlow/DefUseUtil.java @@ -335,54 +335,54 @@ public class DefUseUtil { @NotNull public static PsiElement[] getRefs(@NotNull PsiCodeBlock body, @NotNull PsiVariable def, @NotNull PsiElement ref) { - try { - return getVariableRefs(body, def, ref); - } - catch (AnalysisCanceledException e) { - return PsiElement.EMPTY_ARRAY; - } + return getRefs(body, def, ref, false); } - /** - * @throws AnalysisCanceledException when body contains syntax error - */ - public static PsiElement[] getVariableRefs(@NotNull PsiCodeBlock body, @NotNull PsiVariable def, @NotNull PsiElement ref) throws AnalysisCanceledException { - RefsDefs refsDefs = new RefsDefs(body) { - @Override - protected int nNext(int index) { - return instructions.get(index).nNext(); - } - - @Override - protected int getNext(int index, int no) { - return instructions.get(index).getNext(index, no); - } - - @Override - protected boolean defs() { - return false; - } - - @Override - protected void processInstruction(@NotNull final Set res, @NotNull final Instruction instruction, int index) { - if (instruction instanceof ReadVariableInstruction) { - ReadVariableInstruction instructionR = (ReadVariableInstruction)instruction; - if (instructionR.variable == def) { - - final PsiElement element = flow.getElement(index); - element.accept(new JavaRecursiveElementWalkingVisitor() { - @Override - public void visitReferenceExpression(PsiReferenceExpression ref) { - if (ref.resolve() == def) { - res.add(ref); + public static PsiElement[] getRefs(@NotNull PsiCodeBlock body, @NotNull PsiVariable def, @NotNull PsiElement ref, boolean rethrow) { + try { + RefsDefs refsDefs = new RefsDefs(body) { + @Override + protected int nNext(int index) { + return instructions.get(index).nNext(); + } + + @Override + protected int getNext(int index, int no) { + return instructions.get(index).getNext(index, no); + } + + @Override + protected boolean defs() { + return false; + } + + @Override + protected void processInstruction(@NotNull final Set res, @NotNull final Instruction instruction, int index) { + if (instruction instanceof ReadVariableInstruction) { + ReadVariableInstruction instructionR = (ReadVariableInstruction)instruction; + if (instructionR.variable == def) { + + final PsiElement element = flow.getElement(index); + element.accept(new JavaRecursiveElementWalkingVisitor() { + @Override + public void visitReferenceExpression(PsiReferenceExpression ref) { + if (ref.resolve() == def) { + res.add(ref); + } } - } - }); + }); + } } } + }; + return refsDefs.get(def, ref); + } + catch (AnalysisCanceledException e) { + if (rethrow) { + ExceptionUtil.rethrowAllAsUnchecked(e); } - }; - return refsDefs.get(def, ref); + return PsiElement.EMPTY_ARRAY; + } } private abstract static class RefsDefs {