Search in sources :

Example 6 with AbstractCheck

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);
    }
}
Also used : AbstractCheck(com.puppycrawl.tools.checkstyle.api.AbstractCheck)

Example 7 with AbstractCheck

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()));
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) HashMap(java.util.HashMap) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) ModuleFactory(com.puppycrawl.tools.checkstyle.ModuleFactory) AbstractCheck(com.puppycrawl.tools.checkstyle.api.AbstractCheck)

Example 8 with AbstractCheck

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);
            }
        }
    }
}
Also used : AbstractCheck(com.puppycrawl.tools.checkstyle.api.AbstractCheck) Test(org.junit.Test)

Aggregations

AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)8 Test (org.junit.Test)4 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)3 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)2 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)2 AbstractJavadocCheck (com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck)2 PropertyDescriptor (java.beans.PropertyDescriptor)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Node (org.w3c.dom.Node)2 Checker (com.puppycrawl.tools.checkstyle.Checker)1 ConfigurationLoader (com.puppycrawl.tools.checkstyle.ConfigurationLoader)1 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)1 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)1 AbstractFileSetCheck (com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck)1 Scope (com.puppycrawl.tools.checkstyle.api.Scope)1 SeverityLevel (com.puppycrawl.tools.checkstyle.api.SeverityLevel)1 AccessModifier (com.puppycrawl.tools.checkstyle.checks.naming.AccessModifier)1 File (java.io.File)1