Jave equivalent of C++ __FUNCTION__ and __LINE__ macros

Here’s one way to get this information, .. add the following line to whatever / wherever you want to trace

System.out.println((new Throwable()).getStackTrace()[0].toString());

Sample output:
com.junk.Junk3.main(Junk3.java:12)

An added advantage of adding debug traces as above is that , when using an IDE such as Eclipse, you can click on an individual trace and automatically beam up the relevant code on your code editor pane.

~ by vinaybalamuru on December 11, 2008.

Leave a Reply

You must be logged in to post a comment.