Class TreeWalker

    • Field Detail

      • DEFAULT_TAB_WIDTH

        private static final int DEFAULT_TAB_WIDTH
        Default distance between tab stops.
        See Also:
        Constant Field Values
      • tokenToOrdinaryChecks

        private final com.google.common.collect.Multimap<java.lang.String,​AbstractCheck> tokenToOrdinaryChecks
        Maps from token name to ordinary checks.
      • tokenToCommentChecks

        private final com.google.common.collect.Multimap<java.lang.String,​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.
      • tabWidth

        private int tabWidth
        The distance between tab stops.
      • classLoader

        private java.lang.ClassLoader classLoader
        Class loader to resolve classes with.
      • 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 new TreeWalker instance.
    • Method Detail

      • setTabWidth

        public void setTabWidth​(int tabWidth)
        Sets tab width.
        Parameters:
        tabWidth - the distance between tab stops
      • setCacheFile

        @Deprecated
        public 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
        Sets cache file.
        Parameters:
        fileName - the cache file
      • setClassLoader

        public void setClassLoader​(java.lang.ClassLoader classLoader)
        Sets classLoader to load class.
        Parameters:
        classLoader - class loader to resolve classes with.
      • 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: AutomaticBean
        Provides 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:
        finishLocalSetup in class AutomaticBean
      • processFiltered

        protected void processFiltered​(java.io.File file,
                                       java.util.List<java.lang.String> lines)
                                throws CheckstyleException
        Description copied from class: AbstractFileSetCheck
        Called to process a file that matches the specified file extensions.
        Specified by:
        processFiltered in class AbstractFileSetCheck
        Parameters:
        file - the file to be processed
        lines - an immutable list of the contents of the file.
        Throws:
        CheckstyleException - if error condition within Checkstyle 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 token
        check - the check to register
        Throws:
        CheckstyleException - if Check is misconfigured
      • registerCheck

        private void registerCheck​(java.lang.String token,
                                   AbstractCheck check)
                            throws CheckstyleException
        Register a check for a specified token name.
        Parameters:
        token - the name of the token
        check - the check to register
        Throws:
        CheckstyleException - if Check is misconfigured
      • validateDefaultTokens

        private static void validateDefaultTokens​(AbstractCheck check)
                                           throws CheckstyleException
        Validates that check's required tokens are subset of default tokens.
        Parameters:
        check - to validate
        Throws:
        CheckstyleException - when validation of default tokens fails
      • walk

        private void walk​(DetailAST ast,
                          FileContents contents,
                          TreeWalker.AstState astState)
        Initiates the walk of an AST.
        Parameters:
        ast - the root AST
        contents - 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 for
        astState - 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 for
        astState - state of AST.
        Returns:
        list of visitors
      • parse

        public static DetailAST parse​(FileContents contents)
                               throws antlr.RecognitionException,
                                      antlr.TokenStreamException
        Static helper method to parses a Java source file.
        Parameters:
        contents - contains the contents of the file
        Returns:
        the root of the AST
        Throws:
        antlr.TokenStreamException - if lexing failed
        antlr.RecognitionException - if parsing failed
      • parseWithComments

        public static DetailAST parseWithComments​(FileContents contents)
                                           throws antlr.RecognitionException,
                                                  antlr.TokenStreamException
        Parses Java source file. Result AST contains comment nodes.
        Parameters:
        contents - source file content
        Returns:
        DetailAST tree
        Throws:
        antlr.RecognitionException - if parser failed
        antlr.TokenStreamException - if lexer failed
      • getExternalResourceLocations

        public java.util.Set<java.lang.String> getExternalResourceLocations()
        Description copied from interface: ExternalResourceHolder
        Returns a set of external configuration resource locations which are used by the module. ATTENTION! If 'getExternalResourceLocations()' return null, there will be 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.
        Specified by:
        getExternalResourceLocations in interface ExternalResourceHolder
        Returns:
        a set of external configuration resource locations which are used by the module.
      • getExternalResourceLocations

        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.
        Parameters:
        checks - a set of checks.
        Returns:
        a set of external configuration resource locations which are used by the checks set.
      • 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 process
        astState - state of AST.
      • appendHiddenCommentNodes

        private static DetailAST appendHiddenCommentNodes​(DetailAST root)
        Appends comment nodes to existing AST. It traverses each node in AST, looks for hidden comment tokens and appends found comment tokens as nodes in AST.
        Parameters:
        root - root of AST.
        Returns:
        root of AST with comment nodes.
      • isPositionGreater

        private static boolean isPositionGreater​(DetailAST ast1,
                                                 DetailAST ast2)
        Checks if position of first DetailAST is greater than position of second DetailAST. Position is line number and column number in source file.
        Parameters:
        ast1 - first DetailAST node.
        ast2 - second DetailAST node.
        Returns:
        true if position of ast1 is greater than position of ast2.
      • createCommentAstFromToken

        private static DetailAST createCommentAstFromToken​(antlr.Token token)
        Create comment AST from token. Depending on token type SINGLE_LINE_COMMENT or BLOCK_COMMENT_BEGIN is created.
        Parameters:
        token - Token object.
        Returns:
        DetailAST of comment node.
      • createSlCommentNode

        private static DetailAST createSlCommentNode​(antlr.Token token)
        Create single-line comment from token.
        Parameters:
        token - Token object.
        Returns:
        DetailAST with SINGLE_LINE_COMMENT type.
      • createBlockCommentNode

        private static DetailAST createBlockCommentNode​(antlr.Token token)
        Create block comment from token.
        Parameters:
        token - Token object.
        Returns:
        DetailAST with BLOCK_COMMENT type.
      • countLinesColumns

        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.
        Parameters:
        text - String.
        initialLinesCnt - initial value of lines counter.
        initialColumnsCnt - initial value of columns counter.
        Returns:
        entry(pair), first element is lines counter, second - columns counter.