Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Retrieve function name

Thread view: 
Brittany.Foo@gmail.com - 02 May 2007 23:21 GMT
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

--


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.