Skip to content

Commit 24571be

Browse files
authored
Add stack trace capture implementation (#26)
1 parent afb56e0 commit 24571be

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@
146146
<configuration>
147147
<jdkToolchain>
148148
<version>${maven.compiler.release}</version>
149-
<vendor>oracle</vendor>
150149
</jdkToolchain>
151150
</configuration>
152151
</plugin>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.nordstrom.common.base;
2+
3+
/**
4+
* Throwable created purely for the purposes of reporting a stack trace.
5+
*
6+
* This is not an Error or an Exception and is not expected to be thrown or caught.
7+
* This <a href='https://blog.vanillajava.blog/2021/12/unusual-java-stacktrace-extends.html'>blog post</a> provided the
8+
* original implementation.
9+
* @author <a href='https://github.com/peter-lawrey'>Peter K Lawrey</a>
10+
*/
11+
12+
public class StackTrace extends Throwable {
13+
14+
private static final long serialVersionUID = -3623586250962214453L;
15+
16+
public StackTrace() {
17+
this("stack trace");
18+
}
19+
20+
public StackTrace(String message) {
21+
this(message, null);
22+
}
23+
24+
public StackTrace(String message, Throwable cause) {
25+
super(message + " on " + Thread.currentThread().getName(), cause);
26+
}
27+
28+
public static StackTrace forThread(Thread t) {
29+
if (t == null) return null;
30+
31+
StackTrace st = new StackTrace(t.toString());
32+
StackTraceElement[] stackTrace = t.getStackTrace();
33+
int start = 0;
34+
35+
if (stackTrace.length > 2) {
36+
if (stackTrace[0].isNativeMethod()) {
37+
start++;
38+
}
39+
}
40+
41+
if (start > 0) {
42+
StackTraceElement[] ste2 = new StackTraceElement[stackTrace.length - start];
43+
System.arraycopy(stackTrace, start, ste2, 0, ste2.length);
44+
stackTrace = ste2;
45+
}
46+
47+
st.setStackTrace(stackTrace);
48+
return st;
49+
}
50+
}

0 commit comments

Comments
 (0)