Class JavaParser


  • public final class JavaParser
    extends java.lang.Object
    Helper methods to parse java source files.
    • Constructor Detail

      • JavaParser

        private JavaParser()
        Stop instances being created.
    • Method Detail

      • parse

        public static DetailAST parse​(FileContents contents)
                               throws CheckstyleException
        Static helper method to parses a Java source file.
        Parameters:
        contents - contains the contents of the file
        Returns:
        the root of the AST
        Throws:
        CheckstyleException - if the contents is not a valid Java source
      • parseFile

        public static DetailAST parseFile​(java.io.File file,
                                          JavaParser.Options options)
                                   throws java.io.IOException,
                                          CheckstyleException
        Parses Java source file.
        Parameters:
        file - the file to parse
        options - JavaParser.Options to control inclusion of comment nodes
        Returns:
        DetailAST tree
        Throws:
        java.io.IOException - if the file could not be read
        CheckstyleException - if the file is not a valid Java source file
      • appendHiddenCommentNodes

        public 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 - of AST
        Returns:
        root of AST with comment nodes
      • createCommentAstFromToken

        private static DetailAST createCommentAstFromToken​(org.antlr.v4.runtime.CommonToken token)
        Create comment AST from token. Depending on token type SINGLE_LINE_COMMENT or BLOCK_COMMENT_BEGIN is created.
        Parameters:
        token - to create the AST
        Returns:
        DetailAST of comment node
      • createSlCommentNode

        private static DetailAST createSlCommentNode​(org.antlr.v4.runtime.CommonToken token)
        Create single-line comment from token.
        Parameters:
        token - to create the AST
        Returns:
        DetailAST with SINGLE_LINE_COMMENT type