Groovy Documentation

org.codenarc.rule
[Groovy] Class AbstractRule

java.lang.Object
  org.codenarc.rule.AbstractRule
All Implemented Interfaces:
Rule

@SuppressWarnings('DuplicateLiteral')
abstract class AbstractRule

Abstract superclass for Rules.

Each subclass must define an name property (String) and a priority property (integer 1..3).

Authors:
Chris Mair
Version:
\$Revision: 541 \$ - \$Date: 2011-01-17 18:34:13 -0500 (Mon, 17 Jan 2011) \$


Property Summary
static def LOG

String applyToFileNames

This rule is only applied to source code (file) names matching this value.

String applyToFilesMatching

This rule is only applied to source code (file) pathnames matching this regular expression.

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.

String doNotApplyToFileNames

This rule is NOT applied to source code (file) names matching this value.

String doNotApplyToFilesMatching

This rule is NOT applied to source code (file) pathnames matching this regular expression.

boolean enabled

Flag indicating whether this rule should be enabled (applied).

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.

 
Method Summary
void applyTo(SourceCode sourceCode, List violations)

Apply this rule to the specified source and return a list of violations (or an empty List)

List applyTo(SourceCode sourceCode)

Apply this rule to the specified source and return a list of violations (or an empty List).

protected Violation createViolation(Integer lineNumber, String sourceLine = null, String message = null)

Create and return a new Violation for this rule and the specified values

protected Violation createViolation(SourceCode sourceCode, org.codehaus.groovy.ast.ASTNode node, def message = null)

Create a new Violation for the AST node.

protected Violation createViolationForImport(SourceCode sourceCode, org.codehaus.groovy.ast.ImportNode importNode)

Create and return a new Violation for this rule and the specified import

protected Violation createViolationForImport(SourceCode sourceCode, String className, String alias)

Create and return a new Violation for this rule and the specified import className and alias

protected List getImportsSortedByLineNumber(def sourceCode)

String getName()

@return the unique name for this rule

int getPriority()

@return the priority of this rule, between 1 (highest priority) and 3 (lowest priority), inclusive.

boolean isReady()

Allows rules to check whether preconditions are satisfied and short-circuit execution (i.e., do nothing) if those preconditions are not satisfied.

protected String packageNameForImport(org.codehaus.groovy.ast.ImportNode importNode)

Return the package name for the specified import statement or else an empty String

void setName(String name)

Set the unique name for this rule

void setPriority(int priority)

Set the priority for this rule

protected def sourceLineAndNumberForImport(SourceCode sourceCode, String className, String alias)

Return the source line and line number for the specified import class name and alias

protected def sourceLineAndNumberForImport(SourceCode sourceCode, org.codehaus.groovy.ast.ImportNode importNode)

Return the source line and line number for the specified import

String toString()

void validate()

Allows rules to perform validation.

 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Property Detail

LOG

static final def LOG


applyToFileNames

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 '?').


applyToFilesMatching

String applyToFilesMatching
This rule is only applied to source code (file) pathnames matching this regular expression.


description

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.


doNotApplyToFileNames

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 '?').


doNotApplyToFilesMatching

String doNotApplyToFilesMatching
This rule is NOT applied to source code (file) pathnames matching this regular expression.


enabled

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.


violationMessage

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.


 
Method Detail

applyTo

void applyTo(SourceCode sourceCode, List violations)
Apply this rule to the specified source and return a list of violations (or an empty List)
Parameters:
source - - the source to apply this rule to
violations - - the List of violations to which new violations from this rule are to be added


applyTo

List applyTo(SourceCode sourceCode)
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.
Parameters:
source - - the source to apply this rule to
Returns:
the List of violations; may be empty


createViolation

protected Violation createViolation(Integer lineNumber, String sourceLine = null, String message = null)
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, def message = null)
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)
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, String className, String alias)
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 statemenet)
alias - - the alias for the import statemenet
Returns:
a new Violation object


getImportsSortedByLineNumber

protected List getImportsSortedByLineNumber(def sourceCode)


getName

String getName()
Returns:
the unique name for this rule


getPriority

int getPriority()
Returns:
the priority of this rule, between 1 (highest priority) and 3 (lowest priority), inclusive.


isReady

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


packageNameForImport

protected String packageNameForImport(org.codehaus.groovy.ast.ImportNode importNode)
Return the package name for the specified import statement or else an empty String
Parameters:
importNode - - the ImportNode for the import
Returns:
the name package being imported (i.e., the import minus the class name/spec) or an empty String if the import contains no package component


setName

void setName(String name)
Set the unique name for this rule
Parameters:
name - - the name for this rule; this should be unique


setPriority

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.


sourceLineAndNumberForImport

protected def sourceLineAndNumberForImport(SourceCode sourceCode, String className, String alias)
Return the source line and line number for the specified import class name and alias
Parameters:
sourceCode - - the SourceCode being processed
importNode - - the ImportNode representing the import
Returns:
an object that has 'sourceLine' and 'lineNumber' fields


sourceLineAndNumberForImport

protected def sourceLineAndNumberForImport(SourceCode sourceCode, org.codehaus.groovy.ast.ImportNode importNode)
Return the source line and line number for the specified import
Parameters:
sourceCode - - the SourceCode being processed
importNode - - the ImportNode representing the import
Returns:
an object that has 'sourceLine' and 'lineNumber' fields


toString

String toString()


validate

@SuppressWarnings('EmptyMethodInAbstractClass')
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.


 

Groovy Documentation