Getting a reference to enclosing instance without using literal classname

Discussion in 'Java' started by Robert Dodier, Sep 14, 2008.

  1. Hello,

    Given something like class Outer { class Inner {...} ... } and an
    instance of Outer.Inner.

    Is there any way to retrieve a reference to the enclosing instance
    without mentioning the class name "Outer" ??

    I know that Class.getEnclosingClass can find the enclosing class, but
    there
    doesn't seem to be a similar object method, say,
    "Object.getEnclosingInstance".

    I am trying to process multiple, undetermined classes in a uniform
    way,
    so if possible I want to avoid peppering the code with literal class
    names.

    Thanks for any information.

    Robert Dodier
     
    Robert Dodier, Sep 14, 2008
    #1
    1. Advertising

  2. Robert Dodier

    Roedy Green Guest

    Re: Getting a reference to enclosing instance without using literal class name

    On Sun, 14 Sep 2008 15:18:22 -0700 (PDT), Robert Dodier
    <> wrote, quoted or indirectly quoted someone
    who said :

    >
    >I am trying to process multiple, undetermined classes in a uniform
    >way,
    >so if possible I want to avoid peppering the code with literal class
    >names.


    I once proposed there should be a "that" keyword corresponding to
    "this" to mean the current class.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 15, 2008
    #2
    1. Advertising

  3. Robert Dodier

    Mark Space Guest

    Re: Getting a reference to enclosing instance without using literalclass name

    Robert Dodier wrote:
    > Hello,
    >
    > Given something like class Outer { class Inner {...} ... } and an
    > instance of Outer.Inner.
    >
    > Is there any way to retrieve a reference to the enclosing instance
    > without mentioning the class name "Outer" ??


    If you think about it... an inner class is just a concept maintained by
    the compiler, so

    class Outer {
    class Inner {}
    }

    is really just:

    class Outer {}

    class Outer$$Inner {
    private Outer that;
    }

    Where the private field is just used to maintain the illusion of a
    connection. So my first thought is if it's private, you really ought to
    make a public getter.

    class Outer {
    class Inner {
    public Outer getOuter() { return Outer.this; }
    }
    }

    But my second thought was that if it's just a field... you could
    probably get at it with reflection somehow. This could be very brittle
    since not all Java compilers will use the same naming convention, but it
    might get you out of whatever jam you are in for now, assuming that the
    public getter is not an option.

    (I have no idea what this field is actually named. You'll have to build
    a little test case to discover what your compiler does.)
     
    Mark Space, Sep 15, 2008
    #3
  4. Re: Getting a reference to enclosing instance without using literalclass name

    On Sep 14, 7:01 pm, Mark Space <> wrote:

    > But my second thought was that if it's just a field... you could
    > probably get at it with reflection somehow.


    Thanks for the hint. Looks like javac names it "this$0".

    However, since that field is private, access via reflection
    from outside the class is prohibited, isn't it?

    best,

    Robert Dodier
     
    Robert Dodier, Sep 15, 2008
    #4
  5. Robert Dodier

    Mark Space Guest

    Re: Getting a reference to enclosing instance without using literalclass name

    Robert Dodier wrote:
    > On Sep 14, 7:01 pm, Mark Space <> wrote:
    >
    >> But my second thought was that if it's just a field... you could
    >> probably get at it with reflection somehow.

    >
    > Thanks for the hint. Looks like javac names it "this$0".
    >
    > However, since that field is private, access via reflection
    > from outside the class is prohibited, isn't it?


    As far as I know, yeah you can't get at it. I've heard rumor that
    there's a way around this, but I don't know what the technique is.
     
    Mark Space, Sep 15, 2008
    #5
  6. Robert Dodier

    EJP Guest

    Re: Getting a reference to enclosing instance without using literalclass name

    Robert Dodier wrote:
    > However, since that field is private, access via reflection
    > from outside the class is prohibited, isn't it?


    From outside the outer class, yes, but see Field.setAccessible().
     
    EJP, Sep 15, 2008
    #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. -
    Replies:
    1
    Views:
    13,733
  2. Oliver S.

    classname::classname a type ?

    Oliver S., Sep 11, 2003, in forum: C++
    Replies:
    1
    Views:
    524
    White Wolf
    Sep 11, 2003
  3. Hongzheng Wang
    Replies:
    32
    Views:
    894
  4. Stanimir Stamenkov
    Replies:
    4
    Views:
    518
    Chris Uppal
    Jul 14, 2006
  5. Ante Perkovic

    CSS: "tagname.classname" or ".classname"

    Ante Perkovic, Dec 22, 2003, in forum: Javascript
    Replies:
    2
    Views:
    117
Loading...

Share This Page