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

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

Am 20.09.2013 18:38, schrieb Peter Levart:
> As I understand the Thread.firstCaller() does exactly that.
> "findFirstCallerMatchingPredicate". Choosen name abbreviation is maybe
> not making the semantic immediately obvious.

then sorry, I overlooked that in the link

>> 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());
>>> }
> But the API is making it possible without walking entire stack or making
> N partial walks with lengths 1..N, which is O(N^2)...

You know the trouble with the O-notation. For once, there are constants 
in this, that are not mentioned and for second O(N^2) is only always 
better than any O(N^3) if N is big enough. Otherwise even an O(2^N) can 
be less effort. And the lengths we are talking here about are not big Ns

> More compact, using lambda:
> Class getCallerClass(int nonSkippedFramesDepth) {
>     int[] depth = new int[1];
>     return Thread.fistCaller(
>        info -> !haveToSkip(info.getDeclaringClass()) && (++depth[0] >
> nonSkippedFramesDepth),
>        StackFrameInfo::getDeclaringClass
>     );
> }

silly me... of course... why do I return the class when I need to return 
a boolean.. its been a long day already... ok, your version is accepted 
;)  using an array instead is quite the hack for me, but we are used to 
that for Java AIC

bye blackdrag

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