<> wrote:
> In Perl it is possible to determine the caller. Is there an equivalent
> function in Java? Approximately, this is what I want to do
>
> class A {
>
> function f () {
> if (caller is B) do B stuff;
> if (caller is C) do C stuff;
> }
> class B extends A{}
> class C extends A{}
First of all, the need to do this is symptomatic of extremely serious
design problems. You should think hard before relying on something like
this.
That said, yes you can do it:
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
assert stackTrace.length >= 2 : "Can't determine calling context";
String callerClassName = stackTrace[1].getClassName();
if (callerClassName.equals("my.package.B")) ...;
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation