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:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

whoseCalledMe()

List<StackTraceElement>

Returns the full list of StackTraceElement entries from the calling frame upward. Returns an empty list if the stack is too shallow.

isCallingMe

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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

getTrace

Description:

MethodReturn valuesDescription

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

Description:

MethodReturn valuesDescription

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 row, excluding framework frames.

toString

Description:

MethodReturn valuesDescription

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

getThreadDump

Description:

MethodReturn valuesDescription

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:

MethodReturn valuesDescription

searchHprofFiles()

List<File>

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()));
    }
}