Skip to main content
gamedev.net
🔒 Locked

[java] Reference to calling class?

Started by erydo 12 replies 2.6k views
Original Post
erydo
erydo
Hello - Does anyone know of a way to obtain a reference to an object's calling class in Java? As in, the object B is a member of object A, and a member function of B wants to reference A. Basically, I have been provided class A but am not allowed to modify it, but class B, which can be guaranteed to be only a member of class A, is mine to do as I please with. I should think this would be possible; the code
sun.reflect.Reflection.getCallerClass(2).getName();
when called by a member of B will return 'A', but I don't want the type I want an actual reference to the specific instance of A. Any ideas? (all class names have been changed to protect their privacy) ;) Also, although this project is for an assignment, this is not an assignment question. That is, I want to know how to do this so that I can expand the capabilities of my program beyond the limitations of the provided wrapper code. Thanks for the help [Edited by - erydo on November 18, 2004 5:16:30 PM]
erydo
erydo
It would be, but B's constructor is called without arguments in A, so I can't modify how that's done.
Kevinator
Kevinator
Ergoth, he's already said he got that far. He's looking for a reference, not a name. As for what you should do, that's a tough one. Are you allowed to write a subclass of A? That way, you could reference a subclass of A quite easily while maintaining A's functionality.

If you literally need to reference the calling class, then I'm drawing blanks. I can't think of a situation where this could possibly be useful.
erydo
erydo
Yes, I need an actual reference to the instance of the calling class.

To be more specific, the class that I'm not allowed to modify is called GraphicApp, which extends JFrame. I need a reference to this so I can add more event handlers than are set up by default. The class I'm allowed to modify is called Cartoon, which is a member variable (not subclass or anything like that) of GraphicApp.

The relevant bits of GraphicApp are like this (and I cannot change any of it):
public class GraphicApp extends JFrame {   private Cartoon myCartoon;   public GraphicApp () {   myCartoon = new Cartoon();   }    public static void main(String [] s) {      GraphicApp g = new GraphicApp();   }}


Cartoon needs a reference to its parent GraphicApp so it can pass it to an EventHandler class.

I would think that there would be some function to climb the stack and actually obtain the instance of the object I need, but I can't figure out how to do it.
DJHoy
DJHoy
Quote:
Original post by erydo
public class GraphicApp extends JFrame {   private Cartoon myCartoon;   public GraphicApp () {   myCartoon = new Cartoon();   }    public static void main(String [] s) {      GraphicApp g = new GraphicApp();   }}

... snipped ...


If you can modify Cartoon, do something like this:

public class Cartoon {  private GraphicApp caller;  //... construction code here...  public void setCaller(GraphicApp whichCaller) {    caller=whichCaller;  }}


then in Graphic App:
//...   public GraphicApp () {   myCartoon = new Cartoon();   myCartoon.setCaller(this);   } //...

andrewk3652
andrewk3652
I there anything that says you can't write another class to do this? Do you have to use ONLY those two classes? Like this...

public class StudyApplication{        private GraphicApp gApp;        private Cartoon toon;        public StudyApplication()        {               gApp = new GraphicApp();               toon = new toon();               toon.setCaller(gApp);               // do any appropriate linking between GraphicApp               // and toon here ...        }        public static void main(String[] args)        {               // and now just run this class instead of running               // GraphicApp's main function, to get around not                // being able to modify it...               StudyApplication theApp = new StudyApplication();        }}


but if you can't use another class, and you have to run the whole shebang from within GraphicApp's main, you're still screwed...
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================
erydo
erydo
Nope, it has to be GraphicApp's main().

However, andrewk3652, you do bring to mind an interesting idea.

It would be drastic and ugly, but if I could then have Cartoon call a function to fork a new thread, then I could customize that one; but then I'd have find a way to close the originating thread. That's something I'll look into, but as I said that's taking drastic action.

I still look forward to more suggestions, your responses thus far have been great.
Koroljov
Koroljov
As anonymous poster 1 said, if the Cartoon-class is a Component that is added to the JFrame, you can get the JFrame by doing something like this:

Component comp=this;
while(comp.getParent()!=null)
{
comp=comp.getParent();
}

(obviousely this can only be done after the Cartoon is added to the JFrame using getContentPane().add(theCartoon) in the GraphicApp)
Why do my programs never work on other computers?
erydo
erydo
*sigh*

I'm beginning to think that it's impossible to do it this way at all and that I might need to look into an alternative method of getting an event handler.

Koroljov: That idea looked good at first, but the Cartoon class does not extend Component, and it isn't one -- it performs all drawing options by being passed a java.awt.Graphics object from GraphicApp.

If there's a way to get the parent component from that, it should be good, but I'm still looking for a way to do that.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.