Skip to content

Commit

Permalink
Issue checkstyle#4370: Add multi thread mode to checkstyle launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
soon committed Jun 22, 2017
1 parent d1ecad1 commit 8bf8122
Show file tree
Hide file tree
Showing 21 changed files with 858 additions and 36 deletions.
1 change: 1 addition & 0 deletions config/import-control.xml
Expand Up @@ -93,6 +93,7 @@
<allow class="com.google.common.collect.ImmutableMap" local-only="true"/>

<allow class="com.puppycrawl.tools.checkstyle.Checker" local-only="true"/>
<allow class="com.puppycrawl.tools.checkstyle.MultiThreadModeSettings" local-only="true"/>
<!-- allowed till https://github.com/checkstyle/checkstyle/issues/3817 -->
<allow pkg="com.puppycrawl.tools.checkstyle.utils"/>
</subpackage>
Expand Down
6 changes: 5 additions & 1 deletion pom.xml
Expand Up @@ -1916,7 +1916,7 @@
<configuration>
<targetClasses>
<param>com.puppycrawl.tools.checkstyle.AuditEventDefaultFormatter</param>
<param>com.puppycrawl.tools.checkstyle.ConfigurationLoader</param>
<param>com.puppycrawl.tools.checkstyle.ConfigurationLoader*</param>
<param>com.puppycrawl.tools.checkstyle.PackageNamesLoader</param>
<param>com.puppycrawl.tools.checkstyle.DefaultConfiguration</param>
<param>com.puppycrawl.tools.checkstyle.DefaultContext</param>
Expand All @@ -1929,6 +1929,8 @@
<param>com.puppycrawl.tools.checkstyle.Checker</param>
<param>com.puppycrawl.tools.checkstyle.ant.*</param>
<param>com.puppycrawl.tools.checkstyle.doclets.*</param>
<param>com.puppycrawl.tools.checkstyle.MultiThreadModuleResolver</param>
<param>com.puppycrawl.tools.checkstyle.MultiThreadModeSettings</param>
</targetClasses>
<targetTests>
<param>com.puppycrawl.tools.checkstyle.AuditEventDefaultFormatterTest</param>
Expand All @@ -1944,6 +1946,8 @@
<param>com.puppycrawl.tools.checkstyle.CheckerTest</param>
<param>com.puppycrawl.tools.checkstyle.ant.*</param>
<param>com.puppycrawl.tools.checkstyle.doclets.*</param>
<param>com.puppycrawl.tools.checkstyle.MultiThreadModuleResolverTest</param>
<param>com.puppycrawl.tools.checkstyle.MultiThreadModeConfigurationTest</param>
</targetTests>
<excludedMethods>
<!-- till https://github.com/hcoles/pitest/issues/353 -->
Expand Down
Expand Up @@ -44,6 +44,7 @@
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.ConfigurationLoader;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.MultiThreadModuleResolver;
import com.puppycrawl.tools.checkstyle.PropertiesExpander;
import com.puppycrawl.tools.checkstyle.TreeWalker;
import com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter;
Expand All @@ -70,8 +71,10 @@ public class BaseCheckTestSupport {
*/
protected static Configuration getConfiguration() throws CheckstyleException {
if (configuration == null) {
final MultiThreadModuleResolver moduleResolver =
MultiThreadModuleResolver.singleThreadMode();
configuration = ConfigurationLoader.loadConfiguration(XML_NAME, new PropertiesExpander(
System.getProperties()));
System.getProperties()), moduleResolver);
}

return configuration;
Expand Down
142 changes: 128 additions & 14 deletions src/main/java/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java
Expand Up @@ -97,29 +97,41 @@ public final class ConfigurationLoader {

/** Property resolver. **/
private final PropertyResolver overridePropsResolver;

/** Module resolver. */
private final ModuleResolver moduleResolver;

/** The loaded configurations. **/
private final Deque<DefaultConfiguration> configStack = new ArrayDeque<>();

/** Flags if modules with the severity 'ignore' should be omitted. */
private final boolean omitIgnoredModules;

/** The multi thread mode configuration. */
private final MultiThreadModeSettings multiThreadModeSettings;

/** The Configuration that is being built. */
private Configuration configuration;

/**
* Creates a new {@code ConfigurationLoader} instance.
* @param overrideProps resolver for overriding properties
* @param moduleResolver resolver for overriding module names
* @param omitIgnoredModules {@code true} if ignored modules should be
* omitted
* @param multiThreadModeSettings the multi thread mode configuration
* @throws ParserConfigurationException if an error occurs
* @throws SAXException if an error occurs
*/
private ConfigurationLoader(final PropertyResolver overrideProps,
final boolean omitIgnoredModules)
final ModuleResolver moduleResolver, final boolean omitIgnoredModules,
final MultiThreadModeSettings multiThreadModeSettings)
throws ParserConfigurationException, SAXException {
saxHandler = new InternalLoader();
overridePropsResolver = overrideProps;
this.omitIgnoredModules = omitIgnoredModules;
this.multiThreadModeSettings = multiThreadModeSettings;
this.moduleResolver = moduleResolver;
}

/**
Expand Down Expand Up @@ -159,27 +171,96 @@ private void parseInputSource(InputSource source)
*/
public static Configuration loadConfiguration(String config,
PropertyResolver overridePropsResolver) throws CheckstyleException {
return loadConfiguration(config, overridePropsResolver, false);
return loadConfiguration(config, overridePropsResolver, null, false);
}

/**
* Returns the module configurations in a specified file.
* @param config location of config file, can be either a URL or a filename
* @param overridePropsResolver overriding properties
* @param moduleResolver overriding module
* @return the check configurations
* @throws CheckstyleException if an error occurs
*/
public static Configuration loadConfiguration(String config,
PropertyResolver overridePropsResolver, ModuleResolver moduleResolver)
throws CheckstyleException {
return loadConfiguration(config, overridePropsResolver, moduleResolver, false);
}

/**
* Returns the module configurations in a specified file.
* @param config location of config file, can be either a URL or a filename
* @param overridePropsResolver overriding properties
* @param moduleResolver overriding properties
* @param multiThreadModeSettings the multi thread mode configuration
* @return the check configurations
* @throws CheckstyleException if an error occurs
*/
public static Configuration loadConfiguration(String config,
PropertyResolver overridePropsResolver, ModuleResolver moduleResolver,
MultiThreadModeSettings multiThreadModeSettings) throws CheckstyleException {
return loadConfiguration(config, overridePropsResolver, moduleResolver,
false, multiThreadModeSettings);
}

/**
* Returns the module configurations in a specified file.
*
* @param config location of config file, can be either a URL or a filename
* @param overridePropsResolver overriding properties
* @param omitIgnoredModules {@code true} if modules with severity
* 'ignore' should be omitted, {@code false} otherwise
* @return the check configurations
* @throws CheckstyleException if an error occurs
*/
public static Configuration loadConfiguration(String config,
PropertyResolver overridePropsResolver, boolean omitIgnoredModules)
throws CheckstyleException {
return loadConfiguration(config, overridePropsResolver,
MultiThreadModuleResolver.singleThreadMode(), omitIgnoredModules);
}

/**
* Returns the module configurations in a specified file.
*
* @param config location of config file, can be either a URL or a filename
* @param overridePropsResolver overriding properties
* @param moduleResolver overriding modules
* @param omitIgnoredModules {@code true} if modules with severity
* 'ignore' should be omitted, {@code false} otherwise
* @return the check configurations
* @throws CheckstyleException if an error occurs
*/
public static Configuration loadConfiguration(String config,
PropertyResolver overridePropsResolver, boolean omitIgnoredModules)
PropertyResolver overridePropsResolver, ModuleResolver moduleResolver,
boolean omitIgnoredModules)
throws CheckstyleException {
return loadConfiguration(config, overridePropsResolver, moduleResolver,
omitIgnoredModules, MultiThreadModeSettings.getSingleThreadMode());
}

/**
* Returns the module configurations in a specified file.
*
* @param config location of config file, can be either a URL or a filename
* @param overridePropsResolver overriding properties
* @param moduleResolver overriding modules
* @param omitIgnoredModules {@code true} if modules with severity
* 'ignore' should be omitted, {@code false} otherwise
* @param multiThreadModeSettings the multi thread mode configuration
* @return the check configurations
* @throws CheckstyleException if an error occurs
*/
public static Configuration loadConfiguration(String config,
PropertyResolver overridePropsResolver, ModuleResolver moduleResolver,
boolean omitIgnoredModules, MultiThreadModeSettings multiThreadModeSettings)
throws CheckstyleException {
// figure out if this is a File or a URL
final URI uri = CommonUtils.getUriByFilename(config);
final InputSource source = new InputSource(uri.toString());
return loadConfiguration(source, overridePropsResolver,
omitIgnoredModules);
return loadConfiguration(source, overridePropsResolver, moduleResolver,
omitIgnoredModules, multiThreadModeSettings);
}

/**
Expand All @@ -188,23 +269,25 @@ public static Configuration loadConfiguration(String config,
*
* @param configStream the input stream to the Checkstyle configuration
* @param overridePropsResolver overriding properties
* @param moduleResolver overriding modules
* @param omitIgnoredModules {@code true} if modules with severity
* 'ignore' should be omitted, {@code false} otherwise
* @return the check configurations
* @throws CheckstyleException if an error occurs
*
* @deprecated As this method does not provide a valid system ID,
* preventing resolution of external entities, a
* {@link #loadConfiguration(InputSource,PropertyResolver,boolean)
* {@link #loadConfiguration(InputSource, PropertyResolver, ModuleResolver, boolean)
* version using an InputSource}
* should be used instead
*/
@Deprecated
public static Configuration loadConfiguration(InputStream configStream,
PropertyResolver overridePropsResolver, boolean omitIgnoredModules)
PropertyResolver overridePropsResolver, ModuleResolver moduleResolver,
boolean omitIgnoredModules)
throws CheckstyleException {
return loadConfiguration(new InputSource(configStream),
overridePropsResolver, omitIgnoredModules);
return loadConfiguration(new InputSource(configStream), overridePropsResolver,
moduleResolver, omitIgnoredModules);
}

/**
Expand All @@ -214,18 +297,42 @@ public static Configuration loadConfiguration(InputStream configStream,
*
* @param configSource the input stream to the Checkstyle configuration
* @param overridePropsResolver overriding properties
* @param moduleResolver overriding modules
* @param omitIgnoredModules {@code true} if modules with severity
* 'ignore' should be omitted, {@code false} otherwise
* @return the check configurations
* @throws CheckstyleException if an error occurs
*/
public static Configuration loadConfiguration(InputSource configSource,
PropertyResolver overridePropsResolver, boolean omitIgnoredModules)
PropertyResolver overridePropsResolver, ModuleResolver moduleResolver,
boolean omitIgnoredModules)
throws CheckstyleException {
return loadConfiguration(configSource, overridePropsResolver, moduleResolver,
omitIgnoredModules, MultiThreadModeSettings.getSingleThreadMode());
}

/**
* Returns the module configurations from a specified input source.
* Note that if the source does wrap an open byte or character
* stream, clients are required to close that stream by themselves
*
* @param configSource the input stream to the Checkstyle configuration
* @param overridePropsResolver overriding properties
* @param moduleResolver resolver for overriding modules
* @param omitIgnoredModules {@code true} if modules with severity
* 'ignore' should be omitted, {@code false} otherwise
* @param multiThreadModeSettings the multi thread mode configuration
* @return the check configurations
* @throws CheckstyleException if an error occurs
*/
public static Configuration loadConfiguration(InputSource configSource,
PropertyResolver overridePropsResolver, ModuleResolver moduleResolver,
boolean omitIgnoredModules, MultiThreadModeSettings multiThreadModeSettings)
throws CheckstyleException {
try {
final ConfigurationLoader loader =
new ConfigurationLoader(overridePropsResolver,
omitIgnoredModules);
new ConfigurationLoader(overridePropsResolver, moduleResolver,
omitIgnoredModules, multiThreadModeSettings);
loader.parseInputSource(configSource);
return loader.configuration;
}
Expand Down Expand Up @@ -411,9 +518,16 @@ public void startElement(String uri,
throws SAXException {
if (qName.equals(MODULE)) {
//create configuration
final String name = attributes.getValue(NAME);
final String originalName = attributes.getValue(NAME);
final String name;
if (moduleResolver == null) {
name = originalName;
}
else {
name = moduleResolver.resolveName(originalName);
}
final DefaultConfiguration conf =
new DefaultConfiguration(name);
new DefaultConfiguration(name, multiThreadModeSettings);

if (configuration == null) {
configuration = conf;
Expand Down
Expand Up @@ -48,12 +48,26 @@ public final class DefaultConfiguration implements Configuration {
/** The map containing custom messages. */
private final Map<String, String> messages = new HashMap<>();

/** The multi thread mode configuration. */
private final MultiThreadModeSettings multiThreadModeSettings;

/**
* Instantiates a DefaultConfiguration.
* @param name the name for this DefaultConfiguration.
*/
public DefaultConfiguration(String name) {
this(name, MultiThreadModeSettings.getSingleThreadMode());
}

/**
* Instantiates a DefaultConfiguration.
* @param name the name for this DefaultConfiguration.
* @param multiThreadModeSettings the multi thread mode configuration.
*/
public DefaultConfiguration(String name,
MultiThreadModeSettings multiThreadModeSettings) {
this.name = name;
this.multiThreadModeSettings = multiThreadModeSettings;
}

@Override
Expand Down Expand Up @@ -131,4 +145,13 @@ public void addMessage(String key, String value) {
public ImmutableMap<String, String> getMessages() {
return ImmutableMap.copyOf(messages);
}

/**
* Gets the multi thread mode configuration.
* @return the multi thread mode configuration.
*/
@Override
public MultiThreadModeSettings getMultiThreadModeSettings() {
return multiThreadModeSettings;
}
}

0 comments on commit 8bf8122

Please sign in to comment.