Ensured that check's required tokens are subset of default tokens, issue #655
This commit is contained in:
parent
efac6bf023
commit
afd9a5c3e5
|
|
@ -247,6 +247,7 @@ public final class TreeWalker
|
|||
*/
|
||||
private void registerCheck(Check check)
|
||||
throws CheckstyleException {
|
||||
validateDefaultTokens(check);
|
||||
final int[] tokens;
|
||||
final Set<String> checkTokens = check.getTokenNames();
|
||||
if (checkTokens.isEmpty()) {
|
||||
|
|
@ -281,6 +282,24 @@ public final class TreeWalker
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that check's required tokens are subset of default tokens.
|
||||
* @param check to validate
|
||||
* @throws CheckstyleException when validation of default tokens fails
|
||||
*/
|
||||
private static void validateDefaultTokens(Check check) throws CheckstyleException {
|
||||
final int[] defaultTokens = check.getDefaultTokens();
|
||||
if (check.getRequiredTokens().length != 0) {
|
||||
Arrays.sort(defaultTokens);
|
||||
for (final int token : check.getRequiredTokens()) {
|
||||
if (Arrays.binarySearch(defaultTokens, token) < 0) {
|
||||
throw new CheckstyleException("Token \"" + token + "\" from required tokens was"
|
||||
+ " not found in default tokens list in check " + check);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a check for a specified token id.
|
||||
* @param tokenID the id of the token
|
||||
|
|
|
|||
|
|
@ -287,7 +287,41 @@ public class TreeWalkerTest extends BaseCheckTestSupport {
|
|||
treeWalker.processFiltered(file, lines);
|
||||
}
|
||||
|
||||
private static class BadJavaDocCheck extends Check {
|
||||
@Test
|
||||
public void testRequiredTokenIsNotInDefaultTokens() throws Exception {
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(RequiredTokenIsNotInDefaultsCheck.class);
|
||||
final String pathToEmptyFile = temporaryFolder.newFile("file.java").getPath();
|
||||
final String[] expected = {
|
||||
};
|
||||
|
||||
try {
|
||||
verify(checkConfig, pathToEmptyFile, expected);
|
||||
fail();
|
||||
}
|
||||
catch (CheckstyleException ignored) {
|
||||
//expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRequiredTokenIsEmptyIntArray() throws Exception {
|
||||
final DefaultConfiguration checkConfig =
|
||||
createCheckConfig(RequiredTokenIsEmptyIntArray.class);
|
||||
final String pathToEmptyFile = temporaryFolder.newFile("file.java").getPath();
|
||||
final String[] expected = {
|
||||
};
|
||||
|
||||
try {
|
||||
verify(checkConfig, pathToEmptyFile, expected);
|
||||
}
|
||||
catch (CheckstyleException ignored) {
|
||||
// unexpected
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
public static class BadJavaDocCheck extends Check {
|
||||
@Override
|
||||
public int[] getDefaultTokens() {
|
||||
return getAcceptableTokens();
|
||||
|
|
@ -304,4 +338,38 @@ public class TreeWalkerTest extends BaseCheckTestSupport {
|
|||
}
|
||||
}
|
||||
|
||||
public static class RequiredTokenIsNotInDefaultsCheck extends Check {
|
||||
@Override
|
||||
public int[] getRequiredTokens() {
|
||||
return new int[] {TokenTypes.ASSIGN};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getDefaultTokens() {
|
||||
return new int[] {TokenTypes.ANNOTATION};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getAcceptableTokens() {
|
||||
return ArrayUtils.EMPTY_INT_ARRAY;
|
||||
}
|
||||
}
|
||||
|
||||
public static class RequiredTokenIsEmptyIntArray extends Check {
|
||||
@Override
|
||||
public int[] getRequiredTokens() {
|
||||
return ArrayUtils.EMPTY_INT_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getDefaultTokens() {
|
||||
return new int[] {TokenTypes.ANNOTATION};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getAcceptableTokens() {
|
||||
return ArrayUtils.EMPTY_INT_ARRAY;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue