|
3 | 3 | import io.cucumber.core.backend.CucumberInvocationTargetException;
|
4 | 4 | import io.cucumber.core.backend.Located;
|
5 | 5 |
|
| 6 | +import java.util.function.Consumer; |
| 7 | + |
6 | 8 | final class StackManipulation {
|
7 | 9 |
|
8 | 10 | private StackManipulation() {
|
9 | 11 |
|
10 | 12 | }
|
11 | 13 |
|
12 |
| - static Throwable removeFrameworkFrames(CucumberInvocationTargetException invocationException) { |
13 |
| - Throwable error = invocationException.getInvocationTargetExceptionCause(); |
14 |
| - StackTraceElement[] stackTraceElements = error.getStackTrace(); |
15 |
| - Located located = invocationException.getLocated(); |
16 |
| - |
17 |
| - int newStackTraceLength = findIndexOf(located, stackTraceElements); |
18 |
| - if (newStackTraceLength == -1) { |
19 |
| - return error; |
20 |
| - } |
| 14 | + static Throwable removeFrameworkFramesAndAppendStepLocation( |
| 15 | + CucumberInvocationTargetException invocationException, StackTraceElement stepLocation |
| 16 | + ) { |
| 17 | + Throwable error = invocationException.getCause(); |
| 18 | + walkException(error, appendStepLocation(invocationException.getLocated(), stepLocation)); |
| 19 | + return error; |
| 20 | + } |
21 | 21 |
|
22 |
| - StackTraceElement[] newStackTrace = new StackTraceElement[newStackTraceLength]; |
23 |
| - System.arraycopy(stackTraceElements, 0, newStackTrace, 0, newStackTraceLength); |
24 |
| - error.setStackTrace(newStackTrace); |
| 22 | + static Throwable removeFrameworkFrames(CucumberInvocationTargetException invocationException) { |
| 23 | + Throwable error = invocationException.getCause(); |
| 24 | + walkException(invocationException, removeFramesAfter(invocationException.getLocated())); |
25 | 25 | return error;
|
26 | 26 | }
|
27 | 27 |
|
28 |
| - private static int findIndexOf(Located located, StackTraceElement[] stackTraceElements) { |
29 |
| - if (stackTraceElements.length == 0) { |
30 |
| - return -1; |
| 28 | + private static void walkException(Throwable cause, Consumer<Throwable> action) { |
| 29 | + while (cause != null) { |
| 30 | + action.accept(cause); |
| 31 | + cause = cause.getCause(); |
31 | 32 | }
|
| 33 | + } |
32 | 34 |
|
33 |
| - int newStackTraceLength; |
34 |
| - for (newStackTraceLength = 1; newStackTraceLength < stackTraceElements.length; ++newStackTraceLength) { |
35 |
| - if (located.isDefinedAt(stackTraceElements[newStackTraceLength - 1])) { |
36 |
| - break; |
| 35 | + static Consumer<Throwable> removeFramesAfter(Located located) { |
| 36 | + return throwable -> { |
| 37 | + StackTraceElement[] stackTrace = throwable.getStackTrace(); |
| 38 | + int lastFrame = findIndexOf(located, stackTrace); |
| 39 | + if (lastFrame == -1) { |
| 40 | + return; |
37 | 41 | }
|
38 |
| - } |
39 |
| - return newStackTraceLength; |
| 42 | + StackTraceElement[] newStackTrace = new StackTraceElement[lastFrame + 1]; |
| 43 | + System.arraycopy(stackTrace, 0, newStackTrace, 0, lastFrame + 1); |
| 44 | + throwable.setStackTrace(newStackTrace); |
| 45 | + }; |
40 | 46 | }
|
41 | 47 |
|
42 |
| - static Throwable removeFrameworkFramesAndAppendStepLocation( |
43 |
| - CucumberInvocationTargetException invocationException, StackTraceElement stepLocation |
44 |
| - ) { |
45 |
| - Located located = invocationException.getLocated(); |
46 |
| - Throwable error = invocationException.getInvocationTargetExceptionCause(); |
47 |
| - if (stepLocation == null) { |
48 |
| - return error; |
49 |
| - } |
50 |
| - StackTraceElement[] stackTraceElements = error.getStackTrace(); |
51 |
| - int newStackTraceLength = findIndexOf(located, stackTraceElements); |
52 |
| - if (newStackTraceLength == -1) { |
53 |
| - return error; |
54 |
| - } |
55 |
| - StackTraceElement[] newStackTrace = new StackTraceElement[newStackTraceLength + 1]; |
56 |
| - System.arraycopy(stackTraceElements, 0, newStackTrace, 0, newStackTraceLength); |
57 |
| - newStackTrace[newStackTraceLength] = stepLocation; |
58 |
| - error.setStackTrace(newStackTrace); |
59 |
| - return error; |
| 48 | + private static Consumer<Throwable> appendStepLocation(Located located, StackTraceElement stepLocation) { |
| 49 | + return throwable -> { |
| 50 | + if (located == null) { |
| 51 | + return; |
| 52 | + } |
| 53 | + StackTraceElement[] stackTrace = throwable.getStackTrace(); |
| 54 | + int lastFrame = findIndexOf(located, stackTrace); |
| 55 | + if (lastFrame == -1) { |
| 56 | + return; |
| 57 | + } |
| 58 | + // One extra for the step location |
| 59 | + StackTraceElement[] newStackTrace = new StackTraceElement[lastFrame + 1 + 1]; |
| 60 | + System.arraycopy(stackTrace, 0, newStackTrace, 0, lastFrame + 1); |
| 61 | + newStackTrace[lastFrame + 1] = stepLocation; |
| 62 | + throwable.setStackTrace(newStackTrace); |
| 63 | + }; |
60 | 64 | }
|
61 | 65 |
|
| 66 | + private static int findIndexOf(Located located, StackTraceElement[] stackTraceElements) { |
| 67 | + for (int index = 0; index < stackTraceElements.length; index++) { |
| 68 | + if (located.isDefinedAt(stackTraceElements[index])) { |
| 69 | + return index; |
| 70 | + } |
| 71 | + } |
| 72 | + return -1; |
| 73 | + } |
62 | 74 | }
|
0 commit comments