• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
3  * Copyright (C) 2011, 2013-2016 The JavaParser Team.
4  *
5  * This file is part of JavaParser.
6  *
7  * JavaParser can be used either under the terms of
8  * a) the GNU Lesser General Public License as published by
9  *     the Free Software Foundation, either version 3 of the License, or
10  *     (at your option) any later version.
11  * b) the terms of the Apache License
12  *
13  * You should have received a copy of both licenses in LICENCE.LGPL and
14  * LICENCE.APACHE. Please refer to those files for details.
15  *
16  * JavaParser is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License for more details.
20  */
21 
22 package com.github.javaparser;
23 
24 import com.github.javaparser.ast.Node;
25 import com.github.javaparser.ast.comments.CommentsCollection;
26 
27 import java.util.List;
28 import java.util.Optional;
29 import java.util.function.Consumer;
30 
31 import static com.github.javaparser.utils.Utils.EOL;
32 
33 /**
34  * The results given when parsing with an instance of JavaParser.
35  */
36 public class ParseResult<T> {
37     private final T result;
38     private final List<Problem> problems;
39     private final CommentsCollection commentsCollection;
40 
41     /**
42      * General constructor.
43      *
44      * @param result the AST, or empty if it wasn't created.
45      * @param problems a list of encountered parsing problems.
46      */
ParseResult(T result, List<Problem> problems, CommentsCollection commentsCollection)47     public ParseResult(T result, List<Problem> problems, CommentsCollection commentsCollection) {
48         this.commentsCollection = commentsCollection;
49         this.result = result;
50         this.problems = problems;
51     }
52 
53     /**
54      * @return if parsing was successful, meaning no errors of any kind were encountered.
55      */
isSuccessful()56     public boolean isSuccessful() {
57         return problems.isEmpty() && result != null;
58     }
59 
60     /**
61      * Calls the consumer with the result if parsing was succesful.
62      */
ifSuccessful(Consumer<T> consumer)63     public void ifSuccessful(Consumer<T> consumer) {
64         if (isSuccessful()) {
65             consumer.accept(result);
66         }
67     }
68 
69     /**
70      * @return the list of encountered parsing problems. Empty when no problems were encountered.
71      */
getProblems()72     public List<Problem> getProblems() {
73         return problems;
74     }
75 
76     /**
77      * @return the <code>i</code>'th encountered parsing problem. May throw <code>IndexOutOfBoundsException</code>.
78      */
getProblem(int i)79     public Problem getProblem(int i) {
80         return getProblems().get(i);
81     }
82 
83     /**
84      * @return the complete collection of comments encountered while parsing.
85      */
getCommentsCollection()86     public Optional<CommentsCollection> getCommentsCollection() {
87         return Optional.ofNullable(commentsCollection);
88     }
89 
90     /**
91      * @return the AST of the parsed source code, or empty if parsing failed completely.
92      */
getResult()93     public Optional<T> getResult() {
94         return Optional.ofNullable(result);
95     }
96 
97     @Override
toString()98     public String toString() {
99         if (isSuccessful()) {
100             return "Parsing successful";
101         }
102         StringBuilder message = new StringBuilder("Parsing failed:").append(EOL);
103         for (Problem problem : problems) {
104             message.append(problem.toString()).append(EOL);
105         }
106         return message.toString();
107     }
108 
109     /**
110      * A post processor that can be added to ParserConfiguration to add some processing right after parsing.
111      */
112     public interface PostProcessor {
process(ParseResult<? extends Node> result, ParserConfiguration configuration)113         void process(ParseResult<? extends Node> result, ParserConfiguration configuration);
114     }
115 }
116