Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Get the "source" class

Thread view: 
Carsten H. Pedersen - 26 Feb 2006 16:59 GMT
I want to get a hold of the "calling" class of a method / constructor.
An example would be as follows:

 public class A() {
   public A() {
     new B();
   }
 }

 public class B() {
   public B() {
     [stuff here]
   }
 }

If i replace [stuff here] with System.out.print(getClass().getName()),
i would get "B" printed. But i'm interested in getting a hold of A -
the instance isn't necessary... the name of the class will do just
fine. I suspect it can be done, since the stacktrace of exceptions
contain pretty much the information i want.

Anyone have a solution to this problem?

Regards,
Carsten H. Pedersen
Carsten H. Pedersen - 26 Feb 2006 20:53 GMT
> I want to get a hold of the "calling" class of a method / constructor.
> An example would be as follows:
[quoted text clipped - 18 lines]
>
> Anyone have a solution to this problem?

Alas, i was too quick in giving up. I found some pointers, and i've
found a solution to the problem. If anyone is interested, this is what
i put in to replace [stuff here]:

   StackTraceElement[] ste = new Throwable().getStackTrace();
   if (ste.length > 1) {
     System.out.println("Class: "+ste[1].getClassName());
   }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.