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.
Brandon McCombs - 03 May 2007 00:27 GMT
> Hi all,
> Does anyone can give me some idea about how to retrieve the function
[quoted text clipped - 3 lines]
> Thanks
> B.
What are you talking about?
Arne Vajhøj - 03 May 2007 00:29 GMT
> 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()[i].getMethodName()
but I would recommend using another approach to the underlying problem.
Arne
Stefan Ram - 03 May 2007 00:36 GMT
>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() ); }}
Daniel Pitts - 03 May 2007 02:40 GMT
> "Brittany....@gmail.com" <Brittany....@gmail.com> writes:
> >Does anyone can give me some idea about how to retrieve the
[quoted text clipped - 6 lines]
> ( java.lang.Thread.currentThread().
> getStackTrace()[ 1 ].getMethodName() ); }}
Yeah, type it out.
public void MyFunction() {
String functionName= "MyFunction";
}
Ben Schumeth - 03 May 2007 09:45 GMT
> Hi all,
> Does anyone can give me some idea about how to retrieve the function
[quoted text clipped - 3 lines]
> 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

Signature
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Gordon Beaton - 03 May 2007 10:26 GMT
> __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
--