aspose file tools*
The moose likes Java in General and the fly likes How to findout invoking class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to findout invoking class" Watch "How to findout invoking class" New topic
Author

How to findout invoking class

Vasantha Prabha
Ranch Hand

Joined: Oct 02, 2003
Posts: 108
Hi,
I have Question in core java.I have two classes A and B

When class A invokes classB.In B I have to findout that class A has invoked it.How can I do this.
Regards,
Sangeetha


Regards,Vasantha<p>Great minds discuss ideas. Average minds discuss events. Small minds discuss people.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In general, having to know who called you would be a sign to rethink the design. You'd like a class to behave the same way for any and all clients who might use it now and in the future. If there is conditional logic in your method, base the logic upon a parameter or some other state, not who called it.
That said, you can throw an exception, catch it, and parse the stacktrace information to learn the whole calling chain. People do this some times to display debugging information. But I'd still strongly avoid basing any logic on who called you.
Hope that helps! If not, fill us in more on exactly the problem you're trying to solve.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
That said, you can throw an exception, catch it, and parse the stacktrace information to learn the whole calling chain.
You can also skip the throw and catch, and just create a Throwable to get its stack trace. And note that since JDK 1.4 no parsing has been necessary - there's an API to get the stack info.

Notes:
  • traces[2] is used because traces[0] is inside getCallingClass(), and traces[1] is the method that called getCalling class. Which we probably already know - we want to know what class called that method.
  • There's no guarantee the stack trace will have the info we need; the JVM may leave it null in some cases. It may help to maximize the chance of getting info here by compiling with the -g option.


  • "I'm not back." - Bill Harding, Twister
    Eddie Vanda
    Ranch Hand

    Joined: Mar 18, 2003
    Posts: 281
    From the api docco on Class:

    You could get class A to pass a reference to its instance (this) as a calling parameter and then use the above code to get the name.


    The nice thing about Standards is that there are so many to choose from!
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    Thanks for the reminder on JDK 1.4. I've not used that yet, but it sounds useful.
    I'd repeat the advice to try real hard to not know who called you. Vasantha, do you have a really compelling reason? I can't think of any right off, but that surely doesn't mean there aren't any.
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    Should've mentioned previously - I agree with Stan's main original point too. It's rarely a good idea for a method to worry about who called it. The only exception I can think of is for logging, for debugging purposes. E.g. in java.util.logging.Logger, methods like entering() and exiting() shouldn't require the programmer to provide class and method names - that should really be done automatically, IMO. But unfortunately there's no guaranteed way for Java to provide this, as long as stack traces are considered to be optional.
    [ February 24, 2004: Message edited by: Jim Yingst ]
    sever oon
    Ranch Hand

    Joined: Feb 08, 2004
    Posts: 268
    The question is, do you need to know the invoking object, or do you need to know about it?
    If you need to know something about that object, just provide parameters for it to pass the knowledge you require. Or you can have it pass a this reference as previously suggested. This is an uncommon technique because good design rarely calls for its use, but it can be a Proper Thing To Do given the circumstances.
    On the other hand, if you are somehow limited from having the invoking object pass in the appropriate knowledge, I would be highly suspicious that that limitation is in place for a good reason--it's probably part of the design and is not required for your situation. For that reason, I can say with a good degree of confidence that there should NEVER be a good reason to pull the caller's identity out of the stack trace, barring a few situations that are too bizarre to contemplate.
    sev
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to findout invoking class
     
    Similar Threads
    how many objects are eligible for Garbage collection
    garbage collector......output.....?
    Reference Variable
    Call to function inside construction?
    Garbage Collection