[New] Use StackWalker instead of new Throwable().getStackTrace()[depth]

This commit is contained in:
Robert von Burg 2022-02-07 10:22:18 +01:00
parent 37e239e3de
commit 75ca814ba0
1 changed files with 3 additions and 7 deletions

View File

@ -39,13 +39,9 @@ public class ExceptionHelper {
}
public static String getCallerMethod(int depth) {
// TODO change to StackWalker:
// StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
// walker.walk(frames -> frames.skip(1)
// .map((StackWalker.StackFrame s) -> s.getDeclaringClass() + "." + s.getMethodName()).findFirst());
StackTraceElement element = new Throwable().getStackTrace()[depth];
return element.getClassName() + "." + element.getMethodName();
return StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE) //
.walk(frames -> frames.map((StackWalker.StackFrame sf) -> sf.getClassName() + "." + sf.getMethodName())
.skip(depth).findFirst()).orElse("UnknownClass.unknownMethod!");
}
/**