• Post Reply Bookmark Topic Watch Topic
  • New Topic

find creator of an object  RSS feed

 
Nijeesh Balan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have a situation here... say there are two classes Test1.java and Test2. java
I want to find the classs[es] which created the Test2 object. Is it possible to find this from ClassLoader etc.,?


Any help would be appreciated.
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is quite simple, but you have to change your code:

Notice that the Creator passes itself in the constructor to the created object. Now Created can have access to its Creator through the "myCreator" variable.
Another point is that you can't do this from a static method (public static void main) because "this" is not defined. However, if you only care about the class of the creator, you can do something like this:

Good luck,
Geoffrey
[ March 01, 2004: Message edited by: Geoffrey Falk ]
 
Nijeesh Balan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Geoffrey...
But the problem is I cannot change class Test1. I have the accessiblity to make modifications only in Test2.
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("PRint the name of the class who created me=" + new Throwable().getStackTrace()[0].getClassName() );
D.
 
Nijeesh Balan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much...
[ March 02, 2004: Message edited by: Nijeesh Balan ]
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try :
System.out.println("PRint the name of the class who created me=" + new Throwable().getStackTrace()[1].getClassName() );
D.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!