StackTraceUtils
Utility class with static helper methods for inspecting the current call stack, formatting exception stack traces, capturing thread dumps, and locating heap dump files. All methods are static. Trace results automatically exclude common framework packages (Tomcat, Spring, Spring Security) so that only application-level frames are returned.
Call stack inspection
whoCalledMe
Description:
| Method | Return values | Description |
|---|---|---|
|
whoCalledMe() |
StackTraceElement |
Returns the |
Example:
StackTraceElement caller = StackTraceUtils.whoCalledMe();
if (caller != null) {
System.out.println("Called by: " + caller.getClassName() + "." + caller.getMethodName());
}
whoseCalledMe
Description:
| Method | Return values | Description |
|---|---|---|
|
whoseCalledMe() |
List<StackTraceElement> |
Returns the full list of |
isCallingMe
Description:
| Method | Return values | Description |
|---|---|---|
|
isCallingMe(String className) |
boolean |
Returns |
|
isCallingMe(String className, String methodName) |
boolean |
Returns |
Example:
package com.openkm;
import com.openkm.util.StackTraceUtils;
public class Test {
public static void main(String[] args) {
boolean calledFromAction = StackTraceUtils.isCallingMe("com.openkm.automation.action");
System.out.println("Called from action: " + calledFromAction);
boolean calledFromFire = StackTraceUtils.isCallingMe("com.openkm.automation", "fireEvent");
System.out.println("Called from fireEvent: " + calledFromFire);
}
}
logTrace
Description:
| Method | Return values | Description |
|---|---|---|
|
logTrace(Logger log) |
void |
Logs the current call stack at WARN level using the supplied SLF4J |
Stack trace formatting
getTrace
Description:
| Method | Return values | Description |
|---|---|---|
|
getTrace() |
String |
Returns the current call stack as a formatted string, excluding framework frames. |
|
getTrace(Throwable t) |
String |
Returns the stack trace of the given throwable as a formatted string, excluding framework frames. |
|
getTrace(Throwable t, int row) |
String |
Returns the stack trace of the given throwable starting from frame index |
getTraceList
Description:
| Method | Return values | Description |
|---|---|---|
|
getTraceList(Throwable t) |
List<String> |
Returns the stack trace of the given throwable as a list of strings, one per frame, excluding framework frames. |
|
getTraceList(Throwable t, int row) |
List<String> |
Returns the stack trace of the given throwable as a list of strings starting from frame index |
toString
Description:
| Method | Return values | Description |
|---|---|---|
|
toString(Throwable t) |
String |
Converts the full exception stack trace (including message and all frames) to a plain text string, equivalent to printing it to a |
Example:
package com.openkm;
import com.openkm.util.StackTraceUtils;
import java.util.List;
public class Test {
public static void main(String[] args) {
try {
List<String> keys = null;
for (String key : keys) {
}
} catch (Exception e) {
System.out.println(StackTraceUtils.toString(e));
System.out.println("--- filtered trace ---");
System.out.println(StackTraceUtils.getTrace(e));
}
}
}
Thread and heap diagnostics
getThreadDump
Description:
| Method | Return values | Description |
|---|---|---|
|
getThreadDump() |
String |
Returns a full thread dump of all live threads in the JVM, including lock and monitor information. Useful for diagnosing deadlocks or hung threads. |
searchHprofFiles
Description:
| Method | Return values | Description |
|---|---|---|
|
searchHprofFiles() |
List<File> |
Searches the Tomcat home directory ( |
Example:
package com.openkm;
import com.openkm.util.StackTraceUtils;
import java.io.File;
import java.util.List;
public class Test {
public static void main(String[] args) {
System.out.println(StackTraceUtils.getThreadDump());
List<File> hprofs = StackTraceUtils.searchHprofFiles();
hprofs.forEach(f -> System.out.println("Heap dump: " + f.getAbsolutePath()));
}
}