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