|
1 |
| package edu.rice.cs.dynamicjava.interpreter; |
|
2 |
| |
|
3 |
| import java.io.PrintWriter; |
|
4 |
| import java.util.ArrayList; |
|
5 |
| import java.util.List; |
|
6 |
| |
|
7 |
| import koala.dynamicjava.tree.SourceInfo; |
|
8 |
| |
|
9 |
| import edu.rice.cs.plt.collect.CollectUtil; |
|
10 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
11 |
| import edu.rice.cs.plt.iter.SizedIterable; |
|
12 |
| |
|
13 |
| public class CompositeException extends InterpreterException { |
|
14 |
| |
|
15 |
| private final SizedIterable<InterpreterException> _exceptions; |
|
16 |
| |
|
17 |
0
| public CompositeException(Iterable<? extends InterpreterException> exceptions) {
|
|
18 |
0
| super(extractCause(exceptions));
|
|
19 |
0
| _exceptions = IterUtil.snapshot(exceptions);
|
|
20 |
| } |
|
21 |
| |
|
22 |
0
| private static InterpreterException extractCause(Iterable<? extends InterpreterException> exceptions) {
|
|
23 |
0
| return IterUtil.isEmpty(exceptions) ? null : IterUtil.first(exceptions);
|
|
24 |
| } |
|
25 |
| |
|
26 |
0
| public void printUserMessage(PrintWriter out) {
|
|
27 |
0
| out.println(_exceptions.size() + " errors:");
|
|
28 |
0
| for (InterpreterException e : _exceptions) {
|
|
29 |
0
| out.println();
|
|
30 |
0
| if (e instanceof SourceInfo.Wrapper) { out.println(((SourceInfo.Wrapper) e).getSourceInfo()); }
|
|
31 |
0
| else { out.println("[Unknown location]"); }
|
|
32 |
0
| e.printUserMessage(out);
|
|
33 |
| } |
|
34 |
| } |
|
35 |
| |
|
36 |
0
| public SizedIterable<InterpreterException> exceptions() { return _exceptions; }
|
|
37 |
| |
|
38 |
0
| public static InterpreterException make(Iterable<? extends InterpreterException> errors) {
|
|
39 |
0
| List<InterpreterException> normalized = new ArrayList<InterpreterException>();
|
|
40 |
0
| for (InterpreterException e : errors) {
|
|
41 |
0
| if (e instanceof CompositeException) {
|
|
42 |
0
| CollectUtil.addAll(normalized, ((CompositeException) e).exceptions());
|
|
43 |
| } |
|
44 |
0
| else { normalized.add(e); }
|
|
45 |
| } |
|
46 |
0
| return new CompositeException(normalized);
|
|
47 |
| } |
|
48 |
| |
|
49 |
| } |