use of com.puppycrawl.tools.checkstyle.api.AbstractCheck in project checkstyle by checkstyle.
the class TreeWalker method notifyBegin.
/**
* Notify checks that we are about to begin walking a tree.
* @param rootAST the root of the tree.
* @param contents the contents of the file the AST was generated from.
* @param astState state of AST.
*/
private void notifyBegin(DetailAST rootAST, FileContents contents, AstState astState) {
final Set<AbstractCheck> checks;
if (astState == AstState.WITH_COMMENTS) {
checks = commentChecks;
} else {
checks = ordinaryChecks;
}
for (AbstractCheck check : checks) {
check.setFileContents(contents);
check.beginTree(rootAST);
}
}
use of com.puppycrawl.tools.checkstyle.api.AbstractCheck in project checkstyle by checkstyle.
the class AllChecksTest method validateAllCheckTokensAreReferencedInConfigFile.
private static void validateAllCheckTokensAreReferencedInConfigFile(String configName, Configuration configuration, Map<String, Set<String>> tokensToIgnore) throws Exception {
final ModuleFactory moduleFactory = TestUtils.getPackageObjectFactory();
final Set<Configuration> configChecks = ConfigurationUtil.getChecks(configuration);
final Map<String, Set<String>> configCheckTokens = new HashMap<>();
final Map<String, Set<String>> checkTokens = new HashMap<>();
for (Configuration checkConfig : configChecks) {
final String checkName = checkConfig.getName();
final Object instance;
try {
instance = moduleFactory.createModule(checkName);
} catch (CheckstyleException ex) {
throw new CheckstyleException("Couldn't find check: " + checkName, ex);
}
if (instance instanceof AbstractCheck) {
final AbstractCheck check = (AbstractCheck) instance;
Set<String> configTokens = configCheckTokens.get(checkName);
if (configTokens == null) {
configTokens = new HashSet<>();
configCheckTokens.put(checkName, configTokens);
// add all overriden tokens
final Set<String> overrideTokens = tokensToIgnore.get(checkName);
if (overrideTokens != null) {
configTokens.addAll(overrideTokens);
}
configTokens.addAll(CheckUtil.getTokenNameSet(check.getDefaultTokens()));
checkTokens.put(checkName, CheckUtil.getTokenNameSet(check.getAcceptableTokens()));
}
try {
configTokens.addAll(Arrays.asList(checkConfig.getAttribute("tokens").trim().split(",\\s*")));
} catch (CheckstyleException ex) {
// no tokens defined, so it is using default
}
}
}
for (Entry<String, Set<String>> entry : checkTokens.entrySet()) {
Assert.assertEquals("'" + entry.getKey() + "' should have all acceptable tokens from check in " + configName + " config or specify an override to ignore the specific tokens", entry.getValue(), configCheckTokens.get(entry.getKey()));
}
}
use of com.puppycrawl.tools.checkstyle.api.AbstractCheck in project checkstyle by checkstyle.
the class AllChecksTest method testRequiredTokensAreSubsetOfDefaultTokens.
@Test
public void testRequiredTokensAreSubsetOfDefaultTokens() throws Exception {
for (Class<?> check : CheckUtil.getCheckstyleChecks()) {
if (AbstractCheck.class.isAssignableFrom(check)) {
final AbstractCheck testedCheck = (AbstractCheck) check.getDeclaredConstructor().newInstance();
final int[] defaultTokens = testedCheck.getDefaultTokens();
final int[] requiredTokens = testedCheck.getRequiredTokens();
if (!isSubset(requiredTokens, defaultTokens)) {
final String errorMessage = String.format(Locale.ROOT, "%s's required tokens must be a subset" + " of default tokens.", check.getName());
Assert.fail(errorMessage);
}
}
}
}
Aggregations