Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Caller info as in Perl

Reply
Thread Tools

Caller info as in Perl

 
 
soup_or_power@yahoo.com
Guest
Posts: n/a
 
      12-19-2005
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.

 
Reply With Quote
 
 
 
 
Hendrik Maryns
Guest
Posts: n/a
 
      12-19-2005
schreef:
> 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{}


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.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      12-19-2005
wrote:
> 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.


There are several solutions depending on what you need:
http://groups.google.com/group/comp....d8c5be4024e398


Kind regards

robert

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      12-19-2005
<> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
SPA3102 pstn caller display - info Steve Hayes UK VOIP 4 10-18-2010 11:14 AM
Anyone successfully passing caller ID info thru voip.co.uk Linker3000 UK VOIP 31 04-24-2007 04:05 PM
Can't get function caller if the caller is from a function within a popup window Mark Javascript 2 04-03-2004 07:57 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57