Search in sources :

Example 1 with ModuleFactory

use of com.puppycrawl.tools.checkstyle.ModuleFactory in project methods-distance by sevntu-checkstyle.

the class ReportCli method main.

public static void main(String... args) throws CheckstyleException {
    final DependencyInformationConsumer consumer = new ViolationReporterDependencyInformationConsumer();
    final ModuleFactory moduleFactory = new DependencyInformationConsumerInjector(consumer);
    final DefaultConfiguration moduleConfig = new DefaultConfiguration(MethodCallDependencyCheckstyleModule.class.getCanonicalName());
    moduleConfig.addAttribute("screenLinesCount", "50");
    final TreeWalker tw = new TreeWalker();
    tw.setModuleFactory(moduleFactory);
    tw.finishLocalSetup();
    tw.setupChild(moduleConfig);
    final AuditListener listener = new DefaultLogger(System.out, false);
    final Checker checker = new Checker();
    checker.setModuleFactory(moduleFactory);
    checker.addFileSetCheck(tw);
    checker.addListener(listener);
    final List<File> files = Collections.singletonList(new File(args[0]));
    checker.process(files);
}
Also used : ModuleFactory(com.puppycrawl.tools.checkstyle.ModuleFactory) Checker(com.puppycrawl.tools.checkstyle.Checker) ViolationReporterDependencyInformationConsumer(com.github.sevntu.checkstyle.module.ViolationReporterDependencyInformationConsumer) DependencyInformationConsumerInjector(com.github.sevntu.checkstyle.common.DependencyInformationConsumerInjector) TreeWalker(com.puppycrawl.tools.checkstyle.TreeWalker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) MethodCallDependencyCheckstyleModule(com.github.sevntu.checkstyle.module.MethodCallDependencyCheckstyleModule) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) ViolationReporterDependencyInformationConsumer(com.github.sevntu.checkstyle.module.ViolationReporterDependencyInformationConsumer) DependencyInformationConsumer(com.github.sevntu.checkstyle.module.DependencyInformationConsumer) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger) File(java.io.File)

Example 2 with ModuleFactory

use of com.puppycrawl.tools.checkstyle.ModuleFactory in project checkstyle by checkstyle.

the class CheckstyleAntTask method createRootModule.

/**
 * Creates new instance of the root module.
 *
 * @return new instance of the root module
 * @throws BuildException if the root module could not be created.
 */
private RootModule createRootModule() {
    final RootModule rootModule;
    try {
        final Properties props = createOverridingProperties();
        final ThreadModeSettings threadModeSettings = ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE;
        final ConfigurationLoader.IgnoredModulesOptions ignoredModulesOptions;
        if (executeIgnoredModules) {
            ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.EXECUTE;
        } else {
            ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.OMIT;
        }
        final Configuration configuration = ConfigurationLoader.loadConfiguration(config, new PropertiesExpander(props), ignoredModulesOptions, threadModeSettings);
        final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
        final ModuleFactory factory = new PackageObjectFactory(Checker.class.getPackage().getName() + ".", moduleClassLoader);
        rootModule = (RootModule) factory.createModule(configuration.getName());
        rootModule.setModuleClassLoader(moduleClassLoader);
        rootModule.configure(configuration);
    } catch (final CheckstyleException ex) {
        throw new BuildException(String.format(Locale.ROOT, "Unable to create Root Module: " + "config {%s}, classpath {%s}.", config, classpath), ex);
    }
    return rootModule;
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Properties(java.util.Properties) ModuleFactory(com.puppycrawl.tools.checkstyle.ModuleFactory) ThreadModeSettings(com.puppycrawl.tools.checkstyle.ThreadModeSettings) RootModule(com.puppycrawl.tools.checkstyle.api.RootModule) BuildException(org.apache.tools.ant.BuildException) PackageObjectFactory(com.puppycrawl.tools.checkstyle.PackageObjectFactory) ConfigurationLoader(com.puppycrawl.tools.checkstyle.ConfigurationLoader)

Example 3 with ModuleFactory

use of com.puppycrawl.tools.checkstyle.ModuleFactory in project checkstyle by checkstyle.

the class XdocsPagesTest method testAllCheckSections.

@Test
public void testAllCheckSections() throws Exception {
    final ModuleFactory moduleFactory = TestUtil.getPackageObjectFactory();
    for (Path path : XdocUtil.getXdocsConfigFilePaths(XdocUtil.getXdocsFilePaths())) {
        final String fileName = path.getFileName().toString();
        if ("config_system_properties.xml".equals(fileName)) {
            continue;
        }
        final String input = Files.readString(path);
        final Document document = XmlUtil.getRawXml(fileName, input, input);
        final NodeList sources = document.getElementsByTagName("section");
        String lastSectionName = null;
        for (int position = 0; position < sources.getLength(); position++) {
            final Node section = sources.item(position);
            final String sectionName = XmlUtil.getNameAttributeOfNode(section);
            if ("Content".equals(sectionName) || "Overview".equals(sectionName)) {
                assertWithMessage(fileName + " section '" + sectionName + "' should be first").that(lastSectionName).isNull();
                continue;
            }
            assertWithMessage(fileName + " section '" + sectionName + "' shouldn't end with 'Check'").that(sectionName.endsWith("Check")).isFalse();
            if (lastSectionName != null) {
                assertWithMessage(fileName + " section '" + sectionName + "' is out of order compared to '" + lastSectionName + "'").that(sectionName.toLowerCase(Locale.ENGLISH).compareTo(lastSectionName.toLowerCase(Locale.ENGLISH)) >= 0).isTrue();
            }
            validateCheckSection(moduleFactory, fileName, sectionName, section);
            lastSectionName = sectionName;
        }
    }
}
Also used : Path(java.nio.file.Path) ModuleFactory(com.puppycrawl.tools.checkstyle.ModuleFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Test(org.junit.jupiter.api.Test)

Example 4 with ModuleFactory

use of com.puppycrawl.tools.checkstyle.ModuleFactory in project checkstyle by checkstyle.

the class AllChecksTest method validateAllCheckTokensAreReferencedInConfigFile.

private static void validateAllCheckTokensAreReferencedInConfigFile(String configName, Configuration configuration, Map<String, Set<String>> tokensToIgnore, boolean defaultTokensMustBeExplicit) throws Exception {
    final ModuleFactory moduleFactory = TestUtil.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);
        }
        final AbstractCheck check;
        if (instance instanceof AbstractCheck && !isAllTokensAcceptable((AbstractCheck) instance)) {
            check = (AbstractCheck) instance;
        } else {
            // we can not have in our config test for all tokens
            continue;
        }
        Set<String> configTokens = configCheckTokens.get(checkName);
        if (configTokens == null) {
            configTokens = new HashSet<>();
            configCheckTokens.put(checkName, configTokens);
            // add all overridden tokens
            final Set<String> overrideTokens = tokensToIgnore.get(checkName);
            if (overrideTokens != null) {
                configTokens.addAll(overrideTokens);
            }
            configTokens.addAll(CheckUtil.getTokenNameSet(check.getRequiredTokens()));
            checkTokens.put(checkName, CheckUtil.getTokenNameSet(check.getAcceptableTokens()));
        }
        try {
            configTokens.addAll(Arrays.asList(checkConfig.getProperty("tokens").trim().split(",\\s*")));
        } catch (CheckstyleException ex) {
            // no tokens defined, so it is using default
            if (defaultTokensMustBeExplicit) {
                validateDefaultTokens(checkConfig, check, configTokens);
            } else {
                configTokens.addAll(CheckUtil.getTokenNameSet(check.getDefaultTokens()));
            }
        }
    }
    for (Entry<String, Set<String>> entry : checkTokens.entrySet()) {
        final Set<String> actual = configCheckTokens.get(entry.getKey());
        assertWithMessage("'" + entry.getKey() + "' should have all acceptable tokens from check in " + configName + " config or specify an override to ignore the specific tokens").that(actual).isEqualTo(entry.getValue());
    }
}
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 5 with ModuleFactory

use of com.puppycrawl.tools.checkstyle.ModuleFactory 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)

Aggregations

ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)7 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)3 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)3 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)3 Path (java.nio.file.Path)3 Test (org.junit.jupiter.api.Test)3 Document (org.w3c.dom.Document)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)3 Checker (com.puppycrawl.tools.checkstyle.Checker)2 AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)2 File (java.io.File)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 DependencyInformationConsumerInjector (com.github.sevntu.checkstyle.common.DependencyInformationConsumerInjector)1 DependencyInformationConsumer (com.github.sevntu.checkstyle.module.DependencyInformationConsumer)1 MethodCallDependencyCheckstyleModule (com.github.sevntu.checkstyle.module.MethodCallDependencyCheckstyleModule)1 ViolationReporterDependencyInformationConsumer (com.github.sevntu.checkstyle.module.ViolationReporterDependencyInformationConsumer)1 ConfigurationLoader (com.puppycrawl.tools.checkstyle.ConfigurationLoader)1