3

I'm searching for a way of developing a MethodInterceptor which prints the caller class.

Is there any way of getting the caller object into the method interceptor?

2 Answers 2

4

This might work, declare an exception and then use it to get a look at the stack at the time when your method is intercepted:

 Throwable t = new Throwable(); StackTraceElement[] elements = t.getStackTrace(); String calleeMethod = elements[0].getMethodName(); String callerMethodName = elements[1].getMethodName(); String callerClassName = elements[1].getClassName(); System.out.println("CallerClassName=" + callerClassName + " , Caller method name: " + callerMethodName); System.out.println("Callee method name: " + calleeMethod);  
Sign up to request clarification or add additional context in comments.

1 Comment

This will get the class name. Is there a way to get the calling instance, so that my advice can visit the calling instance?
0

You could do something crude with generating a stack trace and inspecting it, but that is ugly

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.