Search in sources :

Example 1 with RootModule

use of com.puppycrawl.tools.checkstyle.api.RootModule in project checkstyle by checkstyle.

the class Main method runCheckstyle.

/**
     * Executes required Checkstyle actions based on passed parameters.
     * @param cliOptions
     *        pojo object that contains all options
     * @return number of violations of ERROR level
     * @throws FileNotFoundException
     *         when output file could not be found
     * @throws CheckstyleException
     *         when properties file could not be loaded
     */
private static int runCheckstyle(CliOptions cliOptions) throws CheckstyleException, FileNotFoundException {
    // setup the properties
    final Properties props;
    if (cliOptions.propertiesLocation == null) {
        props = System.getProperties();
    } else {
        props = loadProperties(new File(cliOptions.propertiesLocation));
    }
    // create a configuration
    final Configuration config = ConfigurationLoader.loadConfiguration(cliOptions.configLocation, new PropertiesExpander(props));
    // create a listener for output
    final AuditListener listener = createListener(cliOptions.format, cliOptions.outputLocation);
    // create RootModule object and run it
    int errorCounter = 0;
    final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
    final RootModule rootModule = getRootModule(config.getName(), moduleClassLoader);
    try {
        rootModule.setModuleClassLoader(moduleClassLoader);
        rootModule.configure(config);
        rootModule.addListener(listener);
        // run RootModule
        errorCounter = rootModule.process(cliOptions.files);
    } finally {
        rootModule.destroy();
    }
    return errorCounter;
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) RootModule(com.puppycrawl.tools.checkstyle.api.RootModule) Properties(java.util.Properties) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) File(java.io.File)

Example 2 with RootModule

use of com.puppycrawl.tools.checkstyle.api.RootModule 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 RootModule

use of com.puppycrawl.tools.checkstyle.api.RootModule in project checkstyle by checkstyle.

the class CheckstyleAntTask method realExecute.

/**
 * Helper implementation to perform execution.
 *
 * @param checkstyleVersion Checkstyle compile version.
 */
private void realExecute(String checkstyleVersion) {
    // Create the root module
    RootModule rootModule = null;
    try {
        rootModule = createRootModule();
        // setup the listeners
        final AuditListener[] listeners = getListeners();
        for (AuditListener element : listeners) {
            rootModule.addListener(element);
        }
        final SeverityLevelCounter warningCounter = new SeverityLevelCounter(SeverityLevel.WARNING);
        rootModule.addListener(warningCounter);
        processFiles(rootModule, warningCounter, checkstyleVersion);
    } finally {
        if (rootModule != null) {
            rootModule.destroy();
        }
    }
}
Also used : SeverityLevelCounter(com.puppycrawl.tools.checkstyle.api.SeverityLevelCounter) RootModule(com.puppycrawl.tools.checkstyle.api.RootModule) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener)

Example 4 with RootModule

use of com.puppycrawl.tools.checkstyle.api.RootModule in project checkstyle by checkstyle.

the class Main method runCheckstyle.

/**
 * Executes required Checkstyle actions based on passed parameters.
 *
 * @param options user-specified options
 * @param filesToProcess the list of files whose style to check
 * @return number of violations of ERROR level
 * @throws IOException
 *         when output file could not be found
 * @throws CheckstyleException
 *         when properties file could not be loaded
 */
private static int runCheckstyle(CliOptions options, List<File> filesToProcess) throws CheckstyleException, IOException {
    // setup the properties
    final Properties props;
    if (options.propertiesFile == null) {
        props = System.getProperties();
    } else {
        props = loadProperties(options.propertiesFile);
    }
    // create a configuration
    final ThreadModeSettings multiThreadModeSettings = new ThreadModeSettings(CliOptions.CHECKER_THREADS_NUMBER, CliOptions.TREE_WALKER_THREADS_NUMBER);
    final ConfigurationLoader.IgnoredModulesOptions ignoredModulesOptions;
    if (options.executeIgnoredModules) {
        ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.EXECUTE;
    } else {
        ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.OMIT;
    }
    final Configuration config = ConfigurationLoader.loadConfiguration(options.configurationFile, new PropertiesExpander(props), ignoredModulesOptions, multiThreadModeSettings);
    // create RootModule object and run it
    final int errorCounter;
    final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
    final RootModule rootModule = getRootModule(config.getName(), moduleClassLoader);
    try {
        final AuditListener listener;
        if (options.generateXpathSuppressionsFile) {
            // create filter to print generated xpath suppressions file
            final Configuration treeWalkerConfig = getTreeWalkerConfig(config);
            if (treeWalkerConfig != null) {
                final DefaultConfiguration moduleConfig = new DefaultConfiguration(XpathFileGeneratorAstFilter.class.getName());
                moduleConfig.addProperty(CliOptions.ATTRIB_TAB_WIDTH_NAME, String.valueOf(options.tabWidth));
                ((DefaultConfiguration) treeWalkerConfig).addChild(moduleConfig);
            }
            listener = new XpathFileGeneratorAuditListener(getOutputStream(options.outputPath), getOutputStreamOptions(options.outputPath));
        } else {
            listener = createListener(options.format, options.outputPath);
        }
        rootModule.setModuleClassLoader(moduleClassLoader);
        rootModule.configure(config);
        rootModule.addListener(listener);
        // run RootModule
        errorCounter = rootModule.process(filesToProcess);
    } finally {
        rootModule.destroy();
    }
    return errorCounter;
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) Properties(java.util.Properties) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) RootModule(com.puppycrawl.tools.checkstyle.api.RootModule)

Aggregations

RootModule (com.puppycrawl.tools.checkstyle.api.RootModule)4 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)3 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)3 Properties (java.util.Properties)3 Checker (com.puppycrawl.tools.checkstyle.Checker)1 ConfigurationLoader (com.puppycrawl.tools.checkstyle.ConfigurationLoader)1 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)1 PackageObjectFactory (com.puppycrawl.tools.checkstyle.PackageObjectFactory)1 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)1 ThreadModeSettings (com.puppycrawl.tools.checkstyle.ThreadModeSettings)1 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 SeverityLevelCounter (com.puppycrawl.tools.checkstyle.api.SeverityLevelCounter)1 File (java.io.File)1 BuildException (org.apache.tools.ant.BuildException)1