[PATCH] 4851444: Exposing sun.reflect.Reflection#getCallerClass as a public API in Java 8

Peter Levart peter.levart at gmail.com
Fri Sep 20 10:05:39 UTC 2013

On 09/20/2013 10:58 AM, Jochen Theodorou wrote:
> Am 20.09.2013 09:09, schrieb Mandy Chung:
> [...]
>> Stack is a "stream" that allows you to walk partial stack (e.g. find
>> caller) or full stack trace (e.g. throwable). The filtering and mapping
>> operations are lazy to avoid having the VM eagerly copying the entire
>> stack trace data even for the short reach case (like Groovy and Log4j).
> and is there a link to StackStram too?
> The Thread#walkStack methods don't really describe the behaviour of 
> the consumer. Will the consumer applied only once, or multiple times? 
> If only once, then to replace getCallerClass(int depth), you will need 
> the version with the predicate, which will be difficult to realize 
> with a simple lambda, since you will need to count as well.

The use-cases described used getCallerClass(int depth) repeatedly to 
find a caller by iterating over a range of depths and calling 
getCallerClass(depth). You can use either Thread.walkStack or 
Thread.firstCaller for implementing those use-cases.

Maybe the following method would be handy to optimize search when we 
know that we want to skip 1st N frames before starting testing with 

public static <T> T firstCaller(int startDepth,
                                 Predicate<StackFrameInfo> predicate,
                                 Function<StackFrameInfo,T> function) {


then becomes:

Thread.firstCaller(depth, f -> true, StackFrameInfo::getDeclaringClass);



> bye Jochen

More information about the core-libs-dev mailing list