Search in sources :

Example 11 with PropertiesExpander

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);
    }
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) URLClassLoader(java.net.URLClassLoader) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Method(java.lang.reflect.Method) Properties(java.util.Properties)

Aggregations

PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)11 Properties (java.util.Properties)11 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)8 Checker (com.puppycrawl.tools.checkstyle.Checker)5 File (java.io.File)5 Test (org.junit.jupiter.api.Test)5 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)4 URI (java.net.URI)4 InputSource (org.xml.sax.InputSource)4 DefaultLogger (com.puppycrawl.tools.checkstyle.DefaultLogger)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 StringReader (java.io.StringReader)2 ConfigurationLoader (com.puppycrawl.tools.checkstyle.ConfigurationLoader)1 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)1 ModuleFactory (com.puppycrawl.tools.checkstyle.ModuleFactory)1 PackageObjectFactory (com.puppycrawl.tools.checkstyle.PackageObjectFactory)1 ThreadModeSettings (com.puppycrawl.tools.checkstyle.ThreadModeSettings)1 RootModule (com.puppycrawl.tools.checkstyle.api.RootModule)1 Method (java.lang.reflect.Method)1 URLClassLoader (java.net.URLClassLoader)1