> 8268829: Provide an optimized way to walk the stack with Class object only
> 
> `StackWalker::walk` creates one `StackFrame` per frame and the current 
> implementation
> allocates one `StackFrameInfo` and one `MemberName` objects per frame. Some 
> frameworks
> like logging may only interest in the Class object but not the method name 
> nor the BCI,
> for example, filters out its implementation classes to find the caller class. 
>  It's
> similar to `StackWalker::getCallerClass` but allows a predicate to filter out 
> the element.
> 
> This PR proposes to add `Option.NO_METHOD_INFO` new stack walking option.
> If no method information is needed, this option can be used such that the
> stack walker will save the overhead (1) to extract the method information
> and (2) the memory used for the stack walking.   In addition, this can also 
> fix
> 
> - [8311500](https://bugs.openjdk.org/browse/JDK-8311500): 
> StackWalker.getCallerClass() throws UOE if invoked reflectively
> 
> Adding `NO_METHOD_TIME` option provides a simple way for existing code,
> for example logging frameworks, to take advantage of this enhancement with
> the least change as it can keep the existing implementation in traversing
> `StackFrame`s.
> 
> For example: to find the first caller filtering a known list of 
> implementation class,
> existing code can just add `NO_METHOD_INFO` in the call to 
> `StackWalker::getInstance`
> to create a stack walker instance:
> 
> 
>      StackWalker walker = 
> StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE, NO_METHOD_INFO);
>      Optional<Class<?>> callerClass = walker.walk(s ->
>              s.map(StackFrame::getDeclaringClass)
>               .filter(interestingClasses::contains)
>               .findFirst());
> 
> 
> If method information is accessed on the `StackFrame`s produced by this stack 
> walker such as
> `StackFrame::getMethodName`, then `UnsupportedOperationException` will be 
> thrown.
> 
> The alternative considered is to provide a new API:
> `<T> T walkClass(Function<? super Stream<Class<?>, ? extends T> function)`
> 
> In this case, the caller would need to pass a function that takes a stream
> of `Class` object instead of `StackFrame`.  Existing code would have to
> modify calls to the `walk` method to `walkClass` and the function body.
> 
> ### Implementation Details
> 
> If `NO_METHOD_NAME` is set, the implementation creates `ClassFrameInfo[]`
> buffer that is filled by the VM during stack walking.   `ClassFrameInfo` 
> holds the 
> Class instance plus `flags`  which indicate if it's caller sensitive or 
> hidden.  
> With this change, `StackWalker::getCa...

Mandy Chung has updated the pull request with a new target base due to a merge 
or a rebase. The pull request now contains 16 commits:

 - Replace NO_METHOD_INFO option with StackWalker.Kind enums
 - Merge branch 'master' of https://github.com/openjdk/jdk into 
stackwalker-class
 - clean up
 - fix trailing whitespace
 - Merge branch 'master' of https://github.com/openjdk/jdk into 
stackwalker-class
 - Update LocalLongHelper.java
 - clean up
 - Merge branch 'master' of https://github.com/openjdk/jdk into 
stackwalker-class
 - Add StackWalker.Option.NO_METHOD_INFO
 - Merge branch 'master' of https://github.com/openjdk/jdk into 
stackwalker-class
 - ... and 6 more: https://git.openjdk.org/jdk/compare/ce1ded1a...434d90cb

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

Changes: https://git.openjdk.org/jdk/pull/15370/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=15370&range=02
  Stats: 1195 lines in 33 files changed: 694 ins; 204 del; 297 mod
  Patch: https://git.openjdk.org/jdk/pull/15370.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/15370/head:pull/15370

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

Reply via email to