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
Section titled “Call stack inspection”whoCalledMe
Section titled “whoCalledMe”Description:
| Method | Return values | Description |
|---|---|---|
| whoCalledMe() | StackTraceElement | Returns the StackTraceElement of the method that called the current method (two frames up in the stack). Returns null if the stack is too shallow. |
Example:
StackTraceElement caller = StackTraceUtils.whoCalledMe();if (caller != null) { System.out.println("Called by: " + caller.getClassName() + "." + caller.getMethodName());}whoseCalledMe
Section titled “whoseCalledMe”Description:
| Method | Return values | Description |
|---|---|---|
| whoseCalledMe() | List |
Returns the full list of StackTraceElement entries from the calling frame upward. Returns an empty list if the stack is too shallow. |
isCallingMe
Section titled “isCallingMe”Description:
| Method | Return values | Description |
|---|---|---|
| isCallingMe(String className) | boolean | Returns true if any frame in the current call stack has a class name that starts with className. |
| isCallingMe(String className, String methodName) | boolean | Returns true if any frame in the current call stack matches both the given class name prefix and the exact method name. |
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
Section titled “logTrace”Description:
| Method | Return values | Description |
|---|---|---|
| logTrace(Logger log) | void | Logs the current call stack at WARN level using the supplied SLF4J Logger, one frame per line. Framework frames (Tomcat, Spring, etc.) are omitted. |
Stack trace formatting
Section titled “Stack trace formatting”getTrace
Section titled “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 row, excluding framework frames. |
getTraceList
Section titled “getTraceList”Description:
| Method | Return values | Description |
|---|---|---|
| getTraceList(Throwable t) | List |
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 |
Returns the stack trace of the given throwable as a list of strings starting from frame index row, excluding framework frames. |
toString
Section titled “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 PrintWriter. |
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
Section titled “Thread and heap diagnostics”getThreadDump
Section titled “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
Section titled “searchHprofFiles”Description:
| Method | Return values | Description |
|---|---|---|
| searchHprofFiles() | List |
Searches the Tomcat home directory (catalina.home system property) for heap dump files ending in .hprof. Returns an empty list if catalina.home is not set or is not accessible. |
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())); }}