public final class TreeWalker extends AbstractFileSetCheck implements ExternalResourceHolder
Modifier and Type | Class and Description |
---|---|
private static class |
TreeWalker.AstState
State of AST.
|
Modifier and Type | Field and Description |
---|---|
private Context |
childContext
Context of child components.
|
private java.lang.ClassLoader |
classLoader
Class loader to resolve classes with.
|
private java.util.Set<AbstractCheck> |
commentChecks
Registered comment checks.
|
private static int |
DEFAULT_TAB_WIDTH
Default distance between tab stops.
|
private ModuleFactory |
moduleFactory
A factory for creating submodules (i.e.
|
private java.util.Set<AbstractCheck> |
ordinaryChecks
Registered ordinary checks, that don't use comment nodes.
|
private int |
tabWidth
The distance between tab stops.
|
private com.google.common.collect.Multimap<java.lang.String,AbstractCheck> |
tokenToCommentChecks
Maps from token name to comment checks.
|
private com.google.common.collect.Multimap<java.lang.String,AbstractCheck> |
tokenToOrdinaryChecks
Maps from token name to ordinary checks.
|
Constructor and Description |
---|
TreeWalker()
Creates a new
TreeWalker instance. |
Modifier and Type | Method and Description |
---|---|
private static DetailAST |
appendHiddenCommentNodes(DetailAST root)
Appends comment nodes to existing AST.
|
private static java.util.Map.Entry<java.lang.Integer,java.lang.Integer> |
countLinesColumns(java.lang.String text,
int initialLinesCnt,
int initialColumnsCnt)
Count lines and columns (in last line) in text.
|
private static DetailAST |
createBlockCommentNode(antlr.Token token)
Create block comment from token.
|
private static DetailAST |
createCommentAstFromToken(antlr.Token token)
Create comment AST from token.
|
private static DetailAST |
createSlCommentNode(antlr.Token token)
Create single-line comment from token.
|
void |
destroy()
Cleans up the object.
|
void |
finishLocalSetup()
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 static java.util.Set<java.lang.String> |
getExternalResourceLocations(java.util.Set<AbstractCheck> checks)
Returns a set of external configuration resource locations which are used by the checks set.
|
private java.util.Collection<AbstractCheck> |
getListOfChecks(DetailAST ast,
TreeWalker.AstState astState)
Method returns list of checks.
|
private static boolean |
isPositionGreater(DetailAST ast1,
DetailAST ast2)
Checks if position of first DetailAST is greater than position of
second DetailAST.
|
private void |
notifyBegin(DetailAST rootAST,
FileContents contents,
TreeWalker.AstState astState)
Notify checks that we are about to begin walking a tree.
|
private void |
notifyEnd(DetailAST rootAST,
TreeWalker.AstState astState)
Notify checks that we have finished walking a tree.
|
private void |
notifyLeave(DetailAST ast,
TreeWalker.AstState astState)
Notify checks that leaving a node.
|
private void |
notifyVisit(DetailAST ast,
TreeWalker.AstState astState)
Notify checks that visiting a node.
|
static DetailAST |
parse(FileContents contents)
Static helper method to parses a Java source file.
|
static DetailAST |
parseWithComments(FileContents contents)
Parses Java source file.
|
protected void |
processFiltered(java.io.File file,
java.util.List<java.lang.String> lines)
Called to process a file that matches the specified file extensions.
|
private void |
processIter(DetailAST root,
TreeWalker.AstState astState)
Processes a node calling interested checks at each node.
|
private void |
registerCheck(AbstractCheck check)
Register a check for a given configuration.
|
private void |
registerCheck(int tokenId,
AbstractCheck check)
Register a check for a specified token id.
|
private void |
registerCheck(java.lang.String token,
AbstractCheck check)
Register a check for a specified token name.
|
void |
setCacheFile(java.lang.String fileName)
Deprecated.
Use
Checker.setCacheFile(java.lang.String) instead. It does not do anything now. We just
keep the setter for transition period to the same option in Checker. The
method will be completely removed in Checkstyle 8.0. See
issue#2883 |
void |
setClassLoader(java.lang.ClassLoader classLoader)
Sets classLoader to load class.
|
void |
setModuleFactory(ModuleFactory moduleFactory)
Sets the module factory for creating child modules (Checks).
|
void |
setTabWidth(int tabWidth)
Sets tab width.
|
void |
setupChild(Configuration childConf)
Called by configure() for every child of this component's Configuration.
|
private static void |
validateDefaultTokens(AbstractCheck check)
Validates that check's required tokens are subset of default tokens.
|
private void |
walk(DetailAST ast,
FileContents contents,
TreeWalker.AstState astState)
Initiates the walk of an AST.
|
beginProcessing, finishProcessing, fireErrors, getFileExtensions, getMessageCollector, getMessageDispatcher, init, log, log, process, setFileExtensions, setMessageDispatcher
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, getConfiguration
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
configure
contextualize
private static final int DEFAULT_TAB_WIDTH
private final com.google.common.collect.Multimap<java.lang.String,AbstractCheck> tokenToOrdinaryChecks
private final com.google.common.collect.Multimap<java.lang.String,AbstractCheck> tokenToCommentChecks
private final java.util.Set<AbstractCheck> ordinaryChecks
private final java.util.Set<AbstractCheck> commentChecks
private int tabWidth
private java.lang.ClassLoader classLoader
private Context childContext
private ModuleFactory moduleFactory
public void setTabWidth(int tabWidth)
tabWidth
- the distance between tab stops@Deprecated public void setCacheFile(java.lang.String fileName)
Checker.setCacheFile(java.lang.String)
instead. It does not do anything now. We just
keep the setter for transition period to the same option in Checker. The
method will be completely removed in Checkstyle 8.0. See
issue#2883fileName
- the cache filepublic void setClassLoader(java.lang.ClassLoader classLoader)
classLoader
- class loader to resolve classes with.public void setModuleFactory(ModuleFactory moduleFactory)
moduleFactory
- the factorypublic void finishLocalSetup()
AutomaticBean
The default implementation does nothing.
finishLocalSetup
in class AutomaticBean
public void setupChild(Configuration childConf) throws CheckstyleException
AutomaticBean
The default implementation throws CheckstyleException
if
childConf
is null
because it doesn't support children. It
must be overridden to validate and support children that are wanted.
setupChild
in class AutomaticBean
childConf
- a child of this component's ConfigurationCheckstyleException
- if there is a configuration error.Configuration.getChildren()
protected void processFiltered(java.io.File file, java.util.List<java.lang.String> lines) throws CheckstyleException
AbstractFileSetCheck
processFiltered
in class AbstractFileSetCheck
file
- the file to be processedlines
- an immutable list of the contents of the file.CheckstyleException
- if error condition within Checkstyle occurs.private void registerCheck(AbstractCheck check) throws CheckstyleException
check
- the check to registerCheckstyleException
- if an error occursprivate void registerCheck(int tokenId, AbstractCheck check) throws CheckstyleException
tokenId
- the id of the tokencheck
- the check to registerCheckstyleException
- if Check is misconfiguredprivate void registerCheck(java.lang.String token, AbstractCheck check) throws CheckstyleException
token
- the name of the tokencheck
- the check to registerCheckstyleException
- if Check is misconfiguredprivate static void validateDefaultTokens(AbstractCheck check) throws CheckstyleException
check
- to validateCheckstyleException
- when validation of default tokens failsprivate void walk(DetailAST ast, FileContents contents, TreeWalker.AstState astState)
ast
- the root ASTcontents
- the contents of the file the AST was generated from.astState
- state of AST.private void notifyBegin(DetailAST rootAST, FileContents contents, TreeWalker.AstState astState)
rootAST
- the root of the tree.contents
- the contents of the file the AST was generated from.astState
- state of AST.private void notifyEnd(DetailAST rootAST, TreeWalker.AstState astState)
rootAST
- the root of the tree.astState
- state of AST.private void notifyVisit(DetailAST ast, TreeWalker.AstState astState)
ast
- the node to notify for.astState
- state of AST.private void notifyLeave(DetailAST ast, TreeWalker.AstState astState)
ast
- the node to notify forastState
- state of AST.private java.util.Collection<AbstractCheck> getListOfChecks(DetailAST ast, TreeWalker.AstState astState)
ast
- the node to notify forastState
- state of AST.public static DetailAST parse(FileContents contents) throws antlr.RecognitionException, antlr.TokenStreamException
contents
- contains the contents of the fileantlr.TokenStreamException
- if lexing failedantlr.RecognitionException
- if parsing failedpublic static DetailAST parseWithComments(FileContents contents) throws antlr.RecognitionException, antlr.TokenStreamException
contents
- source file contentantlr.RecognitionException
- if parser failedantlr.TokenStreamException
- if lexer failedpublic void destroy()
FileSetCheck
destroy
in interface FileSetCheck
destroy
in class AbstractFileSetCheck
public java.util.Set<java.lang.String> getExternalResourceLocations()
ExternalResourceHolder
NullPointerException
in Checker
.
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 like TreeWalker
.getExternalResourceLocations
in interface ExternalResourceHolder
private static java.util.Set<java.lang.String> getExternalResourceLocations(java.util.Set<AbstractCheck> checks)
checks
- a set of checks.private void processIter(DetailAST root, TreeWalker.AstState astState)
root
- the root of tree for processastState
- state of AST.private static DetailAST appendHiddenCommentNodes(DetailAST root)
root
- root of AST.private static boolean isPositionGreater(DetailAST ast1, DetailAST ast2)
ast1
- first DetailAST node.ast2
- second DetailAST node.private static DetailAST createCommentAstFromToken(antlr.Token token)
token
- Token object.private static DetailAST createSlCommentNode(antlr.Token token)
token
- Token object.private static DetailAST createBlockCommentNode(antlr.Token token)
token
- Token object.private static java.util.Map.Entry<java.lang.Integer,java.lang.Integer> countLinesColumns(java.lang.String text, int initialLinesCnt, int initialColumnsCnt)
text
- String.initialLinesCnt
- initial value of lines counter.initialColumnsCnt
- initial value of columns counter.