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. Advertising

  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. Advertising

  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. Advertising

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. Vasanth
    Replies:
    0
    Views:
    528
    Vasanth
    Jun 28, 2004
  2. Andrea Williams

    Clear the Render info & change to different info

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

    get CPU info, RAM info

    ilya, Apr 8, 2010, in forum: Java
    Replies:
    43
    Views:
    5,926
    Mike Schilling
    Apr 19, 2010
  5. Mark
    Replies:
    2
    Views:
    385
Loading...

Share This Page