Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

Mechanism to identify calling object/method?

Thread view: 
Nedron - 22 Mar 2006 00:08 GMT
Is there a mechanism within Java (J2SE 5) that would allow me to
identify which object and/or method is calling a target method?

For example, if I have method Class1.foo() that is being called from
Class2.bar(), is there a way for Class1.foo() to identify one or both
of the calling object and method?

-Nedron
Roedy Green - 22 Mar 2006 00:36 GMT
>Is there a mechanism within Java (J2SE 5) that would allow me to
>identify which object and/or method is calling a target method?
>
>For example, if I have method Class1.foo() that is being called from
>Class2.bar(), is there a way for Class1.foo() to identify one or both
>of the calling object and method?

see http://mindprod.com/jgloss/trace.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Nedron - 22 Mar 2006 05:13 GMT
Thanks to all for the pointer to Throwable. That's exactly what I was
looking for. This is specifically to attempt debugging a very strange
multithreaded synchronization issue.

-David
Chris Smith - 22 Mar 2006 00:40 GMT
> Is there a mechanism within Java (J2SE 5) that would allow me to
> identify which object and/or method is calling a target method?
>
> For example, if I have method Class1.foo() that is being called from
> Class2.bar(), is there a way for Class1.foo() to identify one or both
> of the calling object and method?

Yes, at least half of it is possible.  No, you shouldn't do it unless
you're looking for debugging information.

If you are trying to get information for debugging (for example, writing
to debug dump files and such, then the following is what you want:

   new Throwable().printStackTrace();

There's also a version of printStackTrace that takes a PrintWriter.  
There's also a getStackTrace() the returns an array of stack trace
elements containing information about the source file, line number,
method, class, etc. for each stack frame in the call stack.  (It does
not contain the object).

If you're looking at doing this in live code, then you've got design
problems.  Somewhere, you need to have actually told the code you're
calling which object it needs to interact with.  Perhaps that should
have been during the call to the method, or perhaps elsewhere such as in
a constructor.  In any case, it's not at all a good idea to try to
recover this information, having failed to retain it in the first place.  
Even if the information available from Throwable.getStackTrace() is
sufficient for your needs, it leads to fragile code that will break at
some future time.  For that reason, you really need to find a better
answer than making real use of the stack; though providing it to
developers for informational purposes is entirely normal.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Dave Mandelin - 22 Mar 2006 00:42 GMT
For a normal program, it's better to redesign to avoid the need to do
this. If you have some special purpose that makes it necessary, there
are a couple of things you can do. The method is easier to get: you can
create a Throwable and then parse its stack trace. To get the object, I
think you need a runtime instrumentation system, like AspectJ.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.