Class AbstractRule

    • Field Detail

      • LOG

        private static final org.slf4j.Logger LOG
      • enabled

        private boolean enabled
        Flag indicating whether this rule should be enabled (applied). Defaults to true. If set to false, this rule will not produce any violations.
      • applyToFilesMatching

        private java.lang.String applyToFilesMatching
        This rule is only applied to source code (file) pathnames matching this regular expression.
      • doNotApplyToFilesMatching

        private java.lang.String doNotApplyToFilesMatching
        This rule is NOT applied to source code (file) pathnames matching this regular expression.
      • applyToFileNames

        private java.lang.String applyToFileNames
        This rule is only applied to source code (file) names matching this value. The name may optionally contain a path. If a path is specified, then the source code path must match it. If no path is specified, then only the source code (file) name is compared (i.e., its path is ignored). The value may optionally be a comma-separated list of names, in which case one of the names must match. The name(s) may optionally include wildcard characters ('*' or '?').
      • doNotApplyToFileNames

        private java.lang.String doNotApplyToFileNames
        This rule is NOT applied to source code (file) names matching this value. The name may optionally contain a path. If a path is specified, then the source code path must match it. If no path is specified, then only the source code (file) name is compared (i.e., its path is ignored). The value may optionally be a comma-separated list of names, in which case any one of the names can match. The name(s) may optionally include wildcard characters ('*' or '?').
      • violationMessage

        private java.lang.String violationMessage
        If not null, this is used as the message for all violations of this rule, overriding any message generated by the concrete rule subclass. Defaults to null. Note that setting this to an empty string "hides" the message, if any, generated by the actual rule.
      • description

        private java.lang.String description
        If not null, this is used as the description text for this rule, overriding any description text found in the i18n resource bundles. Defaults to null.
    • Constructor Detail

      • AbstractRule

        public AbstractRule()
    • Method Detail

      • getName

        public abstract java.lang.String getName()
        Specified by:
        getName in interface Rule
        Returns:
        the unique name for this rule
      • setName

        public abstract void setName​(java.lang.String name)
        Set the unique name for this rule
        Parameters:
        name - - the name for this rule; this should be unique
      • getPriority

        public abstract int getPriority()
        Specified by:
        getPriority in interface Rule
        Returns:
        the priority of this rule, between 1 (highest priority) and 3 (lowest priority), inclusive.
      • setPriority

        public abstract void setPriority​(int priority)
        Set the priority for this rule
        Parameters:
        priority - - the priority of this rule, between 1 (highest priority) and 3 (lowest priority), inclusive.
      • applyTo

        public abstract void applyTo​(SourceCode sourceCode,
                                     java.util.List<Violation> violations)
        Apply this rule to the specified source and return a list of violations (or an empty List)
        Parameters:
        sourceCode - - the source to apply this rule to
        violations - - the List of violations to which new violations from this rule are to be added
      • applyTo

        public java.util.List<Violation> applyTo​(SourceCode sourceCode)
                                          throws java.lang.Throwable
        Apply this rule to the specified source and return a list of violations (or an empty List). This implementation delegates to the abstract applyCode(SourceCode,List), provided by concrete subclasses. This template method simplifies subclass implementations and also enables common handling of enablement logic.
        Specified by:
        applyTo in interface Rule
        Parameters:
        sourceCode - - the source to apply this rule to
        Returns:
        the List of violations; may be empty
        Throws:
        java.lang.Throwable - could throw anything
      • validateAstCompilerPhase

        private void validateAstCompilerPhase​(SourceCode sourceCode)
      • isReady

        public boolean isReady()
        Allows rules to check whether preconditions are satisfied and short-circuit execution (i.e., do nothing) if those preconditions are not satisfied. Return true by default. This method is provided as a placeholder so subclasses can optionally override.
        Returns:
        true if all preconditions for this rule are satisfied
      • validate

        public void validate()
        Allows rules to perform validation. Do nothing by default. This method is provided as a placeholder so subclasses can optionally override. Subclasses will typically use assert calls to verify required preconditions.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • createViolation

        protected Violation createViolation​(java.lang.Integer lineNumber,
                                            java.lang.String sourceLine,
                                            java.lang.String message)
        Create and return a new Violation for this rule and the specified values
        Parameters:
        lineNumber - - the line number for the violation; may be null
        sourceLine - - the source line for the violation; may be null
        message - - the message for the violation; may be null
        Returns:
        a new Violation object
      • createViolation

        protected Violation createViolation​(SourceCode sourceCode,
                                            org.codehaus.groovy.ast.ASTNode node,
                                            java.lang.String message)
        Create a new Violation for the AST node.
        Parameters:
        sourceCode - - the SourceCode
        node - - the Groovy AST Node
        message - - the message for the violation; defaults to null
      • createViolationForImport

        protected Violation createViolationForImport​(SourceCode sourceCode,
                                                     org.codehaus.groovy.ast.ImportNode importNode,
                                                     java.lang.String message)
        Create and return a new Violation for this rule and the specified import
        Parameters:
        sourceCode - - the SourceCode
        importNode - - the ImportNode for the import triggering the violation
        Returns:
        a new Violation object
      • createViolationForImport

        protected Violation createViolationForImport​(SourceCode sourceCode,
                                                     java.lang.String className,
                                                     java.lang.String alias,
                                                     java.lang.String violationMessage)
        Create and return a new Violation for this rule and the specified import className and alias
        Parameters:
        sourceCode - - the SourceCode
        className - - the class name (as specified within the import statement)
        alias - - the alias for the import statement
        violationMessage - - the violation message; may be null
        Returns:
        a new Violation object
      • shouldApplyThisRuleTo

        private boolean shouldApplyThisRuleTo​(SourceCode sourceCode)
      • getClassNameNoPackage

        private java.lang.String getClassNameNoPackage()
      • overrideViolationMessageIfNecessary

        private void overrideViolationMessageIfNecessary​(java.util.List<Violation> violations)
        If the violationMessage property of this rule has been set, then use it to set the message within each violation, overriding the original message(s), if any.
      • isEnabled

        public boolean isEnabled()
      • setEnabled

        public void setEnabled​(boolean enabled)
      • getApplyToFilesMatching

        public java.lang.String getApplyToFilesMatching()
      • setApplyToFilesMatching

        public void setApplyToFilesMatching​(java.lang.String applyToFilesMatching)
      • getDoNotApplyToFilesMatching

        public java.lang.String getDoNotApplyToFilesMatching()
      • setDoNotApplyToFilesMatching

        public void setDoNotApplyToFilesMatching​(java.lang.String doNotApplyToFilesMatching)
      • getApplyToFileNames

        public java.lang.String getApplyToFileNames()
      • setApplyToFileNames

        public void setApplyToFileNames​(java.lang.String applyToFileNames)
      • getDoNotApplyToFileNames

        public java.lang.String getDoNotApplyToFileNames()
      • setDoNotApplyToFileNames

        public void setDoNotApplyToFileNames​(java.lang.String doNotApplyToFileNames)
      • getViolationMessage

        public java.lang.String getViolationMessage()
      • setViolationMessage

        public void setViolationMessage​(java.lang.String violationMessage)
      • getDescription

        public java.lang.String getDescription()
      • setDescription

        public void setDescription​(java.lang.String description)
      • validateRegularExpression

        private void validateRegularExpression​(java.lang.String regex)