0

Note: To be clear, the question being asked is not about "How can I display line numbers in Eclipse?" That is answered here @ StackOverflow.

Goal: I want to know if Eclipse has a macro that acts as getLineNumber() the same way Visual Studio has the ability to use the preprocessor macro __LINE__ to easily/universally get the line# for where this particular call to getLineNumber() lives on. It's useful for knowing if a line of code was hit, without using a debugger/breakpoints.

I'd like to debug using: println("I just hit file@line: "+__FILE__ +'@'+ __LINE__ );

I am seeking a universal macro that can be copied and pasted to any file/any line, so that those values are known at/before compile time. Does Java/Eclipse offer such a feature? Possibly in an Eclipse plugin?

I can't find any info on this anywhere, I'm at a loss. Thanks.

Community
  • 1
  • 1
unity303
  • 35
  • 6
  • 2
    Um, I think what you're talking about is a language feature, not an IDE feature. `__FILE__` and `__LINE__` are predefined in C and C++ I'm not sure what the Java equivalent is. – Fred Larson Mar 27 '14 at 03:48
  • After researching, yes you are absolutely right, it's language-wide – unity303 Mar 28 '14 at 05:42

2 Answers2

2

You can try that:

System.out.println("I just hit file@line: "+Thread.currentThread().getStackTrace()[1].getFileName() +'@'+ Thread.currentThread().getStackTrace()[1].getLineNumber());

Thread.currentThread().getStackTrace() will return an array of StackTraceElement, and we take the second element (the first one will be in the Thread class), and get the file name and line number from that.

EDIT: You may also want to create a static function (you can put it in an utilitary class) to do that (which will avoid the problems with the different java implementations):

public static void printCurrentFileAndLine() {
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    for (int i=0; i<ste.length; i++)
        if (ste[i].getMethodName().equals("printCurrentFileAndLine")) {
            System.out.println("I just hit file@line: "+ste[i+1].getFileName() +'@'+ ste[i+1].getLineNumber() );
            break;
        }
}

And you just need to call:

printCurrentFileAndLine();
Florent Bayle
  • 11,520
  • 4
  • 34
  • 47
  • Thanks for your quick answer Florent. I ran this in my Android activity but I'm seeing this: `I just hit file@line: Thread.java@579`. I will try outputting the entire getStackTrace() to see if the info I need is there – unity303 Mar 27 '14 at 04:01
  • @unity303 I've tried it under Eclipse, and it worked, I think that maybe the Android java version is a little bit different, and so you may need to use `Thread.currentThread().getStackTrace()[2]`, or something like that. – Florent Bayle Mar 27 '14 at 04:06
  • exactly, I was thinking the same and after seeing the entire [] of the stack trace, I saw that element [2] was outputting perfectly for Android. The 0th element was the Android VM, "VMStack.java@-2". You have *TOTALLY* made my day. Thank you! – unity303 Mar 27 '14 at 04:10
  • @unity303 Glad that it worked ! I've added a static method you can use which will dynamically skip all the unneeded part of the stack, whatever the java implementation is. – Florent Bayle Mar 27 '14 at 04:17
2

Be careful with Thread.getStackTrace() method, it's a costly method. Just use it when you really need.

For more information about that: How Expensive is Thread.getStackTrace()?

Community
  • 1
  • 1
Brice Argenson
  • 802
  • 2
  • 8
  • 13
  • Good point. I profiled it using nanotime over > 100 test cases. The shortest it took was .2ms, but longest was 1.2ms. To make it negligible in production builds, I bypass the method entirely if I am in a release build. – unity303 Mar 28 '14 at 05:36