Class RuleSet

  • All Implemented Interfaces:

    public class RuleSet
    extends Object
    implements ChecksumAware
    This class represents a collection of rules along with some optional filter patterns that can preclude their application on specific files.
    See Also:
    • Constructor Detail

      • RuleSet

        public RuleSet​(RuleSet rs)
    • Method Detail

      • forSingleRule

        public static RuleSet forSingleRule​(Rule rule)
        Creates a new ruleset containing a single rule. The ruleset will have default description, name, and null file name.
        rule - The rule being created
        The newly created RuleSet
      • create

        public static RuleSet create​(String name,
                                     String description,
                                     String fileName,
                                     Collection<Pattern> excludePatterns,
                                     Collection<Pattern> includePatterns,
                                     Iterable<? extends Rule> rules)
        Creates a new ruleset with the given metadata such as name, description, fileName, exclude/include patterns are used. The rules are taken from the given collection.

        Note: The rule instances are shared between the collection and the new ruleset (copy-by-reference). This might lead to concurrency issues, if the rules of the collection are also referenced by other rulesets and used in different threads.

        name - the name of the ruleset
        description - the description
        fileName - the filename
        excludePatterns - list of exclude patterns
        includePatterns - list of include patterns, that override the exclude patterns
        rules - the collection with the rules to add to the new ruleset
        the new ruleset
        NullPointerException - If any parameter is null, or the collections contain null elements
      • copy

        public static RuleSet copy​(RuleSet original)
        Creates a copy of the given ruleset. All properties like name, description, fileName and exclude/include patterns are copied.

        Note: The rule instances are shared between the original and the new ruleset (copy-by-reference). This might lead to concurrency issues, if the original ruleset and the new ruleset are used in different threads.

        original - the original rule set to copy from
        the copy
      • size

        public int size()
        Returns the number of rules in this ruleset
        an int representing the number of rules
      • getRules

        public Collection<Rule> getRules()
        Returns the actual Collection of rules in this ruleset
        a Collection with the rules. All objects are of type Rule
      • getRuleByName

        public Rule getRuleByName​(String ruleName)
        Returns the first Rule found with the given name (case-sensitive). Note: Since we support multiple languages, rule names are not expected to be unique within any specific ruleset.
        ruleName - the exact name of the rule to find
        the rule or null if not found
      • applies

        public boolean applies​(String qualFileName)
        Check if a given source file should be checked by rules in this RuleSet. A file should not be checked if there is an exclude pattern which matches the file, unless there is an include pattern which also matches the file. In other words, include patterns override exclude patterns.
        qualFileName - the source path to check
        true if the file should be checked, false otherwise
      • applies

        public static boolean applies​(Rule rule,
                                      LanguageVersion languageVersion)
        This is internal API, removed in PMD 7. You should not use a ruleset directly.
        Does the given Rule apply to the given LanguageVersion? If so, the Language must be the same and be between the minimum and maximums versions on the Rule.
        rule - The rule.
        languageVersion - The language version.
        true if the given rule matches the given language, which means, that the rule would be executed.
      • equals

        public boolean equals​(Object o)
        Two rulesets are equals, if they have the same name and contain the same rules.
        equals in class Object
        o - the other ruleset to compare with
        true if o is a ruleset with the same name and rules, false otherwise
      • hashCode

        public int hashCode()
        hashCode in class Object
      • getFileName

        public String getFileName()
      • getName

        public String getName()
      • getDescription

        public String getDescription()
      • getFileExclusions

        public List<Pattern> getFileExclusions()
        Returns the file exclusion patterns as an unmodifiable list.
      • getFileInclusions

        public List<Pattern> getFileInclusions()
        Returns the file inclusion patterns as an unmodifiable list.
      • removeDysfunctionalRules

        public void removeDysfunctionalRules​(Collection<Rule> collector)
        This is internal API, removed in PMD 7. You should not use a ruleset directly.
        Remove and collect any misconfigured rules. TODO remove this method. This mutates rulesets for nothing. Whether a rule is dysfunctional or not should be checked when it is initialized.
        collector - the removed rules will be added to this collection
      • getChecksum

        public long getChecksum()
        Description copied from interface: ChecksumAware
        Retrieves the current instance checksum
        Specified by:
        getChecksum in interface ChecksumAware
        The current checksum