I would like to know if there’s a way in java to find out the class/object that called a certain static method.
Example:
public class Util{
...
public static void method(){}
...
}
public class Caller{
...
public void callStatic(){
Util.method();
}
...
}
Can I find out if Util.method was called from the Caller class?
You can use
Thread.currentThread().getStackTrace()inUtil.method.To get the last call before
Util.methodyou can do something like this: