This discussion is archived
8 Replies Latest reply: Feb 17, 2010 6:03 PM by jschellSomeoneStoleMyAlias RSS

how to get caller object reference from a method

843798 Newbie
Currently Being Moderated
Hi,

I am working a already existing Java Swing project, now I got a problem, in a method I need to get the caller object reference otherwise, I can't succeed this operation. So please tell me a way how to get the caller object reference from a method. that method would be static or regular method anything will do for me.

Edited by: navaneeth.j on Jan 29, 2010 11:20 PM
  • 1. Re: how to get caller object reference from a method
    EJP Guru
    Currently Being Moderated
    I need to get the caller object reference
    Why? This sort of thing is almost always a symptom of poor design.
  • 2. Re: how to get caller object reference from a method
    843798 Newbie
    Currently Being Moderated
    Yes sir, I know that is poor design. our project is using for web applications also in a by making the source code into jar format, so at this time even we are changing the design also but it is a huge project it takes more time so for temporary solution, so we need caller object reference in the called method. because if we can't provide temporary solution we need to shutdown our project for sometime and concentrate on only design and development, because of that we need to face more problems
  • 3. Re: how to get caller object reference from a method
    843798 Newbie
    Currently Being Moderated
    navaneeth.j wrote:
    ... Java Swing project ...
    ... in a method I need to get the caller object reference
    ... please tell me a way how to get the caller object reference from a method.
    that method would be static or regular method anything will do for me.
    Put the method where you need a reference to the calling class instance in a (non-static) inner class
    then you can access the embedding class instance as this$0.
  • 4. Re: how to get caller object reference from a method
    843798 Newbie
    Currently Being Moderated
    Please I didn't understand can you explain a bit elaborately.

    Actually my doubt is, I have a method "addition" method, which is using by many classes so my requirement is in the addition method I want to write a code snippet which will identify and get the the caller object. Actually I tried Reflection.getcallerclass but there I am getting "CLASS" object not the actual object reference, but I want object reference.

    Actually we have a huge project which is writen plain JAVA, so in this project the authors written the Database connection package for single database transaction. so now we are using this project source code for JSF application in this web application the DB package has serve based on the dynamic db connection parameters, so if we want to change this package fully means need to solve the dependency problem in hundreds of classes, so my point is if I can access the caller object in the DB package when ever it gets called by any class from any where of the project. So actually I liked Reflection.getcallerclass, the way of implementation perfectly works for me but it is not giving caller object reference, if something gives the caller object then I can get the DB connection parameters then there is no need to pass the parameters in the hierarchy.
  • 5. Re: how to get caller object reference from a method
    843798 Newbie
    Currently Being Moderated
    navaneeth.j wrote:
    Actually my doubt is, I have a method "addition" method, which is using by many classes so my requirement is in the addition method I want to write a code snippet which will identify and get the the caller object. Actually I tried Reflection.getcallerclass but there I am getting "CLASS" object not the actual object reference, but I want object reference.

    Actually we have a huge project which is writen plain JAVA, so in this project the authors written the Database connection package for single database transaction. so now we are using this project source code for JSF application in this web application the DB package has serve based on the dynamic db connection parameters, so if we want to change this package fully means need to solve the dependency problem in hundreds of classes, so my point is if I can access the caller object in the DB package when ever it gets called by any class from any where of the project. So actually I liked Reflection.getcallerclass, the way of implementation perfectly works for me but it is not giving caller object reference, if something gives the caller object then I can get the DB connection parameters then there is no need to pass the parameters in the hierarchy.
    You can add a parameter (of type Object) to your addition() method
    and everywhere you call the addition() method also pass this (which from the POW of the addition() method will be a reference to the calling class instance).

    There may be alternative solutions
    but none that require less effort.
  • 6. Re: how to get caller object reference from a method
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    navaneeth.j wrote:
    Actually we have a huge project which is writen plain JAVA, so in this project the authors written the Database connection package for single database transaction. so now we are using this project source code for JSF application in this web application the DB package has serve based on the dynamic db connection parameters, so if we want to change this package fully means need to solve the dependency problem in hundreds of classes, so my point is if I can access the caller object in the DB package when ever it gets called by any class from any where of the project.
    Hundreds isn't that many. But I can spot some other design problems in that statement as well.
  • 7. Re: how to get caller object reference from a method
    843798 Newbie
    Currently Being Moderated
    In most cases it is not relevant or necessary that for a Java method to know the object that called it.
    If code needs to know the origin of a method call
    it is likely the method is located in the wrong host class,
    or overall program structure does not follow good object oriented principles.
    Consider whether you can move the method to a different host class or refractor to place class-specific code in the relevant classes.

    If you find there really is good reason to know the origin of method calls,
    add an Object argument to the method and use the getClass() method to test.
  • 8. Re: how to get caller object reference from a method
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    sqlexception wrote:
    In most cases it is not relevant or necessary that for ...
    Feel free to stop by in 2011 to add another reply to a question that was already answered.