obtaining a reference to the calling object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Murat Tasan

    obtaining a reference to the calling object

    i have a quick question... is there a way to obtain the reference to the
    object which called the currently executing method?

    here is the scenario, i have a class and a field which i would like to
    populate with a reference to the object that constructed this current
    object. i would attempt to accomplish this by setting the appropriate
    field from within the constructor...

    i figured it might be obtainable from the stack trace, but that doesn't
    seem to work.

    oh, and i do know that i could just add an extra parameter to the
    constructor and from the calling method just pass "this", but i don't want
    to have to trust that the people using my code will actually do this
    correctly each time.

    thanks for any input,

    murat

    --
    Murat Tasan
    mxt6@po.cwru.ed u
    tasan@eecs.cwru .edu
    murat.tasan@cwr u.edu
    Learn how the Genomics Core at Case Western Reserve University can provide you with a wide range of genomics and DNA/RNA quality control services.


  • Gregory A. Swarthout

    #2
    Re: obtaining a reference to the calling object

    Murat Tasan <tasan@eecs.cwr u.edu> wrote in message news:<Pine.SOL. 4.53.0401121353 170.947@zoidber g>...[color=blue]
    > i have a quick question... is there a way to obtain the reference to the
    > object which called the currently executing method?
    >
    > here is the scenario, i have a class and a field which i would like to
    > populate with a reference to the object that constructed this current
    > object. i would attempt to accomplish this by setting the appropriate
    > field from within the constructor...
    >
    > i figured it might be obtainable from the stack trace, but that doesn't
    > seem to work.[/color]

    What is the unworking code?

    Greg

    Comment

    • nos

      #3
      Re: obtaining a reference to the calling object


      "Murat Tasan" <tasan@eecs.cwr u.edu> wrote in message
      news:Pine.SOL.4 .53.04011213531 70.947@zoidberg ...[color=blue]
      > i have a quick question... is there a way to obtain the reference to the
      > object which called the currently executing method?
      >
      > here is the scenario, i have a class and a field which i would like to
      > populate with a reference to the object that constructed this current
      > object. i would attempt to accomplish this by setting the appropriate
      > field from within the constructor...[/color]

      Constructed or called?
      [color=blue]
      >
      > i figured it might be obtainable from the stack trace, but that doesn't
      > seem to work.
      >
      > oh, and i do know that i could just add an extra parameter to the
      > constructor and from the calling method just pass "this", but i don't want
      > to have to trust that the people using my code will actually do this
      > correctly each time.
      >
      > thanks for any input,
      >
      > murat
      >
      > --
      > Murat Tasan
      > mxt6@po.cwru.ed u
      > tasan@eecs.cwru .edu
      > murat.tasan@cwr u.edu
      > http://genomics.cwru.edu
      >[/color]


      Comment

      • Murat Tasan

        #4
        Re: obtaining a reference to the calling object

        really either. if "called", the called method would be a factory method,
        which would at some point set the appropriate calling object parameter...
        and if "constructe d" the constructor would include nearly the same line.

        so basically, if in a factory "called" method which has been called by a
        method in object x, a new object y is created and the correct parameter in
        y is set through something like this:

        y.parentObject = x

        (but without passing x (as a parameter set to value "this") from x, i
        don't know how to get a reference to x)

        if done in a constructor for y:

        this.parentObje ct = x

        but same problem again.

        the reason i don't want to have people pass the parent object as a
        parameter is it opens the door for incorrect objects to be passed... that
        is let us assume that the constructor option is used and the constructor
        includes a parameter for identifying the parent object... so from within x
        there is a line in a method like so:

        yClass y = new yClass(this);

        that would work, but someone could also then do this:

        yClass y = new yClass(z);

        where z is some other object, possibly of the same class as x (so checking
        the class of the object isn't enough to catch the problem).

        i want to guarantee that the parentObject reference in y correctly points
        to x by doing it automatically.

        thanks again,

        murat

        On Mon, 12 Jan 2004, nos wrote:
        [color=blue]
        >
        > "Murat Tasan" <tasan@eecs.cwr u.edu> wrote in message
        > news:Pine.SOL.4 .53.04011213531 70.947@zoidberg ...[color=green]
        > > i have a quick question... is there a way to obtain the reference to the
        > > object which called the currently executing method?
        > >
        > > here is the scenario, i have a class and a field which i would like to
        > > populate with a reference to the object that constructed this current
        > > object. i would attempt to accomplish this by setting the appropriate
        > > field from within the constructor...[/color]
        >
        > Constructed or called?
        >[color=green]
        > >
        > > i figured it might be obtainable from the stack trace, but that doesn't
        > > seem to work.
        > >
        > > oh, and i do know that i could just add an extra parameter to the
        > > constructor and from the calling method just pass "this", but i don't want
        > > to have to trust that the people using my code will actually do this
        > > correctly each time.
        > >
        > > thanks for any input,
        > >
        > > murat
        > >
        > > --
        > > Murat Tasan
        > > mxt6@po.cwru.ed u
        > > tasan@eecs.cwru .edu
        > > murat.tasan@cwr u.edu
        > > http://genomics.cwru.edu
        > >[/color]
        >
        >
        >[/color]

        --
        Murat Tasan
        mxt6@po.cwru.ed u
        tasan@eecs.cwru .edu
        murat.tasan@cwr u.edu
        Learn how the Genomics Core at Case Western Reserve University can provide you with a wide range of genomics and DNA/RNA quality control services.


        Comment

        • Jared Dykstra

          #5
          Re: obtaining a reference to the calling object

          Murat Tasan <tasan@eecs.cwr u.edu> wrote in message news:<Pine.SOL. 4.53.0401131014 290.1226@zoidbe rg>...[color=blue]
          > but same problem again.
          >
          > the reason i don't want to have people pass the parent object as a
          > parameter is it opens the door for incorrect objects to be passed... that
          > is let us assume that the constructor option is used and the constructor
          > includes a parameter for identifying the parent object... so from within x
          > there is a line in a method like so:
          >[/color]

          You could add an extra parameter, and then add some assertions
          ensuring the object is the desired instanceof.

          ....But this raises a bigger question. There should be no reason why
          you would ever want to do this. If there are a number of possible
          objects that a static method or other object can operate on, these
          objects should share a common parent object or interface. Then you can
          specify the interface and a classcastexcept ion will result if abused.
          Otherwise, it should produce some non-error result for every object
          (like toString())

          Rethink the design and odds are you can avoid this API. What you want
          to do may be the only good option, but it sounds like a big hack, and
          your maintenence concerns are wellfounded.

          ---
          Jared Dykstra

          Comment

          Working...