From 98fe96ff6ac6a0dbadfc2df2158662fa88449eda Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Fri, 15 Jul 2016 12:32:08 +0300 Subject: [PATCH] Java inspection: Simplify quick fix in "Add Braces" and "Remove Braces" inspections, in order to avoid exceptions in the inspection tool UI. (IDEA-157727) --- ...lFlowStatementWithoutBracesInspection.java | 26 ++++++---------- .../SingleStatementInBlockInspection.java | 31 ++++++------------- 2 files changed, 18 insertions(+), 39 deletions(-) diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ControlFlowStatementWithoutBracesInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ControlFlowStatementWithoutBracesInspection.java index dad3f7d279f5..83c636e6f932 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ControlFlowStatementWithoutBracesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/ControlFlowStatementWithoutBracesInspection.java @@ -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; } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java index 9b58f013782e..8bea4b5b0c4b 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/SingleStatementInBlockInspection.java @@ -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; } } }