Package com.puppycrawl.tools.checkstyle
Class TreeWalker
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
-
- com.puppycrawl.tools.checkstyle.TreeWalker
-
- All Implemented Interfaces:
Configurable,Contextualizable,ExternalResourceHolder,FileSetCheck
public final class TreeWalker extends AbstractFileSetCheck implements ExternalResourceHolder
Responsible for walking an abstract syntax tree and notifying interested checks at each node.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classTreeWalker.AstStateState of AST.-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private ContextchildContextContext of child components.private java.util.Set<AbstractCheck>commentChecksRegistered comment checks.private java.util.Set<TreeWalkerFilter>filtersThe ast filters.private ModuleFactorymoduleFactoryA factory for creating submodules (i.e.private java.util.Set<AbstractCheck>ordinaryChecksRegistered ordinary checks, that don't use comment nodes.private java.util.Map<java.lang.Integer,java.util.Set<AbstractCheck>>tokenToCommentChecksMaps from token name to comment checks.private java.util.Map<java.lang.Integer,java.util.Set<AbstractCheck>>tokenToOrdinaryChecksMaps from token name to ordinary checks.private java.util.SortedSet<Violation>violationsThe sorted set of violations.
-
Constructor Summary
Constructors Constructor Description TreeWalker()Creates a newTreeWalkerinstance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.util.SortedSet<AbstractCheck>createNewCheckSortedSet()Creates a newSortedSetwith a deterministic order based on the Check's name before the default ordering.voiddestroy()Cleans up the object.voidfinishLocalSetup()Provides a hook to finish the part of this component's setup that was not handled by the bean introspection.java.util.Set<java.lang.String>getExternalResourceLocations()Returns a set of external configuration resource locations which are used by the module.private java.util.SortedSet<Violation>getFilteredViolations(java.lang.String fileName, FileContents fileContents, DetailAST rootAST)Returns filtered set ofViolation.private java.util.Collection<AbstractCheck>getListOfChecks(DetailAST ast, TreeWalker.AstState astState)Method returns list of checks.private voidnotifyBegin(DetailAST rootAST, FileContents contents, TreeWalker.AstState astState)Notify checks that we are about to begin walking a tree.private voidnotifyEnd(DetailAST rootAST, TreeWalker.AstState astState)Notify checks that we have finished walking a tree.private voidnotifyLeave(DetailAST ast, TreeWalker.AstState astState)Notify checks that leaving a node.private voidnotifyVisit(DetailAST ast, TreeWalker.AstState astState)Notify checks that visiting a node.protected voidprocessFiltered(java.io.File file, FileText fileText)Called to process a file that matches the specified file extensions.private voidprocessIter(DetailAST root, TreeWalker.AstState astState)Processes a node calling interested checks at each node.private voidregisterCheck(int tokenId, AbstractCheck check)Register a check for a specified token id.private voidregisterCheck(AbstractCheck check)Register a check for a given configuration.voidsetModuleFactory(ModuleFactory moduleFactory)Sets the module factory for creating child modules (Checks).voidsetupChild(Configuration childConf)Called by configure() for every child of this component's Configuration.private voidwalk(DetailAST ast, FileContents contents, TreeWalker.AstState astState)Initiates the walk of an AST.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
addViolations, beginProcessing, finishProcessing, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getTabWidth, getViolations, init, log, log, process, setFileContents, setFileExtensions, setMessageDispatcher, setTabWidth
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Configurable
configure
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Contextualizable
contextualize
-
-
-
-
Field Detail
-
tokenToOrdinaryChecks
private final java.util.Map<java.lang.Integer,java.util.Set<AbstractCheck>> tokenToOrdinaryChecks
Maps from token name to ordinary checks.
-
tokenToCommentChecks
private final java.util.Map<java.lang.Integer,java.util.Set<AbstractCheck>> tokenToCommentChecks
Maps from token name to comment checks.
-
ordinaryChecks
private final java.util.Set<AbstractCheck> ordinaryChecks
Registered ordinary checks, that don't use comment nodes.
-
commentChecks
private final java.util.Set<AbstractCheck> commentChecks
Registered comment checks.
-
filters
private final java.util.Set<TreeWalkerFilter> filters
The ast filters.
-
violations
private final java.util.SortedSet<Violation> violations
The sorted set of violations.
-
childContext
private Context childContext
Context of child components.
-
moduleFactory
private ModuleFactory moduleFactory
A factory for creating submodules (i.e. the Checks)
-
-
Constructor Detail
-
TreeWalker
public TreeWalker()
Creates a newTreeWalkerinstance.
-
-
Method Detail
-
setModuleFactory
public void setModuleFactory(ModuleFactory moduleFactory)
Sets the module factory for creating child modules (Checks).- Parameters:
moduleFactory- the factory
-
finishLocalSetup
public void finishLocalSetup()
Description copied from class:AutomaticBeanProvides a hook to finish the part of this component's setup that was not handled by the bean introspection.The default implementation does nothing.
- Overrides:
finishLocalSetupin classAbstractViolationReporter
-
setupChild
public void setupChild(Configuration childConf) throws CheckstyleException
Called by configure() for every child of this component's Configuration.The default implementation throws
Creates child module.CheckstyleExceptionifchildConfisnullbecause it doesn't support children. It must be overridden to validate and support children that are wanted.- Overrides:
setupChildin classAutomaticBean- Parameters:
childConf- a child of this component's Configuration- Throws:
CheckstyleException- if there is a configuration error.- See Also:
Configuration.getChildren()
-
processFiltered
protected void processFiltered(java.io.File file, FileText fileText) throws CheckstyleException
Description copied from class:AbstractFileSetCheckCalled to process a file that matches the specified file extensions.- Specified by:
processFilteredin classAbstractFileSetCheck- Parameters:
file- the file to be processedfileText- the contents of the file.- Throws:
CheckstyleException- if error condition within Checkstyle occurs.
-
getFilteredViolations
private java.util.SortedSet<Violation> getFilteredViolations(java.lang.String fileName, FileContents fileContents, DetailAST rootAST)
Returns filtered set ofViolation.- Parameters:
fileName- path to the filefileContents- the contents of the filerootAST- root AST elementDetailASTof the file- Returns:
- filtered set of violations
-
registerCheck
private void registerCheck(AbstractCheck check) throws CheckstyleException
Register a check for a given configuration.- Parameters:
check- the check to register- Throws:
CheckstyleException- if an error occurs
-
registerCheck
private void registerCheck(int tokenId, AbstractCheck check) throws CheckstyleException
Register a check for a specified token id.- Parameters:
tokenId- the id of the tokencheck- the check to register- Throws:
CheckstyleException- if Check is misconfigured
-
walk
private void walk(DetailAST ast, FileContents contents, TreeWalker.AstState astState)
Initiates the walk of an AST.- Parameters:
ast- the root ASTcontents- the contents of the file the AST was generated from.astState- state of AST.
-
notifyBegin
private void notifyBegin(DetailAST rootAST, FileContents contents, TreeWalker.AstState astState)
Notify checks that we are about to begin walking a tree.- Parameters:
rootAST- the root of the tree.contents- the contents of the file the AST was generated from.astState- state of AST.
-
notifyEnd
private void notifyEnd(DetailAST rootAST, TreeWalker.AstState astState)
Notify checks that we have finished walking a tree.- Parameters:
rootAST- the root of the tree.astState- state of AST.
-
notifyVisit
private void notifyVisit(DetailAST ast, TreeWalker.AstState astState)
Notify checks that visiting a node.- Parameters:
ast- the node to notify for.astState- state of AST.
-
notifyLeave
private void notifyLeave(DetailAST ast, TreeWalker.AstState astState)
Notify checks that leaving a node.- Parameters:
ast- the node to notify forastState- state of AST.
-
getListOfChecks
private java.util.Collection<AbstractCheck> getListOfChecks(DetailAST ast, TreeWalker.AstState astState)
Method returns list of checks.- Parameters:
ast- the node to notify forastState- state of AST.- Returns:
- list of visitors
-
destroy
public void destroy()
Description copied from interface:FileSetCheckCleans up the object.- Specified by:
destroyin interfaceFileSetCheck- Overrides:
destroyin classAbstractFileSetCheck
-
getExternalResourceLocations
public java.util.Set<java.lang.String> getExternalResourceLocations()
Description copied from interface:ExternalResourceHolderReturns a set of external configuration resource locations which are used by the module. ATTENTION! If 'getExternalResourceLocations()' return null, there will beNullPointerExceptioninChecker. Such behaviour will signal that your module (check or filter) is designed incorrectly. It make sense to return an empty set from 'getExternalResourceLocations()' only for composite modules likeTreeWalker.- Specified by:
getExternalResourceLocationsin interfaceExternalResourceHolder- Returns:
- a set of external configuration resource locations which are used by the module.
-
processIter
private void processIter(DetailAST root, TreeWalker.AstState astState)
Processes a node calling interested checks at each node. Uses iterative algorithm.- Parameters:
root- the root of tree for processastState- state of AST.
-
createNewCheckSortedSet
private static java.util.SortedSet<AbstractCheck> createNewCheckSortedSet()
Creates a newSortedSetwith a deterministic order based on the Check's name before the default ordering.- Returns:
- The new
SortedSet.
-
-