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 / December 2005

Tip: Looking for answers? Try searching our database.

Caller info as in Perl

Thread view: 
soup_or_power@yahoo.com - 19 Dec 2005 17:09 GMT
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



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.