Class RuleContext
- java.lang.Object
-
- net.sourceforge.pmd.RuleContext
-
public class RuleContext extends Object
The API for rules to report violations or errors during analysis. The non-deprecated API of this class represents what will be left in PMD 7. In PMD 6, the 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()
Deprecated.Internal API, removed in PMD 7RuleContext(RuleContext ruleContext)
Deprecated.Internal API, removed in PMD 7
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addViolation(Node location)
Record a new violation of the contextual rule, at the given node.void
addViolation(Node location, Object... formatArgs)
Record a new violation of the contextual rule, at the given node.void
addViolationWithMessage(Node location, String message)
Record a new violation of the contextual rule, at the given node.void
addViolationWithMessage(Node location, String message, Object... formatArgs)
Record a new violation of the contextual rule, at the given node.void
addViolationWithPosition(Node location, int beginLine, int endLine, String message, Object... formatArgs)
Record a new violation of the contextual rule, at the given node.Object
getAttribute(String name)
Deprecated.Stateful methods of the rule context will be removed.Rule
getCurrentRule()
Deprecated.Internal API.LanguageVersion
getLanguageVersion()
Deprecated.Will be removed in PMD 7, as the nodes have access to their language version.Report
getReport()
Deprecated.Internal API, removed in PMD 7File
getSourceCodeFile()
Deprecated.Internal API, removed in PMD 7String
getSourceCodeFilename()
Deprecated.Internal API, removed in PMD 7boolean
isIgnoreExceptions()
Deprecated.Internal API, removed in PMD 7Object
removeAttribute(String name)
Deprecated.Stateful methods of the rule context will be removed.boolean
setAttribute(String name, Object value)
Deprecated.Stateful methods of the rule context will be removed.void
setCurrentRule(Rule currentRule)
Deprecated.Internal API.void
setIgnoreExceptions(boolean ignoreExceptions)
Deprecated.Internal API, removed in PMD 7void
setLanguageVersion(LanguageVersion languageVersion)
Deprecated.Internal API, removed in PMD 7void
setReport(Report report)
Deprecated.Internal API, removed in PMD 7void
setSourceCodeFile(File sourceCodeFile)
Deprecated.Internal API, removed in PMD 7void
setSourceCodeFilename(String filename)
Deprecated.Internal API, removed in PMD 7
-
-
-
Constructor Detail
-
RuleContext
@Deprecated @InternalApi public RuleContext()
Deprecated.Internal API, removed in PMD 7Default constructor.
-
RuleContext
@Deprecated @InternalApi public RuleContext(RuleContext ruleContext)
Deprecated.Internal API, removed in PMD 7Constructor which shares attributes and report listeners with the given RuleContext.- Parameters:
ruleContext
- the context from which the values are shared
-
-
Method Detail
-
getCurrentRule
@InternalApi @Deprecated public Rule getCurrentRule()
Deprecated.Internal API.
-
setCurrentRule
@InternalApi @Deprecated public void setCurrentRule(Rule currentRule)
Deprecated.Internal API.
-
addViolation
public void addViolation(Node location)
Record a new violation of the contextual rule, at the given node.- Parameters:
location
- Location of the violation
-
addViolation
public void addViolation(Node location, Object... formatArgs)
Record a new violation of the contextual rule, at the given node. The default violation message (Rule.getMessage()
) is formatted using the given format arguments.- Parameters:
location
- Location of the violationformatArgs
- Format arguments for the message- See Also:
MessageFormat
-
addViolationWithMessage
public void addViolationWithMessage(Node location, String message)
Record a new violation of the contextual rule, at the given node. The given violation message (Rule.getMessage()
) is treated as a format string for aMessageFormat
and should hence use appropriate escapes. No formatting arguments are provided.- Parameters:
location
- Location of the violationmessage
- Violation message
-
addViolationWithMessage
public void addViolationWithMessage(Node location, String message, Object... formatArgs)
Record a new violation of the contextual rule, at the given node. The given violation message (Rule.getMessage()
) is treated as a format string for aMessageFormat
and should hence use appropriate escapes. The given formatting arguments are used.- Parameters:
location
- Location of the violationmessage
- Violation messageformatArgs
- Format arguments for the message
-
addViolationWithPosition
public void addViolationWithPosition(Node location, int beginLine, int endLine, String message, Object... formatArgs)
Record a new violation of the contextual rule, at the given node. The position is refined using the given begin and end line numbers. The given violation message (Rule.getMessage()
) is treated as a format string for aMessageFormat
and should hence use appropriate escapes. The given formatting arguments are used.- Parameters:
location
- Location of the violationmessage
- Violation messageformatArgs
- Format arguments for the message
-
getReport
@Deprecated @InternalApi public Report getReport()
Deprecated.Internal API, removed in PMD 7Get the Report to which Rule Violations are sent.- Returns:
- The Report.
-
setReport
@Deprecated @InternalApi public void setReport(Report report)
Deprecated.Internal API, removed in PMD 7Set the Report to which Rule Violations are sent.- Parameters:
report
- The Report.
-
getSourceCodeFile
@Deprecated @InternalApi public File getSourceCodeFile()
Deprecated.Internal API, removed in PMD 7Get the File associated with the current source file.- Returns:
- The File.
-
setSourceCodeFile
@Deprecated @InternalApi public void setSourceCodeFile(File sourceCodeFile)
Deprecated.Internal API, removed in PMD 7Set 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
@Deprecated @InternalApi public String getSourceCodeFilename()
Deprecated.Internal API, removed in PMD 7Get 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 @InternalApi public void setSourceCodeFilename(String filename)
Deprecated.Internal API, removed in PMD 7Set the file name associated with the current source file.- Parameters:
filename
- The file name.
-
getLanguageVersion
@Deprecated public LanguageVersion getLanguageVersion()
Deprecated.Will be removed in PMD 7, as the nodes have access to their language version. In PMD 6, this is still the only way to access the language version within a rule, and cannot be replaced. The deprecation warning hints that the method should be replaced in PMD 7.Get the LanguageVersion associated with the current source file.- Returns:
- The LanguageVersion,
null
if unknown.
-
setLanguageVersion
@Deprecated @InternalApi public void setLanguageVersion(LanguageVersion languageVersion)
Deprecated.Internal API, removed in PMD 7Set the LanguageVersion associated with the current source file. This may be set tonull
to indicate the version is unknown and should be automatically determined.- Parameters:
languageVersion
- The LanguageVersion.
-
setAttribute
@Deprecated public boolean setAttribute(String name, Object value)
Deprecated.Stateful methods of the rule context will be removed. Their interaction with incremental analysis are unspecified.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:
true
if the attribute was set,false
otherwise.- Throws:
IllegalArgumentException
- ifname
orvalue
arenull
-
getAttribute
@Deprecated public Object getAttribute(String name)
Deprecated.Stateful methods of the rule context will be removed. Their interaction with incremental analysis are unspecified.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
null
if the attribute does not exist.
-
removeAttribute
@Deprecated public Object removeAttribute(String name)
Deprecated.Stateful methods of the rule context will be removed. Their interaction with incremental analysis are unspecified.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
null
if the attribute does not exist.
-
setIgnoreExceptions
@Deprecated @InternalApi public void setIgnoreExceptions(boolean ignoreExceptions)
Deprecated.Internal API, removed in PMD 7Configure whether exceptions during applying a rule should be ignored or not. If set totrue
then 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 tofalse
then the processing will be aborted with the exception. This is especially useful during unit tests, in order to not oversee any exceptions.- Parameters:
ignoreExceptions
- iftrue
simply skip failing rules (default).
-
isIgnoreExceptions
@Deprecated @InternalApi public boolean isIgnoreExceptions()
Deprecated.Internal API, removed in PMD 7Gets 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:
true
when failing rules are skipped,false
otherwise.
-
-