Class AbstractCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- All Implemented Interfaces:
Configurable,Contextualizable
- Direct Known Subclasses:
AbbreviationAsWordInNameCheck,AbstractClassCouplingCheck,AbstractClassNameCheck,AbstractJavadocCheck,AbstractNameCheck,AbstractParenPadCheck,AbstractSuperCheck,AnnotationLocationCheck,AnnotationOnSameLineCheck,AnnotationUseStyleCheck,AnonInnerLengthCheck,ArrayTrailingCommaCheck,ArrayTypeStyleCheck,AvoidDoubleBraceInitializationCheck,AvoidEscapedUnicodeCharactersCheck,AvoidInlineConditionalsCheck,AvoidNestedBlocksCheck,AvoidNoArgumentSuperConstructorCallCheck,AvoidStarImportCheck,AvoidStaticImportCheck,BooleanExpressionComplexityCheck,ClassMemberImpliedModifierCheck,CommentsIndentationCheck,CovariantEqualsCheck,CustomImportOrderCheck,CyclomaticComplexityCheck,DeclarationOrderCheck,DefaultComesLastCheck,DescendantTokenCheck,DesignForExtensionCheck,EmptyBlockCheck,EmptyCatchBlockCheck,EmptyForInitializerPadCheck,EmptyForIteratorPadCheck,EmptyLineSeparatorCheck,EmptyStatementCheck,EqualsAvoidNullCheck,EqualsHashCodeCheck,ExecutableStatementCountCheck,ExplicitInitializationCheck,FallThroughCheck,FinalClassCheck,FinalLocalVariableCheck,FinalParametersCheck,GenericWhitespaceCheck,HiddenFieldCheck,HideUtilityClassConstructorCheck,IllegalCatchCheck,IllegalImportCheck,IllegalInstantiationCheck,IllegalThrowsCheck,IllegalTokenCheck,IllegalTokenTextCheck,IllegalTypeCheck,ImportControlCheck,ImportOrderCheck,IndentationCheck,InnerAssignmentCheck,InnerTypeLastCheck,InterfaceIsTypeCheck,InterfaceMemberImpliedModifierCheck,InvalidJavadocPositionCheck,JavadocContentLocationCheck,JavadocMethodCheck,JavadocStyleCheck,JavadocTypeCheck,JavadocVariableCheck,JavaNCSSCheck,LambdaBodyLengthCheck,LeftCurlyCheck,MagicNumberCheck,MatchXpathCheck,MethodCountCheck,MethodLengthCheck,MethodParamPadCheck,MissingCtorCheck,MissingJavadocMethodCheck,MissingJavadocPackageCheck,MissingJavadocTypeCheck,MissingOverrideCheck,MissingSwitchDefaultCheck,ModifiedControlVariableCheck,ModifierOrderCheck,MultipleStringLiteralsCheck,MultipleVariableDeclarationsCheck,MutableExceptionCheck,NeedBracesCheck,NestedForDepthCheck,NestedIfDepthCheck,NestedTryDepthCheck,NoArrayTrailingCommaCheck,NoCloneCheck,NoCodeInFileCheck,NoEnumTrailingCommaCheck,NoFinalizerCheck,NoLineWrapCheck,NoWhitespaceAfterCheck,NoWhitespaceBeforeCaseDefaultColonCheck,NoWhitespaceBeforeCheck,NPathComplexityCheck,OneStatementPerLineCheck,OneTopLevelClassCheck,OperatorWrapCheck,OuterTypeFilenameCheck,OuterTypeNumberCheck,OverloadMethodsDeclarationOrderCheck,PackageAnnotationCheck,PackageDeclarationCheck,PackageNameCheck,ParameterAssignmentCheck,ParameterNumberCheck,RecordComponentNumberCheck,RedundantImportCheck,RedundantModifierCheck,RegexpCheck,RegexpSinglelineJavaCheck,RequireThisCheck,ReturnCountCheck,RightCurlyCheck,SeparatorWrapCheck,SimplifyBooleanExpressionCheck,SimplifyBooleanReturnCheck,SingleSpaceSeparatorCheck,StringLiteralEqualityCheck,SuppressWarningsCheck,SuppressWarningsHolder,ThrowsCountCheck,TodoCommentCheck,TrailingCommentCheck,UncommentedMainCheck,UnnecessaryParenthesesCheck,UnnecessarySemicolonAfterOuterTypeDeclarationCheck,UnnecessarySemicolonAfterTypeMemberDeclarationCheck,UnnecessarySemicolonInEnumerationCheck,UnnecessarySemicolonInTryWithResourcesCheck,UnusedImportsCheck,UnusedLocalVariableCheck,UpperEllCheck,VariableDeclarationUsageDistanceCheck,VisibilityModifierCheck,WhitespaceAfterCheck,WhitespaceAroundCheck,WriteTagCheck
public abstract class AbstractCheck extends AbstractViolationReporter
The base class for checks.- See Also:
- Writing your own checks
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classAbstractCheck.FileContextThe actual context holder.-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.ThreadLocal<AbstractCheck.FileContext>contextThe check context.private inttabWidthThe tab width for column reporting.private java.util.Set<java.lang.String>tokensThe tokens the check is interested in.
-
Constructor Summary
Constructors Constructor Description AbstractCheck()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidbeginTree(DetailAST rootAST)Called before the starting to process a tree.voidclearViolations()Clears the sorted set ofViolationof the check.voiddestroy()Destroy the check.voidfinishTree(DetailAST rootAST)Called after finished processing a tree.abstract int[]getAcceptableTokens()The configurable token set.abstract int[]getDefaultTokens()Returns the default token a check is interested in.FileContentsgetFileContents()Deprecated.Usage of this method is no longer accepted.java.lang.StringgetLine(int index)Returns the line associated with the tree.int[]getLineCodePoints(int index)Returns code point representation of file text from given line number.java.lang.String[]getLines()Returns the lines associated with the tree.abstract int[]getRequiredTokens()The tokens that this check must be registered for.protected intgetTabWidth()Get tab width to report audit events with.java.util.Set<java.lang.String>getTokenNames()Returns the tokens registered for the check.java.util.SortedSet<Violation>getViolations()Returns the sorted set ofViolation.voidinit()Initialize the check.booleanisCommentNodesRequired()Whether comment nodes are required or not.voidleaveToken(DetailAST ast)Called after all the child nodes have been process.voidlog(int lineNo, int colNo, java.lang.String key, java.lang.Object... args)Log a message that has column information.voidlog(int line, java.lang.String key, java.lang.Object... args)Log a message that has no column information.voidlog(DetailAST ast, java.lang.String key, java.lang.Object... args)Helper method to log a Violation.voidsetFileContents(FileContents contents)Set the file contents associated with the tree.voidsetTabWidth(int tabWidth)Set the tab width to report audit events with.voidsetTokens(java.lang.String... strRep)Adds a set of tokens the check is interested in.voidvisitToken(DetailAST ast)Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
context
private final java.lang.ThreadLocal<AbstractCheck.FileContext> context
The check context.
-
tokens
private final java.util.Set<java.lang.String> tokens
The tokens the check is interested in.
-
tabWidth
private int tabWidth
The tab width for column reporting.
-
-
Constructor Detail
-
AbstractCheck
public AbstractCheck()
-
-
Method Detail
-
getDefaultTokens
public abstract int[] getDefaultTokens()
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public abstract int[] getAcceptableTokens()
The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public abstract int[] getRequiredTokens()
The tokens that this check must be registered for.- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
isCommentNodesRequired
public boolean isCommentNodesRequired()
Whether comment nodes are required or not.- Returns:
- false as a default value.
-
setTokens
public final void setTokens(java.lang.String... strRep)
Adds a set of tokens the check is interested in.- Parameters:
strRep- the string representation of the tokens interested in
-
getTokenNames
public final java.util.Set<java.lang.String> getTokenNames()
Returns the tokens registered for the check.- Returns:
- the set of token names
-
getViolations
public java.util.SortedSet<Violation> getViolations()
Returns the sorted set ofViolation.- Returns:
- the sorted set of
Violation.
-
clearViolations
public final void clearViolations()
Clears the sorted set ofViolationof the check.
-
init
public void init()
Initialize the check. This is the time to verify that the check has everything required to perform it job.
-
destroy
public void destroy()
Destroy the check. It is being retired from service.
-
beginTree
public void beginTree(DetailAST rootAST)
Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Parameters:
rootAST- the root of the tree
-
finishTree
public void finishTree(DetailAST rootAST)
Called after finished processing a tree. Ideal place to report on information collected whilst processing a tree.- Parameters:
rootAST- the root of the tree
-
visitToken
public void visitToken(DetailAST ast)
Called to process a token.- Parameters:
ast- the token to process
-
leaveToken
public void leaveToken(DetailAST ast)
Called after all the child nodes have been process.- Parameters:
ast- the token leaving
-
setFileContents
public final void setFileContents(FileContents contents)
Set the file contents associated with the tree.- Parameters:
contents- the manager
-
getFileContents
@Deprecated(since="9.3") public final FileContents getFileContents()
Deprecated.Usage of this method is no longer accepted. Please use AST based methods instead.Returns the file contents associated with the tree.- Returns:
- the file contents
-
getTabWidth
protected final int getTabWidth()
Get tab width to report audit events with.- Returns:
- the tab width to audit events with
-
setTabWidth
public final void setTabWidth(int tabWidth)
Set the tab width to report audit events with.- Parameters:
tabWidth- anintvalue
-
log
public final void log(int line, java.lang.String key, java.lang.Object... args)
Description copied from class:AbstractViolationReporterLog a message that has no column information.- Specified by:
login classAbstractViolationReporter- Parameters:
line- the line number where the audit event was foundkey- the message that describes the audit eventargs- the details of the message- See Also:
MessageFormat
-
log
public final void log(int lineNo, int colNo, java.lang.String key, java.lang.Object... args)
Description copied from class:AbstractViolationReporterLog a message that has column information.- Specified by:
login classAbstractViolationReporter- Parameters:
lineNo- the line number where the audit event was foundcolNo- the column number where the audit event was foundkey- the message that describes the audit eventargs- the details of the message- See Also:
MessageFormat
-
log
public final void log(DetailAST ast, java.lang.String key, java.lang.Object... args)
Helper method to log a Violation.- Parameters:
ast- a node to get line id column numbers associated with the violationkey- key to locale violation formatargs- arguments to format
-
getLines
public final java.lang.String[] getLines()
Returns the lines associated with the tree.- Returns:
- the file contents
-
getLine
public final java.lang.String getLine(int index)
Returns the line associated with the tree.- Parameters:
index- index of the line- Returns:
- the line from the file contents
-
getLineCodePoints
public final int[] getLineCodePoints(int index)
Returns code point representation of file text from given line number.- Parameters:
index- index of the line- Returns:
- the array of Unicode code points
-
-