From afd9a5c3e56d942eaa7ff6393a2ecb5e9ec2a231 Mon Sep 17 00:00:00 2001 From: Andrei Selkin Date: Thu, 20 Aug 2015 18:06:32 +0300 Subject: [PATCH] Ensured that check's required tokens are subset of default tokens, issue #655 --- .../tools/checkstyle/TreeWalker.java | 19 +++++ .../tools/checkstyle/TreeWalkerTest.java | 70 ++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java index 2809ff783..708710119 100755 --- a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java @@ -247,6 +247,7 @@ public final class TreeWalker */ private void registerCheck(Check check) throws CheckstyleException { + validateDefaultTokens(check); final int[] tokens; final Set 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 diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java index 3bc8d0c41..66be81cd7 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/TreeWalkerTest.java @@ -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; + } + } + }