Skip to content

Commit

Permalink
Add StaticUtils.getBacktrace
Browse files Browse the repository at this point in the history
Added a StaticUtils.getBacktrace method that can be used to retrieve
a compact single-line string representation of a stack trace of the
code from which the method was called.
  • Loading branch information
dirmgr committed Aug 4, 2023
1 parent 28b0186 commit 8213885
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/release-notes.html
Expand Up @@ -49,6 +49,13 @@ <h3>Version 6.0.10</h3>
<br><br>
</li>

<li>
Added a StaticUtils.getBacktrace method that can be used to retrieve a compact
single-line string representation of a stack trace of the code from which the
method was called.
<br><br>
</li>

<li>
Added client-side support for the access log field request control that can be
used in an upcoming version of the Ping Identity Directory Server to include a
Expand Down
46 changes: 46 additions & 0 deletions src/com/unboundid/util/StaticUtils.java
Expand Up @@ -2384,6 +2384,52 @@ public static void byteArrayToCode(@NotNull final byte[] array,



/**
* Retrieves a single-line string representation of the stack trace for the
* current thread. It will not include the call to the {@code getBacktrace}
* method itself, nor anything that it calls either directly or indirectly.
*
* @return A single-line string representation of the stack trace for the
* current thread.
*/
@NotNull()
public static String getBacktrace()
{
// Get the stack trace elements for the curren thread. It will likely
// include not only an element for this method, but also for the
// Thread.getStackTrace method itself. So we want to filter those out
final StackTraceElement[] stackTraceElements =
Thread.currentThread().getStackTrace();
final List<StackTraceElement> elementList = new ArrayList<>();

boolean foundStartingPoint = false;
for (final StackTraceElement e : stackTraceElements)
{
if (foundStartingPoint)
{
elementList.add(e);
continue;
}

if (e.getClassName().equals(StaticUtils.class.getName()) &&
e.getMethodName().equals("getBacktrace"))
{
foundStartingPoint = true;
}
}

if (foundStartingPoint)
{
return getStackTrace(toArray(elementList, StackTraceElement.class));
}
else
{
return getStackTrace(stackTraceElements);
}
}



/**
* Retrieves a single-line string representation of the stack trace for the
* provided {@code Throwable}. It will include the unqualified name of the
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/src/com/unboundid/util/StaticUtilsTestCase.java
Expand Up @@ -540,6 +540,22 @@ public Object[][] getTestToHexData()



/**
* Tests the {@code getBacktrace} method.
*/
@Test()
public void testGetBacktrace()
{
final String backtrace = StaticUtils.getBacktrace();
assertNotNull(backtrace);

assertTrue(backtrace.startsWith(
"testGetBacktrace(StaticUtilsTestCase.java"));
assertFalse(backtrace.contains("getBacktrace(StaticUtils.java"));
}



/**
* Tests the {@code getStackTrace} method.
*/
Expand Down

0 comments on commit 8213885

Please sign in to comment.