Groovy Documentation

org.codenarc.rule
Class AbstractRule

java.lang.Object
  org.codenarc.rule.AbstractRule

class AbstractRule

Abstract superclass for Rules.

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

author:
Chris Mair
version:
$Revision: 302 $ - $Date: 2010-01-31 19:06:00 -0500 (Sun, 31 Jan 2010) $


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.

 
Constructor Summary
AbstractRule()

 
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, ASTNode node, def message = null)

Create a new Violation for the AST node.

protected Violation createViolationForImport(SourceCode sourceCode, 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

String getApplyToFilenames()

Throws UnsupportedOperationException

String getDoNotApplyToFilenames()

Throws UnsupportedOperationException

protected List getImportsSortedByLineNumber(def sourceCode)

String getName()

int getPriority()

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(ImportNode importNode)

Return the package name for the specified import statement or else an empty String or an empty String if the import contains no package component

void setApplyToFilenames(String applyTo)

Throws UnsupportedOperationException

void setDoNotApplyToFilenames(String doNotApply)

Throws UnsupportedOperationException

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, 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.


 
Constructor Detail

AbstractRule

AbstractRule()


 
Method Detail

applyTo

public void applyTo(SourceCode sourceCode, List violations)
Apply this rule to the specified source and return a list of violations (or an empty List)
param:
source - the source to apply this rule to
param:
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.
param:
source - the source to apply this rule to
return:
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
param:
lineNumber - the line number for the violation; may be null
param:
sourceLine - the source line for the violation; may be null
param:
message - the message for the violation; may be null
return:
a new Violation object


createViolation

protected Violation createViolation(SourceCode sourceCode, ASTNode node, def message = null)
Create a new Violation for the AST node.
param:
sourceCode - the SourceCode
param:
node - the Groovy AST Node
param:
message - the message for the violation; defaults to null


createViolationForImport

protected Violation createViolationForImport(SourceCode sourceCode, ImportNode importNode)
Create and return a new Violation for this rule and the specified import
param:
sourceCode - the SourceCode
param:
importNode - the ImportNode for the import triggering the violation
return:
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
param:
sourceCode - the SourceCode
param:
className - the class name (as specified within the import statemenet)
param:
alias - the alias for the import statemenet
return:
a new Violation object


getApplyToFilenames

String getApplyToFilenames()
Throws UnsupportedOperationException
deprecated:
Use getApplyToFileNames() instead


getDoNotApplyToFilenames

String getDoNotApplyToFilenames()
Throws UnsupportedOperationException
deprecated:
Use doNotApplyToFileNames instead


getImportsSortedByLineNumber

protected List getImportsSortedByLineNumber(def sourceCode)


getName

public String getName()
return:
the unique name for this rule


getPriority

public int getPriority()
return:
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.
return:
true if all preconditions for this rule are satisfied


packageNameForImport

protected String packageNameForImport(ImportNode importNode)
Return the package name for the specified import statement or else an empty String
param:
importNode - the ImportNode for the import
return:
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


setApplyToFilenames

void setApplyToFilenames(String applyTo)
Throws UnsupportedOperationException
deprecated:
Use setApplyToFileNames(String) instead


setDoNotApplyToFilenames

void setDoNotApplyToFilenames(String doNotApply)
Throws UnsupportedOperationException
deprecated:
Use setDoNotApplyToFileNames(String) instead


setName

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


setPriority

public void setPriority(int priority)
Set the priority for this rule
param:
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
param:
sourceCode - the SourceCode being processed
param:
importNode - the ImportNode representing the import
return:
an object that has 'sourceLine' and 'lineNumber' fields


sourceLineAndNumberForImport

protected def sourceLineAndNumberForImport(SourceCode sourceCode, ImportNode importNode)
Return the source line and line number for the specified import
param:
sourceCode - the SourceCode being processed
param:
importNode - the ImportNode representing the import
return:
an object that has 'sourceLine' and 'lineNumber' fields


toString

String toString()


validate

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