Issue #2451: removed excess hierarchy from MissingSwitchDefaultCheck
This commit is contained in:
parent
bca492cc21
commit
d74f5bb61c
|
|
@ -37,7 +37,7 @@
|
|||
files="AbstractClassNameCheckTest.java|AbstractTypeAwareCheckTest.java|AbstractJavadocCheckTest.java|AbstractViolationReporterTest.java"/>
|
||||
|
||||
<!-- Tone down the checking for test code -->
|
||||
<suppress checks="CyclomaticComplexity" files="[\\/]XDocsPagesTest\.java" lines="341"/>
|
||||
<suppress checks="CyclomaticComplexity" files="[\\/]XDocsPagesTest\.java" lines="333"/>
|
||||
<suppress checks="EmptyBlock" files=".*[\\/]src[\\/]test[\\/]"/>
|
||||
<suppress checks="ImportControl" files=".*[\\/]src[\\/](test|it)[\\/]"/>
|
||||
<suppress checks="Javadoc" files=".*[\\/]src[\\/](test|it)[\\/]"/>
|
||||
|
|
|
|||
|
|
@ -45,12 +45,12 @@ public class MissingSwitchDefaultTest extends BaseCheckTestSupport {
|
|||
"missing.switch.default");
|
||||
|
||||
final String[] expected = {
|
||||
"11:9: " + msg,
|
||||
"19:9: " + msg,
|
||||
"23:9: " + msg,
|
||||
"31:13: " + msg,
|
||||
"38:21: " + msg,
|
||||
"42:21: " + msg,
|
||||
"11: " + msg,
|
||||
"19: " + msg,
|
||||
"23: " + msg,
|
||||
"31: " + msg,
|
||||
"38: " + msg,
|
||||
"42: " + msg,
|
||||
};
|
||||
|
||||
final Configuration checkConfig = builder.getCheckConfig("MissingSwitchDefault");
|
||||
|
|
|
|||
|
|
@ -19,9 +19,9 @@
|
|||
|
||||
package com.puppycrawl.tools.checkstyle.checks.coding;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.Check;
|
||||
import com.puppycrawl.tools.checkstyle.api.DetailAST;
|
||||
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
|
||||
import com.puppycrawl.tools.checkstyle.checks.DescendantTokenCheck;
|
||||
import com.puppycrawl.tools.checkstyle.utils.TokenUtils;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
|
@ -44,7 +44,7 @@ import com.puppycrawl.tools.checkstyle.utils.TokenUtils;
|
|||
* </pre>
|
||||
* @author o_sukhodolsky
|
||||
*/
|
||||
public class MissingSwitchDefaultCheck extends DescendantTokenCheck {
|
||||
public class MissingSwitchDefaultCheck extends Check {
|
||||
|
||||
/**
|
||||
* A key is pointing to the warning message text in "messages.properties"
|
||||
|
|
@ -52,14 +52,6 @@ public class MissingSwitchDefaultCheck extends DescendantTokenCheck {
|
|||
*/
|
||||
public static final String MSG_KEY = "missing.switch.default";
|
||||
|
||||
/** Creates new instance of the check. */
|
||||
public MissingSwitchDefaultCheck() {
|
||||
setLimitedTokens(TokenUtils.getTokenName(TokenTypes.LITERAL_DEFAULT));
|
||||
setMinimumNumber(1);
|
||||
setMaximumDepth(2);
|
||||
setMinimumMessage(MSG_KEY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getDefaultTokens() {
|
||||
return new int[]{TokenTypes.LITERAL_SWITCH};
|
||||
|
|
@ -74,4 +66,34 @@ public class MissingSwitchDefaultCheck extends DescendantTokenCheck {
|
|||
public int[] getRequiredTokens() {
|
||||
return getDefaultTokens();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitToken(DetailAST ast) {
|
||||
final DetailAST firstCaseGroupAst = ast.findFirstToken(TokenTypes.CASE_GROUP);
|
||||
|
||||
if (!containsDefaultSwitch(firstCaseGroupAst)) {
|
||||
log(ast.getLineNo(), MSG_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the case group or its sibling contain the 'default' switch.
|
||||
* @param caseGroupAst first case group to check.
|
||||
* @return true if 'default' switch found.
|
||||
*/
|
||||
private static boolean containsDefaultSwitch(DetailAST caseGroupAst) {
|
||||
DetailAST nextAst = caseGroupAst;
|
||||
boolean found = false;
|
||||
|
||||
while (nextAst != null) {
|
||||
if (nextAst.findFirstToken(TokenTypes.LITERAL_DEFAULT) != null) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
nextAst = nextAst.getNextSibling();
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -131,14 +131,6 @@ public class XDocsPagesTest {
|
|||
"TypeNameCheck.compileFlags",
|
||||
"RegexpCheck.compileFlags",
|
||||
"SuppressionCommentFilter.fileContents",
|
||||
"MissingSwitchDefaultCheck.limitedTokens",
|
||||
"MissingSwitchDefaultCheck.maximumDepth",
|
||||
"MissingSwitchDefaultCheck.maximumMessage",
|
||||
"MissingSwitchDefaultCheck.maximumNumber",
|
||||
"MissingSwitchDefaultCheck.minimumDepth",
|
||||
"MissingSwitchDefaultCheck.minimumMessage",
|
||||
"MissingSwitchDefaultCheck.minimumNumber",
|
||||
"MissingSwitchDefaultCheck.sumTokenCounts",
|
||||
"MissingCtorCheck.limitedTokens",
|
||||
"MissingCtorCheck.maximumDepth",
|
||||
"MissingCtorCheck.maximumMessage",
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ public class MissingSwitchDefaultCheckTest
|
|||
@Test
|
||||
public void testMissingSwitchDefault() throws Exception {
|
||||
final String[] expected = {
|
||||
"17:9: " + getCheckMessage(MSG_KEY, "default"),
|
||||
"17: " + getCheckMessage(MSG_KEY, "default"),
|
||||
};
|
||||
verify(
|
||||
checkConfig,
|
||||
|
|
|
|||
Loading…
Reference in New Issue