🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #18581

Re: How to know the caller of a method?

#18581
From: John P. McGrath
Date: Mon, 25 May 1998 00:00
31 lines
1492 bytes
soklay <icost@singnet.com.sg> wrote in article
<6jirgc$haf4@forums.borland.com>...

> Anyone knows whether there any way to know who is/are the 
> caller(s) of a method during run-time? In a method, I would 
> like to get hold of the string name of the class and method 
> which calls it. Else, is it possible to, in a method, get hold 
> if the name of itself and the name of the class it is declared 
> in at run-rime?

I can think of one way that would work, but you definitely would not want
to use this for "normal" programming:

	Throwable t = new Throwable();
	t.fillInStackTrace();

	StringWriter writer = new StringWriter();
	t.printStackTrace( writer );

	String str = writer.toString();

This will create a string containing a stack trace.  You could then parse
the stack trace to find out who the caller was.  Obviously it would not be
a good idea to write your application this way.  But for debugging, the
approach could be useful.

-- 
John P. McGrath
<mcgrath@enter.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID: <01bd87bf$19c07af0$0e47aacc@ferd>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <6jirgc$haf4@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,81abe3417e28e50d
X-Google-Attributes: gidf74b3,public
From: "John P. McGrath" <mcgrath@enter.net>
Subject: Re: How to know the caller of a method?
Date: 1998/05/25
Message-ID: <01bd87bf$19c07af0$0e47aacc@ferd>
X-Deja-AN: 356278367
References: <6jirgc$haf4@forums.borland.com>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.language