Hi Dave, Thanks very much for the information. I will try this out.
Regards Shrinivas Joshi On Tue, 21 Jun 2005, David Hovemeyer wrote: > Shrinivas Joshi wrote: > > >Hi All, > > Is there any way by which one can determine the name/type of the class > >that invokes a particular method of some other class. I need to do this > >when the application control is within the method being called. I looked > >at BCEL ControlFlowGraph, Frame and OperandStack classes but it seems that > >they dont fetch any information related to caller class. > >Kindly let me know if someone has worked on similar requirement. > > > > > > I am assuming you need to do this at runtime, since finding out the > callers statically is generally impossible. > > One possible approach is to insert a call to a static instrumentation method > into the method you are trying to capture the callers for. The > instrumentation > method can create a stack trace as follows: > > StackTraceElement[] trace = new Throwable().getStackTrace(); > > Each stack trace element has the class name, method name, and line number > of the call site. You'll want to skip the top element, since it refers to > the instrumentation method. > > Note that you will have to use the line number to figure out > which version of the method is called if there are overloaded variants. > (If the class doesn't have line number information, then you're out of > luck.) > However, since all you need is the class name, then this approach should > do what you need. > > This works for 1.4 and later VMs. On earlier VMs you can accomplish > more or less the same thing using printStackTrace() and supplying a > PrintWriter backed by a StringWriter or similar object, and manually parsing > the printed stack trace. > > Hope this helps. > > -Dave > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
