Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Mechanism to identify calling object/method?

Reply
Thread Tools

Mechanism to identify calling object/method?

 
 
Nedron
Guest
Posts: n/a
 
      03-21-2006
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

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      03-21-2006
On 21 Mar 2006 15:08:47 -0800, "Nedron" <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>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
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
 
Chris Smith
Guest
Posts: n/a
 
      03-21-2006
Nedron <(E-Mail Removed)> wrote:
> 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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Dave Mandelin
Guest
Posts: n/a
 
      03-21-2006
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.

 
Reply With Quote
 
Nedron
Guest
Posts: n/a
 
      03-22-2006
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

 
Reply With Quote
 
asimkhan asimkhan is offline
Junior Member
Join Date: Jul 2011
Posts: 1
 
      07-06-2011
If I want the url of the calling file then what i hav to do.
(means if a servlet A is calling my method then how can i get the url of that servlet)

Please help.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
firefox's lock mechanism (linux) Alexander Schmolck Firefox 0 02-08-2005 03:28 PM
include mechanism for access lists? jimmy@temp01.nccom.com Cisco 2 12-07-2004 11:42 AM
Does .NET provide any mechanism to replace ActiveX controls on web??? Muhammad Shuaib Anjum ASP .Net 2 08-13-2004 05:23 AM
IPC Mechanism Named pipes or Windows messages or sthg else ? piyush ASP .Net 0 07-14-2004 02:41 PM
Re: IOS exploit: please disclose vehicle, not mechanism jankemi(remove) Cisco 16 07-25-2003 05:16 PM



Advertisments