[New] Added ExceptionHelper.getCallerMethod(depth)

This commit is contained in:
Robert von Burg 2020-02-28 09:22:21 +01:00
parent ec555edf12
commit 91ecd0be8e
1 changed files with 10 additions and 1 deletions

View File

@ -35,7 +35,16 @@ public class ExceptionHelper {
* @return the class name and method name of the caller
*/
public static String getCallerMethod() {
StackTraceElement element = new Throwable().getStackTrace()[1];
return getCallerMethod(2);
}
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();
}