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

Jochen Theodorou blackdrag at gmx.org
Fri Sep 20 11:19:58 UTC 2013

Am 20.09.2013 12:05, schrieb Peter Levart:
> 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.

first Caller is only going back one, or not? Not useful for us in the 
cases we need this. I wouldn't say the API through getCallerClass(int) 
was optimal for us, I think the new one can be better... I only miss (or 
didn't get how) the ability not to have to walk the entire stack, 
without knowing before how deep I have to go

> 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
> predicate:
> public static <T> T firstCaller(int startDepth,
>                                  Predicate<StackFrameInfo> predicate,
>                                  Function<StackFrameInfo,T> function) {
> Reflection.getCallerClass(depth)
> then becomes:
> Thread.firstCaller(depth, f -> true, StackFrameInfo::getDeclaringClass);
> Hm...

that is I think a usecase for some... as I said, getCallerClass(int) is 
not really ideal for us either. More ideal in this style would be for us

>  public static <T> T findCaller(Predicate<StackFrameInfo> predicate,
>                                 Function<StackFrameInfo,T> function)

with the predicate indicating when to stop.. though the usage of this is 
not that nice:

> Class getCallerClass(final int nonSkippedFramesDepth) {
>    return findCaller(new Predicate<StackFrameInfo>() {
> 		int depth = 0;
> 		boolean test(StackFrameInfo info) {
>                     if (haveToSkip(info.getDeclaringClass())) return false;
>                     depth++;
>                     if (depth>=nonSkippedFramesDepth) return info.getDeclaringClass();
>                 }
>           }, StackFrameInfo::getDeclaringClass());
> }

Have you guys though about exposing the StackStream instead? Then you 
could use all the existing JDK method for streams on that, which gives 
you a much more flexible API. I could then for example change the Stream 
of StackFrameInfo into one of Class.

bye Jochen

Jochen "blackdrag" Theodorou - Groovy Project Tech Lead
blog: http://blackdragsview.blogspot.com/
german groovy discussion newsgroup: de.comp.lang.misc
For Groovy programming sources visit http://groovy-lang.org

More information about the core-libs-dev mailing list