Java inspection: Simplify quick fix in "Add Braces" and "Remove Braces" inspections, in order to avoid exceptions in the inspection tool UI. (IDEA-157727)

This commit is contained in:
Pavel Dolgov
2016-07-15 12:32:08 +03:00
parent 48fc2a262e
commit 98fe96ff6a
2 changed files with 18 additions and 39 deletions
@@ -47,23 +47,23 @@ public class ControlFlowStatementWithoutBracesInspection
@Override
public InspectionGadgetsFix buildFix(Object... infos) {
if (infos.length == 1 && infos[0] instanceof String) {
switch ((String)infos[0]) {
case PsiKeyword.DO: return new DoBracesFix();
case PsiKeyword.ELSE: return new ElseBracesFix();
case PsiKeyword.FOR: return new ForBracesFix();
case PsiKeyword.IF: return new IfBracesFix();
case PsiKeyword.WHILE: return new WhileBracesFix();
}
return new ControlFlowStatementFix((String)infos[0]);
}
return null;
}
private static abstract class ControlFlowStatementFix extends InspectionGadgetsFix {
private static class ControlFlowStatementFix extends InspectionGadgetsFix {
private final String myKeywordText;
private ControlFlowStatementFix(String keywordText) {
myKeywordText = keywordText;
}
@Override
@NotNull
public String getName() {
return InspectionGadgetsBundle.message(
"control.flow.statement.without.braces.message", getKeywordText());
"control.flow.statement.without.braces.message", myKeywordText);
}
@Override
@@ -73,8 +73,6 @@ public class ControlFlowStatementWithoutBracesInspection
"control.flow.statement.without.braces.add.quickfix");
}
abstract String getKeywordText();
@Override
protected void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
@@ -142,10 +140,4 @@ public class ControlFlowStatementWithoutBracesInspection
return body != null && !(body instanceof PsiBlockStatement);
}
}
private static class DoBracesFix extends ControlFlowStatementFix { @Override String getKeywordText() { return PsiKeyword.DO; } }
private static class ElseBracesFix extends ControlFlowStatementFix { @Override String getKeywordText() { return PsiKeyword.ELSE; } }
private static class ForBracesFix extends ControlFlowStatementFix { @Override String getKeywordText() { return PsiKeyword.FOR; } }
private static class IfBracesFix extends ControlFlowStatementFix { @Override String getKeywordText() { return PsiKeyword.IF; } }
private static class WhileBracesFix extends ControlFlowStatementFix { @Override String getKeywordText() { return PsiKeyword.WHILE; } }
}
@@ -52,18 +52,7 @@ public class SingleStatementInBlockInspection extends BaseInspection {
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
if (infos.length == 1 && infos[0] instanceof String) {
switch ((String)infos[0]) {
case PsiKeyword.DO:
return new RemoveDoBracesFix();
case PsiKeyword.ELSE:
return new RemoveElseBracesFix();
case PsiKeyword.FOR:
return new RemoveForBracesFix();
case PsiKeyword.IF:
return new RemoveIfBracesFix();
case PsiKeyword.WHILE:
return new RemoveWhileBracesFix();
}
return new SingleStatementInBlockFix((String)infos[0]);
}
return null;
}
@@ -124,12 +113,18 @@ public class SingleStatementInBlockInspection extends BaseInspection {
}
}
private static abstract class SingleStatementInBlockFix extends InspectionGadgetsFix {
private static class SingleStatementInBlockFix extends InspectionGadgetsFix {
private final String myKeywordText;
private SingleStatementInBlockFix(String keywordText) {
myKeywordText = keywordText;
}
@Nls
@NotNull
@Override
public String getName() {
return InspectionGadgetsBundle.message("single.statement.in.block.descriptor", getKeywordText());
return InspectionGadgetsBundle.message("single.statement.in.block.descriptor", myKeywordText);
}
@Nls
@@ -161,13 +156,5 @@ public class SingleStatementInBlockInspection extends BaseInspection {
assert body instanceof PsiBlockStatement;
doFixImpl((PsiBlockStatement)body);
}
abstract String getKeywordText();
}
private static class RemoveDoBracesFix extends SingleStatementInBlockFix { @Override String getKeywordText() { return PsiKeyword.DO; } }
private static class RemoveElseBracesFix extends SingleStatementInBlockFix { @Override String getKeywordText() { return PsiKeyword.ELSE; } }
private static class RemoveForBracesFix extends SingleStatementInBlockFix { @Override String getKeywordText() { return PsiKeyword.FOR; } }
private static class RemoveIfBracesFix extends SingleStatementInBlockFix { @Override String getKeywordText() { return PsiKeyword.IF; } }
private static class RemoveWhileBracesFix extends SingleStatementInBlockFix { @Override String getKeywordText() { return PsiKeyword.WHILE; } }
}