On Tue, 25 Oct 2022 19:43:14 GMT, Jorn Vernee <jver...@openjdk.org> wrote:

>> Is it possible to use filter/findfirst without using lambdas? I want to 
>> avoid recursion inside the tracing code.
>> 
>> I am not sure about dumping the call stack. It seems an overkill and not 
>> useful in most cases. It’s easier to rebuild the JDK and add 
>> Thread.dumpStack() in the rare occasion that you need to do this.
>
>> Is it possible to use filter/findfirst without using lambdas? I want to 
>> avoid recursion inside the tracing code.
> 
> You could do this I believe (if I've eye-balled that correctly :)):
> Suggestion:
> 
>         String callerName = caller.getName();
>         String callerInfo = StackWalker.getInstance().walk(new 
> Function<Stream<StackWalker.StackFrame>, String>() {
>             // We use inner classes (instead of stream/lambda) to avoid 
> triggering
>             // further invokedynamic resolution, which would cause infinite 
> recursion.
>             // It's OK to use + for string concat, because java.base is 
> compiled without
>             // the use of indy string concat.
>             @Override
>             public String apply(Stream<StackWalker.StackFrame> s) {
>                 return s.filter(new Predicate<StackWalker.StackFrame>() {
>                     @Override
>                     public boolean test(StackWalker.StackFrame f) {
>                         return callerName.equals(f.getClassName());
>                     }
>                 }).findFirst().get().toStackTraceElement().toString();
>             }
>         });
> 
> 
>> I am not sure about dumping the call stack. It seems an overkill and not 
>> useful in most cases. It’s easier to rebuild the JDK and add 
>> Thread.dumpStack() in the rare occasion that you need to do this.
> 
> Fair enough.

Or, maybe it's easier to use `Thread.currentThread().getStackTrace()` and avoid 
messing around with streams altogether.

-------------

PR: https://git.openjdk.org/jdk/pull/10842

Reply via email to