Re: Determining Caller class of a method
Stephen Kolaroff <[email protected]>
| Newsgroups | gmane.comp.jakarta.bcel.devel |
|---|---|
| Message-ID | <[email protected]> |
A good way could be overriding ScurityManager and using its protected method getClassContext(), which " Returns the current execution stack as an array of classes" (See javadocs for details). This also addresses the issue for a malicious code packed in class HarmlessClassName, but loaded from a different class loader. Unfortunately, this will not give you the name of the method invoking you. Best regards Cheffo [email protected] 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] >