Retrieve function name

B

Brittany.Foo

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

Brandon McCombs

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?
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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
 
S

Stefan Ram

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() ); }}
 
D

Daniel Pitts

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";
}
 
B

Ben Schumeth

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
 
G

Gordon Beaton

__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

--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top