Refer to current filename and line.

A

Aaron Fude

Hi,

I remember this was possible in C with macros. Is it possible in Java
to refer to the current file name and line number as in

System.out.println( something.that.returns.current.file.name() + "
printed this line.");

Many thanks in advance!

Aaron
 
N

Neil Coffey


As of Java 5, you can also go via the ThreadMXBean.getThreadInfo().
I've not tested, but I wonder if there could be a sliiight performance
gain because you can specify that you only need the top stack trace
element(s). (The code would be uglier, of course.)

You can also call Thread.currentThread().getStackTrace(), but internally
this appears to just create a Throwable and query it, so it depends
which code you like looking at...!

Neil
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top