use of com.puppycrawl.tools.checkstyle.PropertiesExpander in project maven-plugins by apache.
the class DefaultCheckstyleExecutor method getConfiguration.
public Configuration getConfiguration(CheckstyleExecutorRequest request) throws CheckstyleExecutorException {
try {
// Checkstyle will always use the context classloader in order
// to load resources (dtds),
// so we have to fix it
ClassLoader checkstyleClassLoader = PackageNamesLoader.class.getClassLoader();
Thread.currentThread().setContextClassLoader(checkstyleClassLoader);
String configFile = getConfigFile(request);
Properties overridingProperties = getOverridingProperties(request);
Configuration config = ConfigurationLoader.loadConfiguration(configFile, new PropertiesExpander(overridingProperties), request.isOmitIgnoredModules());
String effectiveEncoding = StringUtils.isNotEmpty(request.getEncoding()) ? request.getEncoding() : System.getProperty("file.encoding", "UTF-8");
if (StringUtils.isEmpty(request.getEncoding())) {
getLogger().warn("File encoding has not been set, using platform encoding " + effectiveEncoding + ", i.e. build is platform dependent!");
}
// MCHECKSTYLE-332 Checkstyle 6.16+ (#569): the cache is moved to the Checker module instead of TreeWalker
boolean cacheInChecker = false;
for (Method method : Checker.class.getMethods()) {
if ("setCacheFile".equals(method.getName()) && Arrays.equals(method.getParameterTypes(), new Class<?>[] { String.class })) {
cacheInChecker = true;
break;
}
}
if ("Checker".equals(config.getName()) || "com.puppycrawl.tools.checkstyle.Checker".equals(config.getName())) {
if (config instanceof DefaultConfiguration) {
// MCHECKSTYLE-173 Only add the "charset" attribute if it has not been set
addAttributeIfNotExists((DefaultConfiguration) config, "charset", effectiveEncoding);
if (cacheInChecker) {
addAttributeIfNotExists((DefaultConfiguration) config, "cacheFile", request.getCacheFile());
}
} else {
getLogger().warn("Failed to configure file encoding on module " + config);
}
}
Configuration[] modules = config.getChildren();
for (Configuration module : modules) {
if ("TreeWalker".equals(module.getName()) || "com.puppycrawl.tools.checkstyle.TreeWalker".equals(module.getName())) {
if (module instanceof DefaultConfiguration) {
if (!cacheInChecker) {
addAttributeIfNotExists((DefaultConfiguration) module, "cacheFile", request.getCacheFile());
}
} else {
getLogger().warn("Failed to configure cache file on module " + module);
}
}
}
return config;
} catch (CheckstyleException e) {
throw new CheckstyleExecutorException("Failed during checkstyle configuration", e);
}
}
Aggregations