net.sourceforge.pmd

## Class RuleContext

- java.lang.Object
- - net.sourceforge.pmd.RuleContext

    public class RuleContext
    extends Object

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.

As well as the following source file specific information:

- A File for the source file.
- A String for the name of the source file.
- The Language Version of the source file.

It is required that all source file specific options be set between calls to difference source files. Failure to do so, may result in undefined behavior.

### Constructor Summary

Constructors
Constructor and Description
`RuleContext()` Default constructor.
`RuleContext(RuleContext ruleContext)` Constructor which shares attributes and report listeners with the given RuleContext.

### Method Summary

Methods
`Object` `getAttribute(String name)` Get an attribute value on the RuleContext.
`LanguageVersion` `getLanguageVersion()` Get the LanguageVersion associated with the current source file.
`Report` `getReport()` Get the Report to which Rule Violations are sent.
`File` `getSourceCodeFile()` Get the File associated with the current source file.
`String` `getSourceCodeFilename()` Get the file name associated with the current source file.
`Object` `removeAttribute(String name)` Remove an attribute value on the RuleContext.
`boolean` `setAttribute(String name, Object value)` Set an attribute value on the RuleContext, if it does not already exist.
`void` `setLanguageVersion(LanguageVersion languageVersion)` Set the LanguageVersion associated with the current source file.
`void` `setReport(Report report)` Set the Report to which Rule Violations are sent.
`void` `setSourceCodeFile(File sourceCodeFile)` Set the File associated with the current source file.
`void` `setSourceCodeFilename(String filename)` Set the file name associated with the current source file.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### RuleContext

    public RuleContext()

Default constructor.

#### RuleContext

    public RuleContext(RuleContext ruleContext)

Constructor which shares attributes and report listeners with the given RuleContext.

### 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 File getSourceCodeFile()

Get the File associated with the current source file.

Returns:
  The File.

#### setSourceCodeFile

    public void setSourceCodeFile(File sourceCodeFile)

Set the File associated with the current source file. While this may be set to `null`, the exclude/include facilities will not work properly without a File.

Parameters:
  `sourceCodeFile` - The File.

#### getSourceCodeFilename

    public String getSourceCodeFilename()

Get the file name associated with the current source file.

Returns:
  The file name.

#### setSourceCodeFilename

    public void setSourceCodeFilename(String filename)

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, `null` if unknown.

#### setLanguageVersion

    public void setLanguageVersion(LanguageVersion languageVersion)

Set the LanguageVersion associated with the current source file. This may be set to `null` to indicate the version is unknown and should be automatically determined.

Parameters:
  `languageVersion` - The LanguageVersion.

#### setAttribute

    public boolean setAttribute(String name,
                       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:
  `true` if the attribute was set, `false` otherwise.
Throws:
  `IllegalArgumentException` - if `name` or `value` are `null`

#### getAttribute

    public Object getAttribute(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 `null` if the attribute does not exist.

#### removeAttribute

    public Object removeAttribute(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 `null` if the attribute does not exist.