Support forcing a specific language from the command-line

Store the language version provided by a -force-language command-line argument and use that as the default language before falling back to the filename
This commit is contained in:
Aidan Harding
2021-07-23 11:54:42 +01:00
parent f638ac5642
commit ca52b841ce
4 changed files with 58 additions and 3 deletions

View File

@@ -88,6 +88,7 @@ public class PMDConfiguration extends AbstractConfiguration {
private int threads = Runtime.getRuntime().availableProcessors();
private ClassLoader classLoader = getClass().getClassLoader();
private LanguageVersionDiscoverer languageVersionDiscoverer = new LanguageVersionDiscoverer();
private LanguageVersion forceLanguageVersion;
// Rule and source file options
private String ruleSets;
@@ -210,6 +211,26 @@ public class PMDConfiguration extends AbstractConfiguration {
return languageVersionDiscoverer;
}
/**
* Get the LanguageVersion specified by the force-language parameter. This overrides detection based on file
* extensions
*
* @return The LanguageVersion.
*/
public LanguageVersion getForceLanguageVersion() {
return forceLanguageVersion;
}
/**
* Set the LanguageVersion specified by the force-language parameter. This overrides detection based on file
* extensions
*
* @param forceLanguageVersion the language version
*/
public void setForceLanguageVersion(LanguageVersion forceLanguageVersion) {
this.forceLanguageVersion = forceLanguageVersion;
}
/**
* Set the given LanguageVersion as the current default for it's Language.
*

View File

@@ -203,8 +203,10 @@ public class SourceCodeProcessor {
private void determineLanguage(RuleContext ctx) {
// If LanguageVersion of the source file is not known, make a
// determination
if (ctx.getLanguageVersion() == null) {
LanguageVersion languageVersion = configuration.getLanguageVersionOfFile(ctx.getSourceCodeFilename());
LanguageVersion languageVersion = ctx.getLanguageVersion();
if (languageVersion == null) {
languageVersion = configuration.getForceLanguageVersion();
languageVersion = languageVersion != null ? languageVersion : configuration.getLanguageVersionOfFile(ctx.getSourceCodeFilename());
ctx.setLanguageVersion(languageVersion);
}
}

View File

@@ -101,6 +101,9 @@ public class PMDParameters {
@Parameter(names = { "-language", "-l" }, description = "Specify a language PMD should use.")
private String language = null;
@Parameter(names = "-force-language", description = "Force a language to be used for all input files, irrespective of filenames.")
private String forceLanguage = null;
@Parameter(names = "-auxclasspath",
description = "Specifies the classpath for libraries used by the source code. "
+ "This is used by the type resolution. The platform specific path delimiter "
@@ -215,8 +218,11 @@ public class PMDParameters {
configuration.setIgnoreIncrementalAnalysis(this.isIgnoreIncrementalAnalysis());
LanguageVersion languageVersion = LanguageRegistry
.findLanguageVersionByTerseName(this.getLanguage() + ' ' + this.getVersion());
.findLanguageVersionByTerseName(forceLanguage != null ? forceLanguage : (this.getLanguage()) + ' ' + this.getVersion());
if (languageVersion != null) {
if (forceLanguage != null) {
configuration.setForceLanguageVersion(languageVersion);
}
configuration.getLanguageVersionDiscoverer().setDefaultLanguageVersion(languageVersion);
}
try {

View File

@@ -0,0 +1,26 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang;
import org.junit.Assert;
import org.junit.Test;
import net.sourceforge.pmd.cli.PMDCommandLineInterface;
import net.sourceforge.pmd.cli.PMDParameters;
public class LanguageParameterTest {
/** Test that language parameters from the CLI are correctly passed through to the PMDConfiguration. Although this is a
* CLI test, it resides here to take advantage of {@link net.sourceforge.pmd.lang.DummyLanguageModule}
*/
@Test
public void testLanguageFromCliToConfiguration() {
PMDParameters params = new PMDParameters();
String[] args = { "-d", "source_folder", "-f", "ideaj", "-P", "sourcePath=/home/user/source/", "-R", "java-empty", "-force-language", "dummy"};
PMDCommandLineInterface.extractParameters(params, args, "PMD");
Assert.assertEquals(new DummyLanguageModule().getDefaultVersion().getName(), params.toConfiguration().getForceLanguageVersion().getName());
}
}