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;
}
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;
}
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();
}
}
}
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;
}
Aggregations