forked from mockito/mockito
/
Java8LocationImpl.java
67 lines (54 loc) · 2.27 KB
/
Java8LocationImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.debugging;
import java.io.Serializable;
import org.mockito.internal.exceptions.stacktrace.StackTraceFilter;
import org.mockito.invocation.Location;
class Java8LocationImpl implements Location, Serializable {
private static final long serialVersionUID = -9054861157390980624L;
// Limit the amount of objects being created, as this class is heavily instantiated:
private static final StackTraceFilter stackTraceFilter = new StackTraceFilter();
private String stackTraceLine;
private String sourceFile;
public Java8LocationImpl() {
this(new Throwable(), false);
}
public Java8LocationImpl(Throwable stackTraceHolder, boolean isInline) {
this(stackTraceFilter, stackTraceHolder, isInline);
}
public Java8LocationImpl(StackTraceFilter stackTraceFilter) {
this(stackTraceFilter, new Throwable(), false);
}
private Java8LocationImpl(
StackTraceFilter stackTraceFilter, Throwable stackTraceHolder, boolean isInline) {
computeStackTraceInformation(stackTraceFilter, stackTraceHolder, isInline);
}
@Override
public String toString() {
return stackTraceLine;
}
/**
* Eagerly compute the stacktrace line from the stackTraceHolder. Storing the Throwable is
* memory-intensive for tests that have large stacktraces and have a lot of invocations on
* mocks.
*/
private void computeStackTraceInformation(
StackTraceFilter stackTraceFilter, Throwable stackTraceHolder, boolean isInline) {
StackTraceElement filtered = stackTraceFilter.filterFirst(stackTraceHolder, isInline);
// there are corner cases where exception can have a null or empty stack trace
// for example, a custom exception can override getStackTrace() method
if (filtered == null) {
this.stackTraceLine = "-> at <<unknown line>>";
this.sourceFile = "<unknown source file>";
} else {
this.stackTraceLine = "-> at " + filtered;
this.sourceFile = filtered.getFileName();
}
}
@Override
public String getSourceFile() {
return sourceFile;
}
}