• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

find creator of an object  RSS feed

 
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.
 
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.
 
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.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!