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. Once configured, usePmdAnalysis.create(PMDConfiguration)in a try-with-resources to execute the analysis (seePmdAnalysis).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. - Use
setRuleSetFactoryCompatibilityEnabled(boolean)to disable the compatibility measures for removed and renamed rules in the rulesets that will be 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.
getReportFile() - 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()
- You can configure paths to the rulesets to use with
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_SUPPRESS_MARKERThe default suppress marker string.
-
Constructor Summary
Constructors Constructor Description PMDConfiguration()PMDConfiguration(@NonNull LanguageRegistry languageRegistry)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddRuleSet(@NonNull 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.AnalysisCachegetAnalysisCache()Retrieves the currently used analysis cache.ClassLoadergetClassLoader()Get the ClassLoader being used by PMD when processing Rules.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.@NonNull List<@NonNull String>getRuleSetPaths()Returns the list of ruleset URIs.@Nullable StringgetRuleSets()Deprecated.StringgetSuppressMarker()Get the suppress marker.intgetThreads()Get the number of threads to use when processing Rules.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.).booleanisIgnoreIncrementalAnalysis()Returns whether incremental analysis was explicitly disabled by the user or not.booleanisRuleSetFactoryCompatibilityEnabled()Checks if the rule set factory compatibility feature is enabled.booleanisShowSuppressedViolations()Get whether the report should show suppressed violations.voidprependAuxClasspath(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.voidsetClassLoader(ClassLoader classLoader)Set the ClassLoader being used by PMD when processing Rules.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.).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.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.voidsetRuleSetFactoryCompatibilityEnabled(boolean ruleSetFactoryCompatibilityEnabled)Sets the rule set factory compatibility feature enabled/disabled.voidsetRuleSets(@NonNull List<@NonNull String> ruleSetPaths)Sets the list of ruleset paths to load when starting the analysis.voidsetRuleSets(@Nullable String ruleSets)Deprecated.UsesetRuleSets(List)oraddRuleSet(String).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, getSourceEncoding, getUri, isForceLanguageVersion, setDefaultLanguageVersion, setDefaultLanguageVersions, setExcludes, setForceLanguageVersion, setIgnoreFilePath, setIgnoreFilePath, setInputFilePath, setInputFilePath, setInputPathList, setInputPaths, setInputUri, setInputUri, setOnlyRecognizeLanguage, setReporter, setSourceEncoding
-
-
-
-
Field Detail
-
DEFAULT_SUPPRESS_MARKER
public static final String DEFAULT_SUPPRESS_MARKER
The default suppress marker string.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
PMDConfiguration
public PMDConfiguration()
-
PMDConfiguration
public PMDConfiguration(@NonNull LanguageRegistry languageRegistry)
-
-
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.You can specify multiple class paths separated by `:` on Unix-systems or `;` under Windows. See
File.pathSeparator.- Parameters:
classpath- The prepended classpath.- Throws:
IllegalArgumentException- if the given classpath is invalid (e.g. does not exist)- See Also:
setClassLoader(ClassLoader)
-
getRuleSets
@Deprecated public @Nullable String getRuleSets()
Deprecated.Get the comma separated list of RuleSet URIs.- Returns:
- The RuleSet URIs.
-
getRuleSetPaths
public @NonNull List<@NonNull String> getRuleSetPaths()
Returns the list of ruleset URIs.- See Also:
RuleSetLoader.loadFromResource(String)
-
setRuleSets
public void setRuleSets(@NonNull List<@NonNull 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(@NonNull 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(@Nullable 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.
-
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
-
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.
-
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:
RuleSetLoader.enableCompatibility(boolean)
-
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:
RuleSetLoader.enableCompatibility(boolean)
-
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
-
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
-
-