Class ParseResult<T>


  • public class ParseResult<T>
    extends java.lang.Object
    The results given when parsing with an instance of JavaParser.
    • Field Detail

      • result

        private final T result
      • problems

        private final java.util.List<Problem> problems
      • sourcePath

        private java.nio.file.Path sourcePath
        Optional source path associated with this parse operation. This may be set by higher-level utilities (e.g., SourceRoot) to allow callers to correlate parse problems with the originating file path even when no CompilationUnit is produced.

        Contract: - If present, it's expected to be an absolute Path to the source input used for this parse. - This value should be set immediately after parsing is performed and treated as effectively immutable thereafter.

    • Constructor Detail

      • ParseResult

        public ParseResult​(T result,
                           java.util.List<Problem> problems,
                           CommentsCollection commentsCollection)
        General constructor.
        Parameters:
        result - the AST, or empty if it wasn't created.
        problems - a list of encountered parsing problems.
    • Method Detail

      • isSuccessful

        public boolean isSuccessful()
        Returns:
        if parsing was successful, meaning no errors of any kind were encountered.
      • ifSuccessful

        public void ifSuccessful​(java.util.function.Consumer<T> consumer)
        Calls the consumer with the result if parsing was succesful.
      • setSourcePath

        public ParseResult<T> setSourcePath​(java.nio.file.Path sourcePath)
        Associates a source path with this parse result. Returns this for chaining.

        Notes: - Intended for use by infrastructure (e.g., SourceRoot) to publish the originating file path. - Should be called immediately after parsing and not modified afterwards to avoid surprises in concurrent contexts. - The provided path is expected to be absolute.

      • getProblems

        public java.util.List<Problem> getProblems()
        Returns:
        the list of encountered parsing problems. Empty when no problems were encountered.
      • getProblem

        public Problem getProblem​(int i)
        Returns:
        the i'th encountered parsing problem. May throw IndexOutOfBoundsException.
      • getCommentsCollection

        public java.util.Optional<CommentsCollection> getCommentsCollection()
        Returns:
        the complete collection of comments encountered while parsing.
      • getResult

        public java.util.Optional<T> getResult()
        Returns:
        the AST of the parsed source code, or empty if parsing failed completely.
      • getSourcePath

        public java.util.Optional<java.nio.file.Path> getSourcePath()
        Returns:
        the absolute path to the source file this parse result originates from, or empty if no file parsing.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object