Class 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.
  • Field Details

    • PARSE_EXCEPTION_MSG

      public static final String PARSE_EXCEPTION_MSG
      Message to use when an exception occurs and should be printed as a violation.
      See Also:
    • tokenToOrdinaryChecks

      private final Map<Integer, Set<AbstractCheck>> tokenToOrdinaryChecks
      Maps from token name to ordinary checks.
    • tokenToCommentChecks

      private final Map<Integer, Set<AbstractCheck>> tokenToCommentChecks
      Maps from token name to comment checks.
    • ordinaryChecks

      private final Set<AbstractCheck> ordinaryChecks
      Registered ordinary checks, that don't use comment nodes.
    • commentChecks

      private final Set<AbstractCheck> commentChecks
      Registered comment checks.
    • filters

      private final Set<TreeWalkerFilter> filters
      The ast filters.
    • violations

      private final 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)
    • skipFileOnJavaParseException

      private boolean skipFileOnJavaParseException
      Control whether to skip files with Java parsing exceptions.
    • javaParseExceptionSeverity

      private SeverityLevel javaParseExceptionSeverity
      Specify severity Level to log Java parsing exceptions when they are skipped.
  • Constructor Details

    • TreeWalker

      public TreeWalker()
      Creates a new TreeWalker instance.
  • Method Details

    • setModuleFactory

      public void setModuleFactory(ModuleFactory moduleFactory)
      Sets the module factory for creating child modules (Checks).
      Parameters:
      moduleFactory - the factory
    • setSkipFileOnJavaParseException

      public void setSkipFileOnJavaParseException(boolean skipFileOnJavaParseException)
      Setter to control whether to skip files with Java parsing exceptions.
      Parameters:
      skipFileOnJavaParseException - whether to skip files with Java parsing errors.
      Since:
      10.18.0
    • setJavaParseExceptionSeverity

      public void setJavaParseExceptionSeverity(SeverityLevel javaParseExceptionSeverity)
      Setter to specify the severity level to log Java parsing exceptions when they are skipped.
      Parameters:
      javaParseExceptionSeverity - severity level to log parsing exceptions when they are skipped.
      Since:
      10.18.0
    • finishLocalSetup

      public void finishLocalSetup()
      Description copied from class: AbstractAutomaticBean
      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 AbstractViolationReporter
    • setupChild

      public void setupChild(Configuration childConf) throws CheckstyleException
      Called by configure() for every child of this component's Configuration.

      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.

      Creates child module.
      Overrides:
      setupChild in class AbstractAutomaticBean
      Parameters:
      childConf - a child of this component's Configuration
      Throws:
      CheckstyleException - if there is a configuration error.
      See Also:
    • processFiltered

      protected void processFiltered(File file, FileText fileText) throws CheckstyleException
      Called to process a file that matches the specified file extensions. Processes the file.
      Specified by:
      processFiltered in class AbstractFileSetCheck
      Parameters:
      file - the file to be processed
      fileText - the contents of the file.
      Throws:
      CheckstyleException - if error condition within Checkstyle occurs.
    • getFilteredViolations

      private SortedSet<Violation> getFilteredViolations(String fileName, FileContents fileContents, DetailAST rootAST)
      Returns filtered set of Violation.
      Parameters:
      fileName - path to the file
      fileContents - the contents of the file
      rootAST - root AST element DetailAST of 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 token
      check - 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 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 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
    • destroy

      public void destroy()
      Description copied from interface: FileSetCheck
      Cleans up the object.
      Specified by:
      destroy in interface FileSetCheck
      Overrides:
      destroy in class AbstractFileSetCheck
    • getExternalResourceLocations

      public Set<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 makes 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.
    • 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.
    • createNewCheckSortedSet

      private static SortedSet<AbstractCheck> createNewCheckSortedSet()
      Creates a new SortedSet with a deterministic order based on the Check's name before the default ordering.
      Returns:
      The new SortedSet.