1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 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 java.util.Comparator; |
25 | import java.util.Optional; |
26 | |
27 | import static com.github.javaparser.utils.Utils.SYSTEM_EOL; |
28 | import static com.github.javaparser.utils.Utils.assertNotNull; |
29 | |
30 | /** |
31 | * A problem that was encountered during parsing. |
32 | */ |
33 | public class Problem { |
34 | private final String message; |
35 | private final TokenRange location; |
36 | private final Throwable cause; |
37 | |
38 | public Problem(String message, TokenRange location, Throwable cause) { |
39 | assertNotNull(message); |
40 | |
41 | this.message = message; |
42 | this.location = location; |
43 | this.cause = cause; |
44 | } |
45 | |
46 | @Override |
47 | public String toString() { |
48 | final StringBuilder str = new StringBuilder(getVerboseMessage()); |
49 | if (cause != null) { |
50 | str.append(SYSTEM_EOL).append("Problem stacktrace : ").append(SYSTEM_EOL); |
51 | for (int i = 0; i < cause.getStackTrace().length; i++) { |
52 | StackTraceElement ste = cause.getStackTrace()[i]; |
53 | str.append(" ").append(ste.toString()); |
54 | if (i + 1 != cause.getStackTrace().length) |
55 | str.append(SYSTEM_EOL); |
56 | } |
57 | } |
58 | return str.toString(); |
59 | } |
60 | |
61 | /** |
62 | * @return the message that was passed into the constructor. |
63 | */ |
64 | public String getMessage() { |
65 | return message; |
66 | } |
67 | |
68 | /** |
69 | * @return the message plus location information. |
70 | */ |
71 | public String getVerboseMessage() { |
72 | return getLocation().map(l -> l.getBegin().getRange().map(r -> r.begin.toString()).orElse("(line ?,col ?)") + " " + message).orElse(message); |
73 | } |
74 | |
75 | /** |
76 | * @return the location that was passed into the constructor. |
77 | */ |
78 | public Optional<TokenRange> getLocation() { |
79 | return Optional.ofNullable(location); |
80 | } |
81 | |
82 | /** |
83 | * @return the cause that was passed into the constructor. |
84 | */ |
85 | public Optional<Throwable> getCause() { |
86 | return Optional.ofNullable(cause); |
87 | } |
88 | |
89 | /** |
90 | * Sorts problems on position. |
91 | */ |
92 | public static Comparator<Problem> PROBLEM_BY_BEGIN_POSITION = (a, b) -> { |
93 | final Optional<Position> aBegin= a.getLocation().flatMap(l -> l.getBegin().getRange().map(r -> r.begin)); |
94 | final Optional<Position> bBegin = b.getLocation().flatMap(l -> l.getBegin().getRange().map(r -> r.begin)); |
95 | |
96 | if (aBegin.isPresent() && bBegin.isPresent()) { |
97 | return aBegin.get().compareTo(bBegin.get()); |
98 | } |
99 | if (a.getLocation().isPresent() || b.getLocation().isPresent()) { |
100 | if (a.getLocation().isPresent()) { |
101 | return 1; |
102 | } |
103 | return -1; |
104 | } |
105 | return 0; |
106 | }; |
107 | |
108 | |
109 | } |
110 |
Members