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
public abstract class AbstractRule extends Object implements Rule
Abstract superclass for Rules. Each subclass must define annameproperty (String) and apriorityproperty (integer 1..3).- Author:
- Chris Mair, Hamlet D'Arcy
-
-
Constructor Summary
Constructors Constructor Description AbstractRule()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description List<Violation>applyTo(SourceCode sourceCode)Apply this rule to the specified source and return a list of violations (or an empty List).abstract voidapplyTo(SourceCode sourceCode, List<Violation> violations)Apply this rule to the specified source and return a list of violations (or an empty List)protected ViolationcreateViolation(Integer lineNumber, String sourceLine, String message)Create and return a new Violation for this rule and the specified valuesprotected ViolationcreateViolation(SourceCode sourceCode, org.codehaus.groovy.ast.ASTNode node, String message)Create a new Violation for the AST node.protected ViolationcreateViolationForImport(SourceCode sourceCode, String className, String alias, String violationMessage)Create and return a new Violation for this rule and the specified import className and aliasprotected ViolationcreateViolationForImport(SourceCode sourceCode, org.codehaus.groovy.ast.ImportNode importNode, String message)Create and return a new Violation for this rule and the specified importStringgetApplyToFileNames()StringgetApplyToFilesMatching()intgetCompilerPhase()StringgetDescription()StringgetDoNotApplyToFileNames()StringgetDoNotApplyToFilesMatching()abstract StringgetName()abstract intgetPriority()StringgetViolationMessage()booleanisEnabled()booleanisReady()Allows rules to check whether preconditions are satisfied and short-circuit execution (i.e., do nothing) if those preconditions are not satisfied.voidsetApplyToFileNames(String applyToFileNames)voidsetApplyToFilesMatching(String applyToFilesMatching)voidsetDescription(String description)voidsetDoNotApplyToFileNames(String doNotApplyToFileNames)voidsetDoNotApplyToFilesMatching(String doNotApplyToFilesMatching)voidsetEnabled(boolean enabled)abstract voidsetName(String name)Set the unique name for this ruleabstract voidsetPriority(int priority)Set the priority for this rulevoidsetViolationMessage(String violationMessage)StringtoString()voidvalidate()Allows rules to perform validation.
-
-
-
Method Detail
-
getName
public abstract String getName()
-
setName
public abstract void setName(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:
getPriorityin 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:
getCompilerPhasein interfaceRule- Returns:
- the required compiler phase (as in
Phases) of the AST of theSourceCodehanded to the rule viaapplyTo(SourceCode sourceCode)
-
applyTo
public 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)- 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
public List<Violation> applyTo(SourceCode sourceCode) throws 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.
-
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 useassertcalls to verify required preconditions.
-
createViolation
protected Violation createViolation(Integer lineNumber, String sourceLine, 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 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
-
isEnabled
public boolean isEnabled()
-
setEnabled
public void setEnabled(boolean enabled)
-
getApplyToFilesMatching
public String getApplyToFilesMatching()
-
setApplyToFilesMatching
public void setApplyToFilesMatching(String applyToFilesMatching)
-
getDoNotApplyToFilesMatching
public String getDoNotApplyToFilesMatching()
-
setDoNotApplyToFilesMatching
public void setDoNotApplyToFilesMatching(String doNotApplyToFilesMatching)
-
getApplyToFileNames
public String getApplyToFileNames()
-
setApplyToFileNames
public void setApplyToFileNames(String applyToFileNames)
-
getDoNotApplyToFileNames
public String getDoNotApplyToFileNames()
-
setDoNotApplyToFileNames
public void setDoNotApplyToFileNames(String doNotApplyToFileNames)
-
getViolationMessage
public String getViolationMessage()
-
setViolationMessage
public void setViolationMessage(String violationMessage)
-
getDescription
public String getDescription()
-
setDescription
public void setDescription(String description)
-
-