Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Retrieve function name

Reply
Thread Tools

Retrieve function name

 
 
Brittany.Foo@gmail.com
Guest
Posts: n/a
 
      05-02-2007
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.

 
Reply With Quote
 
 
 
 
Brandon McCombs
Guest
Posts: n/a
 
      05-02-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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?
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      05-02-2007
(E-Mail Removed) 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()[i].getMethodName()

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

Arne
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      05-02-2007
"(E-Mail Removed)" <(E-Mail Removed)> 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() ); }}

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      05-03-2007
On May 2, 4:36 pm, (E-Mail Removed)-berlin.de (Stefan Ram) wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> 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";
}

 
Reply With Quote
 
Ben Schumeth
Guest
Posts: n/a
 
      05-03-2007
On Thu, 03 May 2007 00:21:07 +0200, (E-Mail Removed)
<(E-Mail Removed)> 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/tutor...ect/index.html

Ben

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      05-03-2007
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

--
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
how can i retrieve the name of form when one of the element name in this form is 'name' ashok.dhananjeyan@gmail.com Javascript 6 11-24-2006 02:54 AM
print("my name is {name}, and {age}-year old {gender}", name, age, gender); =?iso-8859-1?B?bW9vcJk=?= Java 7 01-02-2006 04:39 PM
name = name.substring(0, name.lastIndexOf('.')); Help please Jack-2 Javascript 3 12-24-2003 04:39 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments