Class RuleContext
- java.lang.Object
-
- net.sourceforge.pmd.RuleContext
-
public class RuleContext extends java.lang.ObjectThe RuleContext provides access to Rule processing state. This information includes the following global information:- The Report to which Rule Violations are sent.
- Named attributes.
- A File for the source file.
- The Language Version of the source file.
-
-
Constructor Summary
Constructors Constructor Description RuleContext()Default constructor.RuleContext(RuleContext ruleContext)Constructor which shares attributes and report listeners with the given RuleContext.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.lang.ObjectgetAttribute(java.lang.String name)Get an attribute value on the RuleContext.LanguageVersiongetLanguageVersion()Get the LanguageVersion associated with the current source file.ReportgetReport()Get the Report to which Rule Violations are sent.java.io.FilegetSourceCodeFile()Get the File associated with the current source file.java.lang.StringgetSourceCodeFilename()Get the file name associated with the current source file.booleanisIgnoreExceptions()Gets the configuration whether to skip failing rules (true) or whether to throw a a RuntimeException and abort the processing for the first failing rule.java.lang.ObjectremoveAttribute(java.lang.String name)Remove an attribute value on the RuleContext.booleansetAttribute(java.lang.String name, java.lang.Object value)Set an attribute value on the RuleContext, if it does not already exist.voidsetIgnoreExceptions(boolean ignoreExceptions)Configure whether exceptions during applying a rule should be ignored or not.voidsetLanguageVersion(LanguageVersion languageVersion)Set the LanguageVersion associated with the current source file.voidsetReport(Report report)Set the Report to which Rule Violations are sent.voidsetSourceCodeFile(java.io.File sourceCodeFile)Set the File associated with the current source file.voidsetSourceCodeFilename(java.lang.String filename)Deprecated.This method will be removed.
-
-
-
Constructor Detail
-
RuleContext
public RuleContext()
Default constructor.
-
RuleContext
public RuleContext(RuleContext ruleContext)
Constructor which shares attributes and report listeners with the given RuleContext.- Parameters:
ruleContext- the context from which the values are shared
-
-
Method Detail
-
getReport
public Report getReport()
Get the Report to which Rule Violations are sent.- Returns:
- The Report.
-
setReport
public void setReport(Report report)
Set the Report to which Rule Violations are sent.- Parameters:
report- The Report.
-
getSourceCodeFile
public java.io.File getSourceCodeFile()
Get the File associated with the current source file.- Returns:
- The File.
-
setSourceCodeFile
public void setSourceCodeFile(java.io.File sourceCodeFile)
Set the File associated with the current source file. While this may be set tonull, the exclude/include facilities will not work properly without a File.- Parameters:
sourceCodeFile- The File.
-
getSourceCodeFilename
public java.lang.String getSourceCodeFilename()
Get the file name associated with the current source file. If there is no source file, then an empty string is returned.- Returns:
- The file name.
-
setSourceCodeFilename
@Deprecated public void setSourceCodeFilename(java.lang.String filename)
Deprecated.This method will be removed. The file should only be set withsetSourceCodeFile(File). Setting the filename here has no effect.Set the file name associated with the current source file.- Parameters:
filename- The file name.
-
getLanguageVersion
public LanguageVersion getLanguageVersion()
Get the LanguageVersion associated with the current source file.- Returns:
- The LanguageVersion,
nullif unknown.
-
setLanguageVersion
public void setLanguageVersion(LanguageVersion languageVersion)
Set the LanguageVersion associated with the current source file. This may be set tonullto indicate the version is unknown and should be automatically determined.- Parameters:
languageVersion- The LanguageVersion.
-
setAttribute
public boolean setAttribute(java.lang.String name, java.lang.Object value)Set an attribute value on the RuleContext, if it does not already exist.Attributes can be shared between RuleContext instances. This operation is thread-safe.
Attribute values should be modified directly via the reference provided. It is not necessary to call
setAttribute(String, Object)to update an attribute value. Modifications made to the attribute value will automatically be seen by other threads. Because of this, you must ensure the attribute values are themselves thread safe.- Parameters:
name- The attribute name.value- The attribute value.- Returns:
trueif the attribute was set,falseotherwise.- Throws:
java.lang.IllegalArgumentException- ifnameorvaluearenull
-
getAttribute
public java.lang.Object getAttribute(java.lang.String name)
Get an attribute value on the RuleContext.Attributes can be shared between RuleContext instances. This operation is thread-safe.
Attribute values should be modified directly via the reference provided. It is not necessary to call
setAttribute(String, Object)to update an attribute value. Modifications made to the attribute value will automatically be seen by other threads. Because of this, you must ensure the attribute values are themselves thread safe.- Parameters:
name- The attribute name.- Returns:
- The current attribute value, or
nullif the attribute does not exist.
-
removeAttribute
public java.lang.Object removeAttribute(java.lang.String name)
Remove an attribute value on the RuleContext.Attributes can be shared between RuleContext instances. This operation is thread-safe.
Attribute values should be modified directly via the reference provided. It is not necessary to call
setAttribute(String, Object)to update an attribute value. Modifications made to the attribute value will automatically be seen by other threads. Because of this, you must ensure the attribute values are themselves thread safe.- Parameters:
name- The attribute name.- Returns:
- The current attribute value, or
nullif the attribute does not exist.
-
setIgnoreExceptions
public void setIgnoreExceptions(boolean ignoreExceptions)
Configure whether exceptions during applying a rule should be ignored or not. If set totruethen such exceptions are logged as warnings and the processing is continued with the next rule - the failing rule is simply skipped. This is the default behavior.
If set tofalsethen the processing will be aborted with the exception. This is especially useful during unit tests, in order to not oversee any exceptions.- Parameters:
ignoreExceptions- iftruesimply skip failing rules (default).
-
isIgnoreExceptions
public boolean isIgnoreExceptions()
Gets the configuration whether to skip failing rules (true) or whether to throw a a RuntimeException and abort the processing for the first failing rule.- Returns:
truewhen failing rules are skipped,falseotherwise.
-
-