Retrieve function name

Discussion in 'Java' started by Brittany.Foo@gmail.com, May 2, 2007.

  1. Guest

    Hi all,
    Does anyone can give me some idea about how to retrieve the function
    name in Java?
    (similarly __FUNCTION__ in C/C++)

    Thanks
    B.
     
    , May 2, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi all,
    > Does anyone can give me some idea about how to retrieve the function
    > name in Java?
    > (similarly __FUNCTION__ in C/C++)
    >
    > Thanks
    > B.
    >


    What are you talking about?
     
    Brandon McCombs, May 3, 2007
    #2
    1. Advertising

  3. wrote:
    > Hi all,
    > Does anyone can give me some idea about how to retrieve the function
    > name in Java?
    > (similarly __FUNCTION__ in C/C++)


    Try:

    (new Exception()).getStackTrace().getMethodName()

    but I would recommend using another approach to the underlying problem.

    Arne
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, May 3, 2007
    #3
  4. Stefan Ram Guest

    "" <> writes:
    >Does anyone can give me some idea about how to retrieve the
    >function name in Java?
    >(similarly __FUNCTION__ in C/C++)


    public class Main
    { public static void main( final java.lang.String[] args )
    { java.lang.System.out.println
    ( java.lang.Thread.currentThread().
    getStackTrace()[ 1 ].getMethodName() ); }}
     
    Stefan Ram, May 3, 2007
    #4
  5. Daniel Pitts Guest

    On May 2, 4:36 pm, -berlin.de (Stefan Ram) wrote:
    > "" <> writes:
    > >Does anyone can give me some idea about how to retrieve the
    > >function name in Java?
    > >(similarly __FUNCTION__ in C/C++)

    >
    > public class Main
    > { public static void main( final java.lang.String[] args )
    > { java.lang.System.out.println
    > ( java.lang.Thread.currentThread().
    > getStackTrace()[ 1 ].getMethodName() ); }}


    Yeah, type it out.

    public void MyFunction() {
    String functionName= "MyFunction";
    }
     
    Daniel Pitts, May 3, 2007
    #5
  6. Ben Schumeth Guest

    On Thu, 03 May 2007 00:21:07 +0200,
    <> wrote:

    > Hi all,
    > Does anyone can give me some idea about how to retrieve the function
    > name in Java?
    > (similarly __FUNCTION__ in C/C++)
    >
    > Thanks
    > B.
    >


    __FUNCTION__ is not standard C/C++, and I would find any code that uses it
    very suspect.
    Similarly, you may want to try to find a different approach for your Java
    problem. Do you really need to know the function name?
    You may want to have a look at the reflection API, but only after making
    sure there is no plausible alternative design.
    There's a good reflection tutorial at
    http://java.sun.com/docs/books/tutorial/reflect/index.html

    Ben

    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
     
    Ben Schumeth, May 3, 2007
    #6
  7. On Thu, 03 May 2007 10:45:29 +0200, Ben Schumeth wrote:
    > __FUNCTION__ is not standard C/C++, and I would find any code that
    > uses it very suspect.


    On the other hand __func__ is standard (C99) and not at all suspect.

    __FUNCTION__ is gcc "shorthand" for __func__, and was in widespread
    use before __func__ became standard.

    The use of this kind of helper is to avoid repeating information in
    the code, i.e. the method (or function) name only needs to occur in
    one place, and is always correct even if the method (or function) gets
    renamed. This feature is lacking in Java (stacktrace hacks
    notwithstanding).

    See http://en.wikipedia.org/wiki/Don't_repeat_yourself

    /gordon

    --
     
    Gordon Beaton, May 3, 2007
    #7
    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. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    829
    Roedy Green
    Jan 2, 2006
  2. ding feng
    Replies:
    2
    Views:
    2,821
    ding feng
    Jun 25, 2003
  3. Bobby Chamness
    Replies:
    2
    Views:
    2,400
    Joe Smith
    Apr 22, 2007
  4. Jack-2
    Replies:
    3
    Views:
    272
    Jack-2
    Dec 24, 2003
  5. Replies:
    6
    Views:
    170
Loading...

Share This Page