Package org.codenarc.rule
Class AbstractRule
java.lang.Object
org.codenarc.rule.AbstractRule
- All Implemented Interfaces:
Rule
- Direct Known Subclasses:
AbstractAstVisitorRule
,AbstractImportRule
,BlankLineBeforePackageRule
,BracesForClassRule
,ClassJavadocRule
,ClassNameSameAsFilenameRule
,ConsecutiveBlankLinesRule
,FileEndsWithoutNewlineRule
,IllegalRegexRule
,IllegalStringRule
,ImportFromSamePackageRule
,JavadocEmptyFirstLineRule
,MissingBlankLineAfterImportsRule
,MissingBlankLineAfterPackageRule
,NoTabCharacterRule
,PackageNameMatchesFilePathRule
,RequiredRegexRule
,RequiredStringRule
,StubRule
,TrailingWhitespaceRule
,UnnecessaryGroovyImportRule
,UnusedImportRule
Abstract superclass for Rules.
Each subclass must define an
name
property (String) and a priority
property
(integer 1..3).-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String
This rule is only applied to source code (file) names matching this value.private String
This rule is only applied to source code (file) pathnames matching this regular expression.private String
If not null, this is used as the description text for this rule, overriding any description text found in the i18n resource bundles.private String
This rule is NOT applied to source code (file) names matching this value.private String
This rule is NOT applied to source code (file) pathnames matching this regular expression.private boolean
Flag indicating whether this rule should be enabled (applied).private static final org.slf4j.Logger
private String
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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionapplyTo
(SourceCode sourceCode) Apply this rule to the specified source and return a list of violations (or an empty List).abstract void
applyTo
(SourceCode sourceCode, List<Violation> violations) Apply this rule to the specified source and return a list of violations (or an empty List)protected Violation
createViolation
(Integer lineNumber, String sourceLine, String message) Create and return a new Violation for this rule and the specified valuesprotected Violation
createViolation
(SourceCode sourceCode, org.codehaus.groovy.ast.ASTNode node, String message) Create a new Violation for the AST node.protected Violation
createViolationForImport
(SourceCode sourceCode, String className, String alias, String violationMessage) Create and return a new Violation for this rule and the specified import className and aliasprotected Violation
createViolationForImport
(SourceCode sourceCode, org.codehaus.groovy.ast.ImportNode importNode, String message) Create and return a new Violation for this rule and the specified importprivate String
int
abstract String
getName()
abstract int
boolean
boolean
isReady()
Allows rules to check whether preconditions are satisfied and short-circuit execution (i.e., do nothing) if those preconditions are not satisfied.private void
overrideViolationMessageIfNecessary
(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.void
setApplyToFileNames
(String applyToFileNames) void
setApplyToFilesMatching
(String applyToFilesMatching) void
setDescription
(String description) void
setDoNotApplyToFileNames
(String doNotApplyToFileNames) void
setDoNotApplyToFilesMatching
(String doNotApplyToFilesMatching) void
setEnabled
(boolean enabled) abstract void
Set the unique name for this ruleabstract void
setPriority
(int priority) Set the priority for this rulevoid
setViolationMessage
(String violationMessage) private boolean
shouldApplyThisRuleTo
(SourceCode sourceCode) toString()
void
validate()
Allows rules to perform validation.private void
validateAstCompilerPhase
(SourceCode sourceCode) private void
validateRegularExpression
(String regex)
-
Field Details
-
LOG
private static final org.slf4j.Logger LOG -
enabled
private boolean enabledFlag indicating whether this rule should be enabled (applied). Defaults to true. If set to false, this rule will not produce any violations. -
applyToFilesMatching
This rule is only applied to source code (file) pathnames matching this regular expression. -
doNotApplyToFilesMatching
This rule is NOT applied to source code (file) pathnames matching this regular expression. -
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
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
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
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 Details
-
AbstractRule
public AbstractRule()
-
-
Method Details
-
getName
-
setName
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 interfaceRule
- 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.
-
getCompilerPhase
public int getCompilerPhase()- Specified by:
getCompilerPhase
in interfaceRule
- Returns:
- the required compiler phase (as in
Phases
) of the AST of theSourceCode
handed to the rule viaapplyTo(SourceCode sourceCode)
-
applyTo
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 toviolations
- - the List of violations to which new violations from this rule are to be added
-
applyTo
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. -
validateAstCompilerPhase
-
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 useassert
calls to verify required preconditions. -
toString
-
createViolation
Create and return a new Violation for this rule and the specified values- Parameters:
lineNumber
- - the line number for the violation; may be nullsourceLine
- - the source line for the violation; may be nullmessage
- - 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, String message) Create a new Violation for the AST node.- Parameters:
sourceCode
- - the SourceCodenode
- - the Groovy AST Nodemessage
- - the message for the violation; defaults to null
-
createViolationForImport
protected Violation createViolationForImport(SourceCode sourceCode, org.codehaus.groovy.ast.ImportNode importNode, String message) Create and return a new Violation for this rule and the specified import- Parameters:
sourceCode
- - the SourceCodeimportNode
- - the ImportNode for the import triggering the violation- Returns:
- a new Violation object
-
createViolationForImport
protected Violation createViolationForImport(SourceCode sourceCode, String className, String alias, String violationMessage) Create and return a new Violation for this rule and the specified import className and alias- Parameters:
sourceCode
- - the SourceCodeclassName
- - the class name (as specified within the import statement)alias
- - the alias for the import statementviolationMessage
- - the violation message; may be null- Returns:
- a new Violation object
-
shouldApplyThisRuleTo
-
getClassNameNoPackage
-
overrideViolationMessageIfNecessary
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
-
setApplyToFilesMatching
-
getDoNotApplyToFilesMatching
-
setDoNotApplyToFilesMatching
-
getApplyToFileNames
-
setApplyToFileNames
-
getDoNotApplyToFileNames
-
setDoNotApplyToFileNames
-
getViolationMessage
-
setViolationMessage
-
getDescription
-
setDescription
-
validateRegularExpression
-