Caller info as in Perl

Discussion in 'Java' started by soup_or_power@yahoo.com, Dec 19, 2005.

  1. Guest

    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.
     
    , Dec 19, 2005
    #1
    1. Advertisements

  2. 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
     
    Hendrik Maryns, Dec 19, 2005
    #2
    1. Advertisements

  3. 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.lang.java.programmer/browse_thread/thread/34d8c5be4024e398


    Kind regards

    robert
     
    Robert Klemme, Dec 19, 2005
    #3
  4. Chris Smith Guest

    <> 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
     
    Chris Smith, Dec 19, 2005
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    2,190
  2. Vasanth
    Replies:
    0
    Views:
    783
    Vasanth
    Jun 28, 2004
  3. Andrea Williams

    Clear the Render info & change to different info

    Andrea Williams, Oct 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,640
    Ken Dopierala Jr.
    Oct 27, 2004
  4. Kiran
    Replies:
    0
    Views:
    1,442
    Kiran
    Feb 6, 2005
  5. ilya

    get CPU info, RAM info

    ilya, Apr 8, 2010, in forum: Java
    Replies:
    43
    Views:
    8,142
    Mike Schilling
    Apr 19, 2010
  6. Kenshin
    Replies:
    1
    Views:
    399
    Jeff Cochran
    Jun 25, 2004
  7. Mark
    Replies:
    2
    Views:
    576
  8. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    535
    PerlFAQ Server
    Jan 23, 2011
Loading...