From d56ddff7b819387dcf92b474e7a8ce5838594735 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Tue, 1 May 2018 14:07:49 +0300 Subject: [PATCH] inline local: show warning if syntax errors prevent inlining (IDEA-191147) --- .../inline/InlineLocalHandler.java | 9 ++- .../intellij/psi/controlFlow/DefUseUtil.java | 79 ++++++++++--------- 2 files changed, 51 insertions(+), 37 deletions(-) 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 0eb4c17a1b5f..13bcdb5d6217 100644 --- a/java/java-impl/src/com/intellij/refactoring/inline/InlineLocalHandler.java +++ b/java/java-impl/src/com/intellij/refactoring/inline/InlineLocalHandler.java @@ -151,7 +151,14 @@ public class InlineLocalHandler extends JavaInlineActionHandler { } List refsToInlineList = new ArrayList<>(); - Collections.addAll(refsToInlineList, DefUseUtil.getRefs(containerBlock, local, defToInline)); + try { + Collections.addAll(refsToInlineList, DefUseUtil.getVariableRefs(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; + } for (PsiElement innerClassUsage : innerClassUsages) { if (!refsToInlineList.contains(innerClassUsage)) { refsToInlineList.add(innerClassUsage); 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 f63baabb88ac..f34e4b8ca75f 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 @@ -336,48 +336,55 @@ public class DefUseUtil { @NotNull public static PsiElement[] getRefs(@NotNull PsiCodeBlock body, @NotNull PsiVariable def, @NotNull PsiElement ref) { 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); + return getVariableRefs(body, def, ref); } catch (AnalysisCanceledException e) { return PsiElement.EMPTY_ARRAY; } } + /** + * @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); + } + } + }); + } + } + } + }; + return refsDefs.get(def, ref); + } + private abstract static class RefsDefs { protected abstract int nNext(int index); protected abstract int getNext(int index, int no);