This is an automated email from the ASF dual-hosted git repository. He-Pin pushed a commit to branch refactor/stackwalker-testkitutils in repository https://gitbox.apache.org/repos/asf/pekko.git
commit 11aefe9801935e93702f6c29da662eab8955f3cd Author: 虎鸣 <[email protected]> AuthorDate: Tue Jun 23 10:45:51 2026 +0800 refactor: use StackWalker for stack trace inspection in TestKitUtils Motivation: TestKitUtils.testNameFromCallStack uses Thread.currentThread.getStackTrace which eagerly constructs the full StackTraceElement[] array including method names, file names, and line numbers. Modification: Replace with StackWalker (JDK 9) which avoids creating full StackTraceElement objects, only extracting class names via the lighter StackFrame.getClassName(). The existing filtering logic (dropWhile + startsWith matching) is preserved unchanged. Result: More efficient stack inspection for test infrastructure. The StackWalker approach follows the pattern already established by LoggerClass.scala in the same codebase. Tests: - sbt "actor-tests / Test / testOnly org.apache.pekko.actor.ActorSystemSpec" — 20/20 passed References: Refs #3136 --- .../src/main/scala/org/apache/pekko/testkit/TestKitUtils.scala | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/testkit/src/main/scala/org/apache/pekko/testkit/TestKitUtils.scala b/testkit/src/main/scala/org/apache/pekko/testkit/TestKitUtils.scala index 9694eeb61a..b0def8b675 100644 --- a/testkit/src/main/scala/org/apache/pekko/testkit/TestKitUtils.scala +++ b/testkit/src/main/scala/org/apache/pekko/testkit/TestKitUtils.scala @@ -13,7 +13,9 @@ package org.apache.pekko.testkit +import java.lang.StackWalker.StackFrame import java.lang.reflect.Modifier +import java.util.stream.{ Stream => JStream } import scala.util.matching.Regex @@ -25,6 +27,10 @@ import org.apache.pekko.annotation.InternalApi @InternalApi private[pekko] object TestKitUtils { + private val stackWalker: java.util.function.Function[JStream[StackFrame], Array[String]] = + (frames: JStream[StackFrame]) => + frames.map(_.getClassName).toArray[String]((size: Int) => new Array[String](size)) + def testNameFromCallStack(classToStartFrom: Class[?], testKitRegex: Regex): String = { def isAbstractClass(className: String): Boolean = { @@ -36,8 +42,8 @@ private[pekko] object TestKitUtils { } val startFrom = classToStartFrom.getName - val filteredStack = Thread.currentThread.getStackTrace.iterator - .map(_.getClassName) + val classNames = StackWalker.getInstance().walk(stackWalker) + val filteredStack = classNames.iterator // drop until we find the first occurrence of classToStartFrom .dropWhile(!_.startsWith(startFrom)) // then continue to the next entry after classToStartFrom that makes sense --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
