Mechanism to identify calling object/method?

Discussion in 'Java' started by Nedron, Mar 21, 2006.

  1. Nedron

    Nedron Guest

    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
    Nedron, Mar 21, 2006
    #1
    1. Advertising

  2. Nedron

    Roedy Green Guest

    On 21 Mar 2006 15:08:47 -0800, "Nedron" <> 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.
    Roedy Green, Mar 21, 2006
    #2
    1. Advertising

  3. Nedron

    Chris Smith Guest

    Nedron <> 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
    Chris Smith, Mar 21, 2006
    #3
  4. 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.
    Dave Mandelin, Mar 21, 2006
    #4
  5. Nedron

    Nedron Guest

    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
    Nedron, Mar 22, 2006
    #5
  6. Nedron

    asimkhan

    Joined:
    Jul 6, 2011
    Messages:
    1
    url of calling file...

    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.
    asimkhan, Jul 6, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mars
    Replies:
    2
    Views:
    409
    Bengt Richter
    Jul 19, 2003
  2. Replies:
    2
    Views:
    302
    sebastian
    Mar 28, 2009
  3. Replies:
    4
    Views:
    294
    JosephKK
    Apr 2, 2009
  4. why-em-jay

    Calling a method of the calling object ...

    why-em-jay, Sep 15, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    175
    Anno Siegel
    Sep 15, 2005
  5. Ferry Bolhar

    Cache mechanism for method lookup

    Ferry Bolhar, Aug 29, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    104
    Uri Guttman
    Aug 29, 2006
Loading...

Share This Page