Class JavaParser


  • public final class JavaParser
    extends Object
    Entry point for parsing Java source into the source AST, holding the error listener that decides whether parse errors throw or are collected.
    • Method Detail

      • getErrorListener

        public ParseErrorListener getErrorListener()
        Returns:
        the error listener
      • create

        public static JavaParser create()
        Returns:
        a parser that throws on the first parse error
      • withErrorListener

        public static JavaParser withErrorListener​(ParseErrorListener listener)
        Creates a parser that routes errors to a listener.
        Parameters:
        listener - the error sink
        Returns:
        the parser
      • parse

        public CompilationUnit parse​(String source)
        Parses a whole source file.
        Parameters:
        source - the file text
        Returns:
        the compilation unit
        Throws:
        ParseException - if the text is malformed and the listener throws
      • parse

        public CompilationUnit parse​(Reader reader)
                              throws IOException
        Reads a stream fully and parses it as a source file.
        Parameters:
        reader - the source text
        Returns:
        the compilation unit
        Throws:
        IOException - if reading fails
        ParseException - if the text is malformed and the listener throws
      • parseFile

        public CompilationUnit parseFile​(Path path)
                                  throws IOException
        Reads a UTF-8 file and parses it as a source file.
        Parameters:
        path - the file to read
        Returns:
        the compilation unit
        Throws:
        IOException - if the file cannot be read
        ParseException - if the text is malformed and the listener throws
      • parseFile

        public CompilationUnit parseFile​(File file)
                                  throws IOException
        Reads a UTF-8 file and parses it as a source file.
        Parameters:
        file - the file to read
        Returns:
        the compilation unit
        Throws:
        IOException - if the file cannot be read
        ParseException - if the text is malformed and the listener throws
      • parseExpression

        public Expression parseExpression​(String source)
        Parses a single expression.
        Parameters:
        source - the expression text
        Returns:
        the expression node
        Throws:
        ParseException - if the text is malformed and the listener throws
      • parseStatement

        public Statement parseStatement​(String source)
        Parses a single statement.
        Parameters:
        source - the statement text
        Returns:
        the statement node
        Throws:
        ParseException - if the text is malformed and the listener throws
      • parseBlock

        public BlockStmt parseBlock​(String source)
        Parses a block, wrapping the text in braces first if it has none.
        Parameters:
        source - the block text, with or without enclosing braces
        Returns:
        the block node
        Throws:
        ParseException - if the text is malformed, or parses to something other than a block
      • parseType

        public SourceType parseType​(String source)
        Parses a type reference.
        Parameters:
        source - the type text
        Returns:
        the parsed type
        Throws:
        ParseException - if the text is malformed and the listener throws
      • withListener

        public JavaParser withListener​(ParseErrorListener listener)
        Derives a parser using a different error sink.
        Parameters:
        listener - the error sink for the new parser
        Returns:
        the derived parser