|
1 |
| package edu.rice.cs.dynamicjava.interpreter; |
|
2 |
| |
|
3 |
| import java.io.PrintWriter; |
|
4 |
| |
|
5 |
| import static edu.rice.cs.plt.debug.DebugUtil.debug; |
|
6 |
| |
|
7 |
| public class EvaluatorException extends InterpreterException { |
|
8 |
| |
|
9 |
0
| public EvaluatorException(Throwable cause) {
|
|
10 |
0
| super(cause);
|
|
11 |
0
| updateAllStacks(cause, new String[0][]);
|
|
12 |
| } |
|
13 |
| |
|
14 |
0
| public EvaluatorException(Throwable cause, String... extraStackElements) {
|
|
15 |
0
| super(cause);
|
|
16 |
0
| updateAllStacks(cause, new String[][]{ extraStackElements });
|
|
17 |
| } |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
0
| public EvaluatorException(Throwable cause, String[]... extraStackElements) {
|
|
36 |
0
| super(cause);
|
|
37 |
0
| updateAllStacks(cause, extraStackElements);
|
|
38 |
| } |
|
39 |
| |
|
40 |
| |
|
41 |
0
| private void updateAllStacks(Throwable cause, String[][]extraStack) {
|
|
42 |
0
| StackTraceElement[] current = new Throwable().getStackTrace();
|
|
43 |
0
| while (cause != null) {
|
|
44 |
0
| updateStack(cause, current, extraStack);
|
|
45 |
0
| cause = cause.getCause();
|
|
46 |
| } |
|
47 |
| } |
|
48 |
| |
|
49 |
| |
|
50 |
0
| private void updateStack(Throwable cause, StackTraceElement[] current, String[][] extraStack) {
|
|
51 |
0
| StackTraceElement[] stack = cause.getStackTrace();
|
|
52 |
0
| int offset = stack.length - current.length;
|
|
53 |
0
| int minMatch = stack.length;
|
|
54 |
0
| boolean allMatch = true;
|
|
55 |
| |
|
56 |
0
| while (minMatch-1 >= 0 && minMatch-1-offset >= 2) {
|
|
57 |
0
| StackTraceElement stackElt = stack[minMatch-1];
|
|
58 |
0
| StackTraceElement currentElt = current[minMatch-1-offset];
|
|
59 |
0
| if (stackElt.getClassName().equals(currentElt.getClassName()) &&
|
|
60 |
| stackElt.getMethodName().equals(currentElt.getMethodName())) { |
|
61 |
0
| minMatch--;
|
|
62 |
| } |
|
63 |
0
| else { allMatch = false; break; }
|
|
64 |
| } |
|
65 |
0
| if (allMatch && minMatch > 0) {
|
|
66 |
0
| int bestExtraMatch = 0;
|
|
67 |
0
| boolean bestExtraMatchesAll = true;
|
|
68 |
0
| for (String[] extras : extraStack) {
|
|
69 |
0
| int extraMatch = 0;
|
|
70 |
0
| boolean extraMatchesAll = true;
|
|
71 |
0
| while (extraMatch < extras.length && minMatch-extraMatch-1 >= 0) {
|
|
72 |
0
| StackTraceElement stackElt = stack[minMatch-extraMatch-1];
|
|
73 |
0
| if (extras[extraMatch].equals(stackElt.getClassName() + "." + stackElt.getMethodName())) {
|
|
74 |
0
| extraMatch++;
|
|
75 |
| } |
|
76 |
0
| else { extraMatchesAll = false; break; }
|
|
77 |
| } |
|
78 |
0
| if (extraMatch > bestExtraMatch) {
|
|
79 |
0
| bestExtraMatch = extraMatch;
|
|
80 |
0
| bestExtraMatchesAll = extraMatchesAll;
|
|
81 |
| } |
|
82 |
0
| else if (extraMatch == bestExtraMatch) {
|
|
83 |
0
| bestExtraMatchesAll |= extraMatchesAll;
|
|
84 |
| } |
|
85 |
| } |
|
86 |
0
| minMatch -= bestExtraMatch;
|
|
87 |
0
| allMatch &= bestExtraMatchesAll;
|
|
88 |
| } |
|
89 |
| |
|
90 |
0
| if (!allMatch) { debug.log("Stack has unmatched elements"); }
|
|
91 |
0
| if (minMatch < stack.length) {
|
|
92 |
0
| StackTraceElement[] newStack = new StackTraceElement[minMatch];
|
|
93 |
0
| System.arraycopy(stack, 0, newStack, 0, minMatch);
|
|
94 |
0
| cause.setStackTrace(newStack);
|
|
95 |
| } |
|
96 |
| } |
|
97 |
| |
|
98 |
0
| public void printUserMessage(PrintWriter out) { getCause().printStackTrace(out); }
|
|
99 |
| |
|
100 |
| } |