Class PMDConfiguration
PmdAnalysis.create(PMDConfiguration)
in a try-with-resources to execute the analysis (see PmdAnalysis).
Rulesets
- You can configure paths to the rulesets to use with
addRuleSet(String). These can be file paths or classpath resources. - Use
setMinimumPriority(RulePriority)to control the minimum priority a rule must have to be included. Defaults to the lowest priority, ie all rules are loaded.
Source files
- The default encoding of source files is the system default as
returned by
System.getProperty("file.encoding"). You can set it withAbstractConfiguration.setSourceEncoding(Charset). - The source files to analyze can be given in many ways. See
AbstractConfiguration.addInputPath(Path)AbstractConfiguration.setInputFilePath(Path),AbstractConfiguration.setInputUri(URI). - Files are assigned a language based on their name. The language
version of languages can be given with
AbstractConfiguration.setDefaultLanguageVersion(LanguageVersion). The default language assignment can be overridden withAbstractConfiguration.setForceLanguageVersion(LanguageVersion).
Rendering
- The renderer format to use for Reports.
getReportFormat() - The file to which the Report should render.
AbstractConfiguration.getReportFilePath() - Configure the root paths that are used to relativize file names in reports via
AbstractConfiguration.addRelativizeRoot(Path). This enables to get short names in reports. - The initialization properties to use when creating a Renderer instance.
getReportProperties() - An indicator of whether to show suppressed Rule violations in Reports.
isShowSuppressedViolations()
Language configuration
- Use
setSuppressMarker(String)to change the comment marker for suppression comments. Defaults to "NOPMD". - See
setClassLoader(ClassLoader)andprependAuxClasspath(String)for information for how to configure classpath for Java analysis. - You can set additional language properties with
AbstractConfiguration.getLanguageProperties(Language)
Miscellaneous
- Use
setThreads(int)to control the parallelism of the analysis. Defaults one thread per available processor.getThreads()
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddRuleSet(@NonNull String rulesetPath) Add a new ruleset paths to load when starting the analysis.protected voidCheck that it is correct to use the given language with this configuration.Create a Renderer instance based upon the configured reporting options.createRenderer(boolean withReportWriter) Create a Renderer instance based upon the configured reporting options.Gets the currently set auxClasspath.Deprecated.Since 7.27.0.Get the minimum priority threshold when loading Rules from RuleSets.Get the report format.Get the Report properties.Returns the list of ruleset URIs.Get the suppress marker.intGet the number of threads to use when processing Rules.booleanReturns whether incremental analysis was explicitly disabled by the user or not.booleanGet whether the report should show suppressed violations.voidprependAuxClasspath(String classpath) Prepend the specified classpath like string to the currently set auxClasspath of the configuration.voidsetAnalysisCacheLocation(String cacheLocation) Sets the location of the analysis cache to be used.voidsetAuxClasspath(String classpath) Uses the specified classpath like string as the auxClasspath of the configuration.voidsetClassLoader(ClassLoader classLoader) Deprecated.Since 7.27.0.voidsetIgnoreIncrementalAnalysis(boolean noCache) Sets whether the user has explicitly disabled incremental analysis or not.voidsetMinimumPriority(RulePriority minimumPriority) Set the minimum priority threshold when loading Rules from RuleSets.voidsetReportFormat(String reportFormat) Set the report format.voidsetReportProperties(Properties reportProperties) Set the Report properties.voidsetRuleSets(@NonNull List<@NonNull 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.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
addInputPath, addRelativizeRoot, addRelativizeRoots, collectFilesRecursively, collectFilesRecursively, getExcludes, getForceLanguageVersion, getIgnoreFile, getInputFile, getInputPathList, getLanguageProperties, getLanguageRegistry, getLanguageVersionDiscoverer, getLanguageVersionOfFile, getRelativizeRoots, getReporter, getReportFilePath, getSourceEncoding, getUri, isFailOnError, isFailOnViolation, isForceLanguageVersion, setDefaultLanguageVersion, setDefaultLanguageVersions, setExcludes, setFailOnError, setFailOnViolation, setForceLanguageVersion, setIgnoreFilePath, setInputFilePath, setInputPathList, setInputUri, setOnlyRecognizeLanguage, setReporter, setReportFile, setSourceEncoding
-
Field Details
-
DEFAULT_SUPPRESS_MARKER
The default suppress marker string.- See Also:
-
-
Constructor Details
-
PMDConfiguration
public PMDConfiguration() -
PMDConfiguration
-
-
Method Details
-
getSuppressMarker
Get the suppress marker. This is the source level marker used to indicate a RuleViolation should be suppressed.- Returns:
- The suppress marker.
-
setSuppressMarker
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
Deprecated.Since 7.27.0. UsegetAuxClasspath()instead.Get the ClassLoader being used by PMD when analyzing code, e.g. Java.Since 7.27.0, this method will only return the classloader, that has been set explicitly via
setClassLoader(ClassLoader). Instead of setting a classloader, the auxClasspath should be configured viasetAuxClasspath(String)orprependAuxClasspath(String).See also the notes on
setClassLoader(ClassLoader)regarding closing and supported types.- Returns:
- The ClassLoader being used
-
setClassLoader
Deprecated.Since 7.27.0. UseprependAuxClasspath(String)orsetAuxClasspath(String)instead.Set the ClassLoader being used by PMD when analyzing code, e.g. Java. Setting a value ofnullwill cause the default ClassLoader to be used.Since 7.27.0, a classloader should not be set anymore. Instead, the auxClasspath should be configured via
setAuxClasspath(String)orprependAuxClasspath(String). For backwards compatibility, if setting a classloader here, it will still be used.Note 1: The classloader might keep open file references to jar files if it is not closed. A
URLClassLoaderis closeable and any given classloader that isCloseablewill be closed, when PMD is called viaPmdAnalysis. In other cases, e.g. not using PmdAnalysis, you need to close the classloader yourself.Note 2: Only subtypes of
URLClassLoaderare compatible with PMD, as for the incremental analysis we need to figure out the actual URLs of the classpath to check the validity of our cache.- Parameters:
classLoader- The ClassLoader to use
-
prependAuxClasspath
Prepend the specified classpath like string to the currently set auxClasspath of the configuration.Use
setAuxClasspath(String)if you don't want any fallbacks (neither to PMD's runtime classpath nor to the current JVM runtime) and only access classes on the given auxClasspath.Specify the JVM's platform classpath yourself explicitly (e.g. adding
jrt-fs.jar) in order to not fall back to the current JVM runtime.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.You can specify multiple class paths separated by `:` on Unix-systems or `;` under Windows. See
File.pathSeparator.- Parameters:
classpath- The additional classpath entries to be prepended.- Throws:
IllegalArgumentException- if the given classpath is invalid (e.g. does not exist)- See Also:
-
setAuxClasspath
Uses the specified classpath like string as the auxClasspath of the configuration.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.You can specify multiple class paths separated by `:` on Unix-systems or `;` under Windows. See
File.pathSeparator.- Parameters:
classpath- The classpath entries to be used.- Throws:
IllegalArgumentException- if the given classpath is invalid (e.g. does not exist)- Since:
- 7.27.0
- See Also:
-
getAuxClasspath
Gets the currently set auxClasspath.- Returns:
- the configured auxClasspath. Might be
null. - Since:
- 7.27.0
- See Also:
-
getRuleSetPaths
Returns the list of ruleset URIs.- See Also:
-
setRuleSets
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
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
-
getMinimumPriority
Get the minimum priority threshold when loading Rules from RuleSets.- Returns:
- The minimum priority threshold.
-
setMinimumPriority
Set the minimum priority threshold when loading Rules from RuleSets.- Parameters:
minimumPriority- The minimum priority.
-
createRenderer
Create a Renderer instance based upon the configured reporting options. No writer is created.- Returns:
- renderer
-
createRenderer
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
Get the report format.- Returns:
- The report format.
-
setReportFormat
Set the report format. This should be a name of a Renderer.- Parameters:
reportFormat- The report format.- See Also:
-
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
Get the Report properties. These are used to create the Renderer.- Returns:
- The report properties.
-
setReportProperties
Set the Report properties. These are used to create the Renderer.- Parameters:
reportProperties- The Report properties to set.
-
setAnalysisCacheLocation
Sets the location of the analysis cache to be used. This will automatically configure and appropriate AnalysisCache implementation. 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:
cacheLocation- The location of the analysis cache to be used. Usenullto disable the cache.
-
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
-
checkLanguageIsAcceptable
Description copied from class:AbstractConfigurationCheck that it is correct to use the given language with this configuration.- Overrides:
checkLanguageIsAcceptablein classAbstractConfiguration- Throws:
UnsupportedOperationException- if the language isn't supported.
-