diff --git a/docs/release-notes.html b/docs/release-notes.html index 5b99835b0..ff7090cca 100644 --- a/docs/release-notes.html +++ b/docs/release-notes.html @@ -49,6 +49,13 @@

Version 6.0.10



+
  • + 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. +

    +
  • +
  • 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 diff --git a/src/com/unboundid/util/StaticUtils.java b/src/com/unboundid/util/StaticUtils.java index 3f95893ba..a8ccb0b68 100644 --- a/src/com/unboundid/util/StaticUtils.java +++ b/src/com/unboundid/util/StaticUtils.java @@ -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 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 diff --git a/tests/unit/src/com/unboundid/util/StaticUtilsTestCase.java b/tests/unit/src/com/unboundid/util/StaticUtilsTestCase.java index c4bf5602c..5922785da 100644 --- a/tests/unit/src/com/unboundid/util/StaticUtilsTestCase.java +++ b/tests/unit/src/com/unboundid/util/StaticUtilsTestCase.java @@ -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. */