diff --git a/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/handlers/ClientCaptureCurrentContextRestHandler.java b/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/handlers/ClientCaptureCurrentContextRestHandler.java index 94ac7b54119e99..2b1d0cacc28126 100644 --- a/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/handlers/ClientCaptureCurrentContextRestHandler.java +++ b/independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/handlers/ClientCaptureCurrentContextRestHandler.java @@ -1,7 +1,9 @@ package org.jboss.resteasy.reactive.client.handlers; -import java.util.ArrayList; import java.util.List; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.stream.Stream; import org.jboss.resteasy.reactive.client.impl.ClientRequestContextImpl; import org.jboss.resteasy.reactive.client.impl.RestClientRequestContext; @@ -35,21 +37,18 @@ public void handle(RestClientRequestContext requestContext) throws Exception { } private void captureCallerStackTrace(ClientRequestContextImpl clientRequestContext) { - StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); - List effectiveStackTrace = new ArrayList<>(stackTrace.length); - boolean foundUserMethod = false; - // skip first trace which is Thread.getStackTrace - for (int i = 1; i < stackTrace.length; i++) { - StackTraceElement trace = stackTrace[i]; - if (foundUserMethod) { - effectiveStackTrace.add(trace); - } else if (!trace.getClassName().startsWith(RESTEASY_REACTIVE_PACKAGE) - && !trace.getClassName().contains(AUTOGENERATED_TAG)) { - // Skip the latest traces that starts with the "org.jboss.resteasy.reactive" package, - effectiveStackTrace.add(trace); - foundUserMethod = true; + List effectiveStackTrace = StackWalker.getInstance().walk(new Function<>() { + @Override + public List apply(Stream stream) { + return stream.dropWhile(new Predicate<>() { + @Override + public boolean test(StackWalker.StackFrame stackFrame) { + String className = stackFrame.getClassName(); + return className.startsWith(RESTEASY_REACTIVE_PACKAGE) || className.contains(AUTOGENERATED_TAG); + } + }).limit(5).map(StackWalker.StackFrame::toStackTraceElement).toList(); } - } + }); clientRequestContext.getRestClientRequestContext() .setCallerStackTrace(effectiveStackTrace.toArray(EMPTY_ARRAY));