fix getRefs/getDefs consistency

IDEA-CR-32302
This commit is contained in:
Anna.Kozlova
2018-05-03 18:50:53 +02:00
parent e491cbed3d
commit bd465790b6
2 changed files with 61 additions and 54 deletions
@@ -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<PsiElement> 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) {
@@ -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<PsiElement> 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<PsiElement> 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 {