so according to post here, we can call StackWalker::getCallerClass to get the caller class of a method.
What I want to achieve is that if class Caller calls class Callee's method doSth(), then I'll get Caller.class as the caller class in doSth().
As I want to have a function to hide the details for getting caller class and for better reusability, instead of directly calling StackWalker in doSth(), I created getCallerClass() function in Callee.
However, calling getCallerClass() will give me Callee.class instead of Caller.class since now that the caller becomes Callee inside function getCallerClass().
So my question is, is there a way for me create a separate function to wrap the StackWalker logics, or I have to do it in doSth()?
public class Caller {
callee.dosth();
}
public class Callee {
public void doSth(){
// CORRECT: return Caller.class
Class<?> callerClass = StackWalker.getInstance(RETAIN_CLASS_REFERENCE).getCallerClass();
// WRONG: return Callee.class
Class<?> wrongCallerClass = getCallerClass();
...
}
// I want to have a function to hide the details for getting caller class
private Class<?> getCallerClass(){
return StackWalker.getInstance(RETAIN_CLASS_REFERENCE).getCallerClass();
}
}