• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

reference calling object

Is there a (reasonable) way to reference the calling object without explicitly passing "this". Or in other words, does the called object know who called it?

Thanks!



0
adg
Asked:
adg
  • 4
  • 4
  • 2
  • +1
3 Solutions
 
objectsCommented:
no
0
 
adgAuthor Commented:
Thats what I thought.
0
 
sciuriwareCommented:
There should be a way digging in the assembly code.
But, what's the purpose?
;JOOP!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
tomboshellCommented:
Could throw and catch an exception and parse the trace, then use some reflection.
0
 
objectsCommented:
> Could throw and catch an exception and parse the trace

that'd only get you the method, not the object.
0
 
sciuriwareCommented:
Well, this way of working would reveal the classes.
But still not the object itself.
;JOOP!
0
 
tomboshellCommented:
If it is simply to identify what called it then that is good.  If it is desired to invoke methods on the caller then it of course would not work that well, except for final static methods and such.  Since the trace-to-reflection route would create a new object and not the calling object, but it would be an object of the same class/type.  
That would be a kinda crazy way and due to the reflection not the best for performance, so a call back object being passed to the class is the best for such purposes.  But it could be fun! And also provide lots of entertainment by debugging and maintaining.
0
 
objectsCommented:
> but it would be an object of the same class/type.  

but very different state.
and how would you create it?
0
 
adgAuthor Commented:
Wow, I hadn't realized all these nuances. In this case, I'm just interested in identifying the class of the caller. But I know that isn't exactly what I asked (because I didn't know any better).

>> But it could be fun! And also provide lots of entertainment by debugging and maintaining
Hah, just what I need!

I'm going to raise the points and split them. Thanks for all your help!
0
 
objectsCommented:
try this:

StackTraceElement[] trace = new Exception().getStackTrace();
String classname = trace[trace.length-2].getClassName();
0
 
tomboshellCommented:
Thanks for the grade!

>> how would you create it?  
I would have to be a bit obtuse and say that it depends.  could do a classForName or use the class loader (hopefully it is the same) or use findClass(String name), could access a helper class (or a factory) with the string name, which I think would be preferrable.  It depends on where the class is and what constructors are available, if it is that person's class or a library, if it allows for construction outside of the package, what parameters if any, and prob other considerations that I did not mention.

>> but very different state.
I know, that is why it is not a very good way of reference, but it may be good just to get the name.  Otherwise, if it is just for knoweldge I would say to use a debugger and set a breakpoint at the part in the code that is of interest.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now