Hi Experts
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{}
Thanks for your help.
Hendrik Maryns - 19 Dec 2005 17:15 GMT
soup_or_power@yahoo.com schreef:
> Hi Experts
> In Perl it is possible to determine the caller. Is there an equivalent
[quoted text clipped - 8 lines]
> class B extends A{}
> class C extends A{}
You obviously didn´t get the idea of polymorphism.
abstract class A {
function f ();
}
class B extends A{
function f (){
do B stuff;
}
}
class C extends A{
function f (){
do C stuff;
}
}
H.

Signature
Hendrik Maryns
==================
www.lieverleven.be
http://aouw.org
Robert Klemme - 19 Dec 2005 17:18 GMT
> Hi Experts
> In Perl it is possible to determine the caller. Is there an equivalent
[quoted text clipped - 10 lines]
>
> Thanks for your help.
There are several solutions depending on what you need:
http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/34
d8c5be4024e398
Kind regards
robert
Chris Smith - 19 Dec 2005 17:25 GMT
> In Perl it is possible to determine the caller. Is there an equivalent
> function in Java? Approximately, this is what I want to do
[quoted text clipped - 7 lines]
> 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")) ...;

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation