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:
@@ -88,6 +88,7 @@ public class PMDConfiguration extends AbstractConfiguration {
|
|||||||
private int threads = Runtime.getRuntime().availableProcessors();
|
private int threads = Runtime.getRuntime().availableProcessors();
|
||||||
private ClassLoader classLoader = getClass().getClassLoader();
|
private ClassLoader classLoader = getClass().getClassLoader();
|
||||||
private LanguageVersionDiscoverer languageVersionDiscoverer = new LanguageVersionDiscoverer();
|
private LanguageVersionDiscoverer languageVersionDiscoverer = new LanguageVersionDiscoverer();
|
||||||
|
private LanguageVersion forceLanguageVersion;
|
||||||
|
|
||||||
// Rule and source file options
|
// Rule and source file options
|
||||||
private String ruleSets;
|
private String ruleSets;
|
||||||
@@ -210,6 +211,26 @@ public class PMDConfiguration extends AbstractConfiguration {
|
|||||||
return languageVersionDiscoverer;
|
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.
|
* Set the given LanguageVersion as the current default for it's Language.
|
||||||
*
|
*
|
||||||
|
@@ -203,8 +203,10 @@ public class SourceCodeProcessor {
|
|||||||
private void determineLanguage(RuleContext ctx) {
|
private void determineLanguage(RuleContext ctx) {
|
||||||
// If LanguageVersion of the source file is not known, make a
|
// If LanguageVersion of the source file is not known, make a
|
||||||
// determination
|
// determination
|
||||||
if (ctx.getLanguageVersion() == null) {
|
LanguageVersion languageVersion = ctx.getLanguageVersion();
|
||||||
LanguageVersion languageVersion = configuration.getLanguageVersionOfFile(ctx.getSourceCodeFilename());
|
if (languageVersion == null) {
|
||||||
|
languageVersion = configuration.getForceLanguageVersion();
|
||||||
|
languageVersion = languageVersion != null ? languageVersion : configuration.getLanguageVersionOfFile(ctx.getSourceCodeFilename());
|
||||||
ctx.setLanguageVersion(languageVersion);
|
ctx.setLanguageVersion(languageVersion);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -101,6 +101,9 @@ public class PMDParameters {
|
|||||||
@Parameter(names = { "-language", "-l" }, description = "Specify a language PMD should use.")
|
@Parameter(names = { "-language", "-l" }, description = "Specify a language PMD should use.")
|
||||||
private String language = null;
|
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",
|
@Parameter(names = "-auxclasspath",
|
||||||
description = "Specifies the classpath for libraries used by the source code. "
|
description = "Specifies the classpath for libraries used by the source code. "
|
||||||
+ "This is used by the type resolution. The platform specific path delimiter "
|
+ "This is used by the type resolution. The platform specific path delimiter "
|
||||||
@@ -215,8 +218,11 @@ public class PMDParameters {
|
|||||||
configuration.setIgnoreIncrementalAnalysis(this.isIgnoreIncrementalAnalysis());
|
configuration.setIgnoreIncrementalAnalysis(this.isIgnoreIncrementalAnalysis());
|
||||||
|
|
||||||
LanguageVersion languageVersion = LanguageRegistry
|
LanguageVersion languageVersion = LanguageRegistry
|
||||||
.findLanguageVersionByTerseName(this.getLanguage() + ' ' + this.getVersion());
|
.findLanguageVersionByTerseName(forceLanguage != null ? forceLanguage : (this.getLanguage()) + ' ' + this.getVersion());
|
||||||
if (languageVersion != null) {
|
if (languageVersion != null) {
|
||||||
|
if (forceLanguage != null) {
|
||||||
|
configuration.setForceLanguageVersion(languageVersion);
|
||||||
|
}
|
||||||
configuration.getLanguageVersionDiscoverer().setDefaultLanguageVersion(languageVersion);
|
configuration.getLanguageVersionDiscoverer().setDefaultLanguageVersion(languageVersion);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user