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 / March 2007

Tip: Looking for answers? Try searching our database.

How to get the signature of the method you are in?

Thread view: 
Lethal Possum - 29 Mar 2007 10:56 GMT
Hello everyone,

I use the following code to do some benchmarking of my application:

public void go(String arg) {
  if (TRACE) {
     long start = System.currentTimeMillis();
     try {
        _impl.go(arg);
     } finally {
        long stop = System.currentTimeMillis();
        Log.trace(_impl.getClass(), "go(String arg)", stop - start);
     }
  } else {
     _impl.go(arg);
  }
}

I was wondering what would be the most efficient way to generate the
"go(String arg)" message automatically. A solution would be to do
something like:

Throwable t = new Throwable();
StackTraceElement s = t.getStackTrace()[0];
String message = s.getMethodName();

But this strikes me as neither very elegant or efficient. And it
doesn't include the full method signature, only the method name. Maybe
someone have a better idea?

Thanks in advance for your suggestions,

Thomas
Daniel Pitts - 29 Mar 2007 16:10 GMT
> Hello everyone,
>
[quoted text clipped - 30 lines]
>
> Thomas

You can also look into creating a tracing Proxy class

look for java.lang.reflect.Proxy

It basically allows you to trap method calls.  If TRACE doesn't
change, you could even make a factory the returns the _impl object if !
TRACE, and return the proxy if TRACE.  That provides even better
performance.

Beyond that, you might even look into AspectJ or other Aspect Oriented
Programming (AOP) frameworks.


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.