Class 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.
    As well as the following source file specific information:
    • A File for 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 Detail

      • RuleContext

        @Deprecated
        @InternalApi
        public RuleContext()
        Deprecated.
        Internal API, removed in PMD 7
        Default constructor.
      • RuleContext

        @Deprecated
        @InternalApi
        public RuleContext​(RuleContext ruleContext)
        Deprecated.
        Internal API, removed in PMD 7
        Constructor which shares attributes and report listeners with the given RuleContext.
        Parameters:
        ruleContext - the context from which the values are shared
    • Method Detail

      • 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 violation
        formatArgs - 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 a MessageFormat and should hence use appropriate escapes. No formatting arguments are provided.
        Parameters:
        location - Location of the violation
        message - 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 a MessageFormat and should hence use appropriate escapes. The given formatting arguments are used.
        Parameters:
        location - Location of the violation
        message - Violation message
        formatArgs - 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 a MessageFormat and should hence use appropriate escapes. The given formatting arguments are used.
        Parameters:
        location - Location of the violation
        message - Violation message
        formatArgs - Format arguments for the message
      • getReport

        @Deprecated
        @InternalApi
        public Report getReport()
        Deprecated.
        Internal API, removed in PMD 7
        Get 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 7
        Set the Report to which Rule Violations are sent.
        Parameters:
        report - The Report.
      • getSourceCodeFile

        @Deprecated
        @InternalApi
        public File getSourceCodeFile()
        Deprecated.
        Internal API, removed in PMD 7
        Get 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 7
        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

        @Deprecated
        @InternalApi
        public String getSourceCodeFilename()
        Deprecated.
        Internal API, removed in PMD 7
        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
        @InternalApi
        public void setSourceCodeFilename​(String filename)
        Deprecated.
        Internal API, removed in PMD 7
        Set 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 7
        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

        @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 - if name or value are null
      • 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 7
        Configure whether exceptions during applying a rule should be ignored or not. If set to true 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 to false 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 - if true simply skip failing rules (default).
      • isIgnoreExceptions

        @Deprecated
        @InternalApi
        public boolean isIgnoreExceptions()
        Deprecated.
        Internal API, removed in PMD 7
        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:
        true when failing rules are skipped, false otherwise.