Package net.sourceforge.pmd
Class PMDConfiguration
- java.lang.Object
-
- net.sourceforge.pmd.AbstractConfiguration
-
- net.sourceforge.pmd.PMDConfiguration
-
public class PMDConfiguration extends AbstractConfiguration
This class contains the details for the runtime configuration of a PMD run. You can either create one and set individual fields, or mimic a CLI run by usingextractParameters.There are several aspects to the configuration of PMD.
The aspects related to generic PMD behavior:
- Suppress marker is used in source files to suppress a RuleViolation,
defaults to
PMD.SUPPRESS_MARKER.getSuppressMarker() - The number of threads to create when invoking on multiple files, defaults
one thread per available processor.
getThreads() - A ClassLoader to use when loading classes during Rule processing (e.g.
during type resolution), defaults to ClassLoader of the Configuration class.
getClassLoader() - A means to configure a ClassLoader using a prepended classpath String,
instead of directly setting it programmatically.
prependAuxClasspath(String) - A LanguageVersionDiscoverer instance, which defaults to using the default
LanguageVersion of each Language. Means are provided to change the
LanguageVersion for each Language.
getLanguageVersionDiscoverer()
The aspects related to Rules and Source files are:
- A comma separated list of RuleSets URIs.
getRuleSets() - A minimum priority threshold when loading Rules from RuleSets, defaults
to
RulePriority.LOW.getMinimumPriority() - The character encoding of source files, defaults to the system default as
returned by
System.getProperty("file.encoding").AbstractConfiguration.getSourceEncoding() - A comma separated list of input paths to process for source files. This
may include files, directories, archives (e.g. ZIP files), etc.
getInputPaths() - A flag which controls, whether
RuleSetFactoryCompatibilityfilter should be used or not: #isRuleSetFactoryCompatibilityEnabled;
- The renderer format to use for Reports.
getReportFormat() - The file to which the Report should render.
getReportFile() - An indicator of whether to use File short names in Reports, defaults to
false.isReportShortNames() - The initialization properties to use when creating a Renderer instance.
getReportProperties() - An indicator of whether to show suppressed Rule violations in Reports.
isShowSuppressedViolations()
The aspects related to special PMD behavior are:
- An indicator of whether PMD should log debug information.
AbstractConfiguration.isDebug() - An indicator of whether PMD should perform stress testing behaviors, such
as randomizing the order of file processing.
isStressTest() - An indicator of whether PMD should log benchmarking information.
isBenchmark()
- Suppress marker is used in source files to suppress a RuleViolation,
defaults to
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_SUPPRESS_MARKERThe default suppress marker string.
-
Constructor Summary
Constructors Constructor Description PMDConfiguration()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddInputPath(String inputPath)Deprecated.voidaddInputPath(Path inputPath)Add an input path.voidaddRuleSet(String rulesetPath)Add a new ruleset paths to load when starting the analysis.RenderercreateRenderer()Create a Renderer instance based upon the configured reporting options.RenderercreateRenderer(boolean withReportWriter)Create a Renderer instance based upon the configured reporting options.List<String>getAllInputPaths()Deprecated.AnalysisCachegetAnalysisCache()Retrieves the currently used analysis cache.ClassLoadergetClassLoader()Get the ClassLoader being used by PMD when processing Rules.LanguageVersiongetForceLanguageVersion()Get the LanguageVersion specified by the force-language parameter.PathgetIgnoreFile()StringgetIgnoreFilePath()Deprecated.UsegetIgnoreFile()PathgetInputFile()StringgetInputFilePath()Deprecated.UsegetInputFile()List<Path>getInputPathList()Returns an unmodifiable list.StringgetInputPaths()Deprecated.StringgetInputUri()Deprecated.UsegetUri()LanguageVersionDiscoverergetLanguageVersionDiscoverer()Get the LanguageVersionDiscoverer, used to determine the LanguageVersion of a source file.LanguageVersiongetLanguageVersionOfFile(String fileName)Get the LanguageVersion of the source file with given name.RulePrioritygetMinimumPriority()Get the minimum priority threshold when loading Rules from RuleSets.StringgetReportFile()Deprecated.PathgetReportFilePath()Get the file to which the report should render.StringgetReportFormat()Get the report format.PropertiesgetReportProperties()Get the Report properties.List<String>getRuleSetPaths()Returns the list of ruleset URIs.StringgetRuleSets()Deprecated.StringgetSuppressMarker()Get the suppress marker.intgetThreads()Get the number of threads to use when processing Rules.URIgetUri()Get the input URI to process for source code objects.booleanisBenchmark()Deprecated.This behavior is down to CLI, not part of the core analysis.booleanisFailOnViolation()Whether PMD should exit with status 4 (the default behavior, true) if violations are found or just with 0 (to not break the build, e.g.).booleanisForceLanguageVersion()Is the force-language parameter set to anything?booleanisIgnoreIncrementalAnalysis()Returns whether incremental analysis was explicitly disabled by the user or not.booleanisReportShortNames()Get whether to use File short names in Reports.booleanisRuleSetFactoryCompatibilityEnabled()Checks if the rule set factory compatibility feature is enabled.booleanisShowSuppressedViolations()Get whether the report should show suppressed violations.booleanisStressTest()Deprecated.For removalvoidprependAuxClasspath(String classpath)Prepend the specified classpath like string to the current ClassLoader of the configuration.voidprependClasspath(String classpath)Deprecated.UseprependAuxClasspath(String), which doesn't throw a checkedIOExceptionvoidsetAnalysisCache(AnalysisCache cache)Sets the analysis cache to be used.voidsetAnalysisCacheLocation(String cacheLocation)Sets the location of the analysis cache to be used.voidsetBenchmark(boolean benchmark)Deprecated.This behavior is down to CLI, not part of the core analysis.voidsetClassLoader(ClassLoader classLoader)Set the ClassLoader being used by PMD when processing Rules.voidsetDefaultLanguageVersion(LanguageVersion languageVersion)Set the given LanguageVersion as the current default for it's Language.voidsetDefaultLanguageVersions(List<LanguageVersion> languageVersions)Set the given LanguageVersions as the current default for their Languages.voidsetFailOnViolation(boolean failOnViolation)Sets whether PMD should exit with status 4 (the default behavior, true) if violations are found or just with 0 (to not break the build, e.g.).voidsetForceLanguageVersion(LanguageVersion forceLanguageVersion)Set the LanguageVersion specified by the force-language parameter.voidsetIgnoreFilePath(String ignoreFilePath)Deprecated.voidsetIgnoreFilePath(Path ignoreFilePath)The input file path points to a single file, which contains a comma-separated list of source file names to ignore.voidsetIgnoreIncrementalAnalysis(boolean noCache)Sets whether the user has explicitly disabled incremental analysis or not.voidsetInputFilePath(String inputFilePath)Deprecated.voidsetInputFilePath(Path inputFilePath)The input file path points to a single file, which contains a comma-separated list of source file names to process.voidsetInputPathList(List<Path> inputPaths)Set the input paths to the given list of paths.voidsetInputPaths(String inputPaths)Deprecated.voidsetInputPaths(List<String> inputPaths)Deprecated.voidsetInputUri(String inputUri)Deprecated.UsesetInputUri(URI)voidsetInputUri(URI inputUri)Set the input URI to process for source code objects.voidsetMinimumPriority(RulePriority minimumPriority)Set the minimum priority threshold when loading Rules from RuleSets.voidsetReportFile(String reportFile)Deprecated.voidsetReportFile(Path reportFile)Set the file to which the report should render.voidsetReportFormat(String reportFormat)Set the report format.voidsetReportProperties(Properties reportProperties)Set the Report properties.voidsetReportShortNames(boolean reportShortNames)Set whether to use File short names in Reports.voidsetRuleSetFactoryCompatibilityEnabled(boolean ruleSetFactoryCompatibilityEnabled)Sets the rule set factory compatibility feature enabled/disabled.voidsetRuleSets(String ruleSets)Deprecated.UsesetRuleSets(List)oraddRuleSet(String).voidsetRuleSets(List<String> ruleSetPaths)Sets the list of ruleset paths to load when starting the analysis.voidsetShowSuppressedViolations(boolean showSuppressedViolations)Set whether the report should show suppressed violations.voidsetStressTest(boolean stressTest)Deprecated.For removal.voidsetSuppressMarker(String suppressMarker)Set the suppress marker.voidsetThreads(int threads)Set the number of threads to use when processing Rules.-
Methods inherited from class net.sourceforge.pmd.AbstractConfiguration
getSourceEncoding, isDebug, setDebug, setSourceEncoding
-
-
-
-
Field Detail
-
DEFAULT_SUPPRESS_MARKER
public static final String DEFAULT_SUPPRESS_MARKER
The default suppress marker string.- See Also:
- Constant Field Values
-
-
Method Detail
-
getSuppressMarker
public String getSuppressMarker()
Get the suppress marker. This is the source level marker used to indicate a RuleViolation should be suppressed.- Returns:
- The suppress marker.
-
setSuppressMarker
public void setSuppressMarker(String suppressMarker)
Set the suppress marker.- Parameters:
suppressMarker- The suppress marker to use.
-
getThreads
public int getThreads()
Get the number of threads to use when processing Rules.- Returns:
- The number of threads.
-
setThreads
public void setThreads(int threads)
Set the number of threads to use when processing Rules.- Parameters:
threads- The number of threads.
-
getClassLoader
public ClassLoader getClassLoader()
Get the ClassLoader being used by PMD when processing Rules.- Returns:
- The ClassLoader being used
-
setClassLoader
public void setClassLoader(ClassLoader classLoader)
Set the ClassLoader being used by PMD when processing Rules. Setting a value ofnullwill cause the default ClassLoader to be used.- Parameters:
classLoader- The ClassLoader to use
-
prependClasspath
@Deprecated public void prependClasspath(String classpath) throws IOException
Deprecated.UseprependAuxClasspath(String), which doesn't throw a checkedIOExceptionPrepend the specified classpath like string to the current ClassLoader of the configuration. If no ClassLoader is currently configured, the ClassLoader used to load thePMDConfigurationclass will be used as the parent ClassLoader of the created ClassLoader.If the classpath String looks like a URL to a file (i.e. starts with
file://) the file will be read with each line representing an entry on the classpath.- Parameters:
classpath- The prepended classpath.- Throws:
IOException- if the given classpath is invalid (e.g. does not exist)- See Also:
setClassLoader(ClassLoader),ClasspathClassLoader
-
prependAuxClasspath
public void prependAuxClasspath(String classpath)
Prepend the specified classpath like string to the current ClassLoader of the configuration. If no ClassLoader is currently configured, the ClassLoader used to load thePMDConfigurationclass will be used as the parent ClassLoader of the created ClassLoader.If the classpath String looks like a URL to a file (i.e. starts with
file://) the file will be read with each line representing an entry on the classpath.- Parameters:
classpath- The prepended classpath.- Throws:
IllegalArgumentException- if the given classpath is invalid (e.g. does not exist)- See Also:
setClassLoader(ClassLoader)
-
getLanguageVersionDiscoverer
public LanguageVersionDiscoverer getLanguageVersionDiscoverer()
Get the LanguageVersionDiscoverer, used to determine the LanguageVersion of a source file.- Returns:
- The LanguageVersionDiscoverer.
-
getForceLanguageVersion
public LanguageVersion getForceLanguageVersion()
Get the LanguageVersion specified by the force-language parameter. This overrides detection based on file extensions- Returns:
- The LanguageVersion.
-
isForceLanguageVersion
public boolean isForceLanguageVersion()
Is the force-language parameter set to anything?- Returns:
- true if $
getForceLanguageVersion()is not null
-
setForceLanguageVersion
public void setForceLanguageVersion(LanguageVersion forceLanguageVersion)
Set the LanguageVersion specified by the force-language parameter. This overrides detection based on file extensions- Parameters:
forceLanguageVersion- the language version
-
setDefaultLanguageVersion
public void setDefaultLanguageVersion(LanguageVersion languageVersion)
Set the given LanguageVersion as the current default for it's Language.- Parameters:
languageVersion- the LanguageVersion
-
setDefaultLanguageVersions
public void setDefaultLanguageVersions(List<LanguageVersion> languageVersions)
Set the given LanguageVersions as the current default for their Languages.- Parameters:
languageVersions- The LanguageVersions.
-
getLanguageVersionOfFile
public LanguageVersion getLanguageVersionOfFile(String fileName)
Get the LanguageVersion of the source file with given name. This depends on the fileName extension, and the java version.For compatibility with older code that does not always pass in a correct filename, unrecognized files are assumed to be java files.
- Parameters:
fileName- Name of the file, can be absolute, or simple.- Returns:
- the LanguageVersion
-
getRuleSets
@Deprecated public String getRuleSets()
Deprecated.Get the comma separated list of RuleSet URIs.- Returns:
- The RuleSet URIs.
-
getRuleSetPaths
public List<String> getRuleSetPaths()
Returns the list of ruleset URIs.- See Also:
RuleSetLoader.loadFromResource(String)
-
setRuleSets
public void setRuleSets(List<String> ruleSetPaths)
Sets the list of ruleset paths to load when starting the analysis.- Parameters:
ruleSetPaths- A list of ruleset paths, understandable byRuleSetLoader.loadFromResource(String).- Throws:
NullPointerException- If the parameter is null
-
addRuleSet
public void addRuleSet(String rulesetPath)
Add a new ruleset paths to load when starting the analysis. This list is initially empty.- Parameters:
rulesetPath- A ruleset path, understandable byRuleSetLoader.loadFromResource(String).- Throws:
NullPointerException- If the parameter is null
-
setRuleSets
@Deprecated public void setRuleSets(String ruleSets)
Deprecated.UsesetRuleSets(List)oraddRuleSet(String).Set the comma separated list of RuleSet URIs.- Parameters:
ruleSets- the rulesets to set
-
getMinimumPriority
public RulePriority getMinimumPriority()
Get the minimum priority threshold when loading Rules from RuleSets.- Returns:
- The minimum priority threshold.
-
setMinimumPriority
public void setMinimumPriority(RulePriority minimumPriority)
Set the minimum priority threshold when loading Rules from RuleSets.- Parameters:
minimumPriority- The minimum priority.
-
getInputPaths
@Deprecated public String getInputPaths()
Deprecated.Get the comma separated list of input paths to process for source files.- Returns:
- A comma separated list.
-
getAllInputPaths
@Deprecated public List<String> getAllInputPaths()
Deprecated.Returns an unmodifiable list.- Throws:
NullPointerException- If the parameter is null
-
getInputPathList
public List<Path> getInputPathList()
Returns an unmodifiable list.- Throws:
NullPointerException- If the parameter is null
-
setInputPaths
@Deprecated public void setInputPaths(String inputPaths)
Deprecated.Set the comma separated list of input paths to process for source files.- Parameters:
inputPaths- The comma separated list.- Throws:
NullPointerException- If the parameter is null
-
setInputPaths
@Deprecated public void setInputPaths(List<String> inputPaths)
Deprecated.Set the input paths to the given list of paths.- Throws:
NullPointerException- If the parameter is null
-
setInputPathList
public void setInputPathList(List<Path> inputPaths)
Set the input paths to the given list of paths.- Throws:
NullPointerException- If the parameter is null
-
addInputPath
@Deprecated public void addInputPath(String inputPath)
Deprecated.Add an input path. It is not split on commas.- Throws:
NullPointerException- If the parameter is null
-
addInputPath
public void addInputPath(Path inputPath)
Add an input path. It is not split on commas.- Throws:
NullPointerException- If the parameter is null
-
getInputFilePath
@Deprecated public String getInputFilePath()
Deprecated.UsegetInputFile()
-
getInputFile
public Path getInputFile()
-
getIgnoreFilePath
@Deprecated public String getIgnoreFilePath()
Deprecated.UsegetIgnoreFile()
-
getIgnoreFile
public Path getIgnoreFile()
-
setInputFilePath
@Deprecated public void setInputFilePath(String inputFilePath)
Deprecated.The input file path points to a single file, which contains a comma-separated list of source file names to process.- Parameters:
inputFilePath- path to the file
-
setInputFilePath
public void setInputFilePath(Path inputFilePath)
The input file path points to a single file, which contains a comma-separated list of source file names to process.- Parameters:
inputFilePath- path to the file
-
setIgnoreFilePath
@Deprecated public void setIgnoreFilePath(String ignoreFilePath)
Deprecated.The input file path points to a single file, which contains a comma-separated list of source file names to ignore.- Parameters:
ignoreFilePath- path to the file
-
setIgnoreFilePath
public void setIgnoreFilePath(Path ignoreFilePath)
The input file path points to a single file, which contains a comma-separated list of source file names to ignore.- Parameters:
ignoreFilePath- path to the file
-
getInputUri
@Deprecated public String getInputUri()
Deprecated.UsegetUri()Get the input URI to process for source code objects.- Returns:
- URI
-
getUri
public URI getUri()
Get the input URI to process for source code objects.- Returns:
- URI
-
setInputUri
@Deprecated public void setInputUri(String inputUri)
Deprecated.UsesetInputUri(URI)Set the input URI to process for source code objects.- Parameters:
inputUri- a single URI
-
setInputUri
public void setInputUri(URI inputUri)
Set the input URI to process for source code objects.- Parameters:
inputUri- a single URI
-
isReportShortNames
public boolean isReportShortNames()
Get whether to use File short names in Reports.- Returns:
truewhen using short names in reports.
-
setReportShortNames
public void setReportShortNames(boolean reportShortNames)
Set whether to use File short names in Reports.- Parameters:
reportShortNames-truewhen using short names in reports.
-
createRenderer
public Renderer createRenderer()
Create a Renderer instance based upon the configured reporting options. No writer is created.- Returns:
- renderer
-
createRenderer
public Renderer createRenderer(boolean withReportWriter)
Create a Renderer instance based upon the configured reporting options. If withReportWriter then we'll configure it with a writer for the reportFile specified.- Parameters:
withReportWriter- whether to configure a writer or not- Returns:
- A Renderer instance.
-
getReportFormat
public String getReportFormat()
Get the report format.- Returns:
- The report format.
-
setReportFormat
public void setReportFormat(String reportFormat)
Set the report format. This should be a name of a Renderer.- Parameters:
reportFormat- The report format.- See Also:
Renderer
-
getReportFile
@Deprecated public String getReportFile()
Deprecated.Get the file to which the report should render.- Returns:
- The file to which to render.
-
getReportFilePath
public Path getReportFilePath()
Get the file to which the report should render.- Returns:
- The file to which to render.
-
setReportFile
@Deprecated public void setReportFile(String reportFile)
Deprecated.Set the file to which the report should render.- Parameters:
reportFile- the file to set
-
setReportFile
public void setReportFile(Path reportFile)
Set the file to which the report should render.- Parameters:
reportFile- the file to set
-
isShowSuppressedViolations
public boolean isShowSuppressedViolations()
Get whether the report should show suppressed violations.- Returns:
trueif showing suppressed violations,falseotherwise.
-
setShowSuppressedViolations
public void setShowSuppressedViolations(boolean showSuppressedViolations)
Set whether the report should show suppressed violations.- Parameters:
showSuppressedViolations-trueif showing suppressed violations,falseotherwise.
-
getReportProperties
public Properties getReportProperties()
Get the Report properties. These are used to create the Renderer.- Returns:
- The report properties.
-
setReportProperties
public void setReportProperties(Properties reportProperties)
Set the Report properties. These are used to create the Renderer.- Parameters:
reportProperties- The Report properties to set.
-
isStressTest
@Deprecated public boolean isStressTest()
Deprecated.For removalReturn the stress test indicator. If this value istruethen PMD will randomize the order of file processing to attempt to shake out bugs.- Returns:
trueif stress test is enbaled,falseotherwise.
-
setStressTest
@Deprecated public void setStressTest(boolean stressTest)
Deprecated.For removal.Set the stress test indicator.- Parameters:
stressTest- The stree test indicator to set.- See Also:
isStressTest()
-
isBenchmark
@Deprecated public boolean isBenchmark()
Deprecated.This behavior is down to CLI, not part of the core analysis.Return the benchmark indicator. If this value istruethen PMD will log benchmark information.- Returns:
trueif benchmark logging is enbaled,falseotherwise.
-
setBenchmark
@Deprecated public void setBenchmark(boolean benchmark)
Deprecated.This behavior is down to CLI, not part of the core analysis.Set the benchmark indicator.- Parameters:
benchmark- The benchmark indicator to set.- See Also:
isBenchmark()
-
isFailOnViolation
public boolean isFailOnViolation()
Whether PMD should exit with status 4 (the default behavior, true) if violations are found or just with 0 (to not break the build, e.g.).- Returns:
- failOnViolation
-
setFailOnViolation
public void setFailOnViolation(boolean failOnViolation)
Sets whether PMD should exit with status 4 (the default behavior, true) if violations are found or just with 0 (to not break the build, e.g.).- Parameters:
failOnViolation- failOnViolation
-
isRuleSetFactoryCompatibilityEnabled
public boolean isRuleSetFactoryCompatibilityEnabled()
Checks if the rule set factory compatibility feature is enabled.- Returns:
- true, if the rule set factory compatibility feature is enabled
- See Also:
RuleSetFactoryCompatibility
-
setRuleSetFactoryCompatibilityEnabled
public void setRuleSetFactoryCompatibilityEnabled(boolean ruleSetFactoryCompatibilityEnabled)
Sets the rule set factory compatibility feature enabled/disabled.- Parameters:
ruleSetFactoryCompatibilityEnabled-trueif the feature should be enabled- See Also:
RuleSetFactoryCompatibility
-
getAnalysisCache
public AnalysisCache getAnalysisCache()
Retrieves the currently used analysis cache. Will never be null.- Returns:
- The currently used analysis cache. Never null.
-
setAnalysisCache
public void setAnalysisCache(AnalysisCache cache)
Sets the analysis cache to be used. Setting a value ofnullwill cause a Noop AnalysisCache to be used. If incremental analysis was explicitly disabled (isIgnoreIncrementalAnalysis()), then this method is a noop.- Parameters:
cache- The analysis cache to be used.
-
setAnalysisCacheLocation
public void setAnalysisCacheLocation(String cacheLocation)
Sets the location of the analysis cache to be used. This will automatically configure and appropriate AnalysisCache implementation.- Parameters:
cacheLocation- The location of the analysis cache to be used.
-
setIgnoreIncrementalAnalysis
public void setIgnoreIncrementalAnalysis(boolean noCache)
Sets whether the user has explicitly disabled incremental analysis or not. If so, incremental analysis is not used, and all suggestions to use it are disabled. The analysis cached location is ignored, even if it's specified.- Parameters:
noCache- Whether to ignore incremental analysis or not
-
isIgnoreIncrementalAnalysis
public boolean isIgnoreIncrementalAnalysis()
Returns whether incremental analysis was explicitly disabled by the user or not.- Returns:
trueif incremental analysis is explicitly disabled
-
-