JDK-6439965 : javac needs __FILE__ and __LINE__ style support for compile t StackTraceElement
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2006-06-16
  • Updated: 2010-04-04
  • Resolved: 2006-11-17
Related Reports
Relates :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
__FILE__ should be a keyword which is replaced with a literal string uniquely identifying the
compilation unit, which in most cases would be the file name.  It should include the package directory names but not other directory names.  For instance, "com/mycompany/Foo.java" but not "/home/myname/mysrcdir/com/mycompany/Foo.java".   An acceptable alternative would be to just provide whatever would otherwise comeback from the Throwable.getFileName() method.

__LINE__ should correspond to the line number within the compilation unit, or whatever would come back from Throwable.getLineNumber() on that platform.

__CLASS__ and __METHOD__ should behave similarly, although the class name is the one piece of these 4 that can currently be accessed efficiently.

all 4 of these tokens should have the same zero run time overhead that would result from using the token "MyClass.java"  instead of __FILE__.


The extra credit feature that would leap frog other languages in its support for real developer requirements would be to support compile time efficient access to the *calling*
functions __FILE__,__LINE__,__CLASS__, and __METHOD__, eg __CALLER_FILE__, etc...

JUSTIFICATION :
Detailed logging enables a lot more programmer efficiency than stepping through a debugger.  printf() style debugging is what many java users do, which is why log4j is
such a popular project within the java community.

Creating a new throwable to get stack trace information is too inefficient to allow for detailed debug logging without imposing an unacceptable performance limitation in many cases.

Please dont close the RFE before other people have had an opportunity to vote on it, as happened the last time.(4411102)  Please dont use platform compatibility as an excuse, as happened with environment variables for years, since this doesnt need anything that isnt already in Throwable.  Please feel free to substitute equivalent solutions that are more aesthetically pleasing.


CUSTOMER SUBMITTED WORKAROUND :
The current work around is to create a Throwable object and walk up the frame stack, or
print out its stack trace and parse it.  See log4j for implementation examples of jumping through this hoop.    This workaround only works for code that can afford to be very very slow.

Comments
EVALUATION Personally, I always preferred to produce lots of log output while debugging, rather than stepping through with a debugger. So I appreciate the interest in __LINE__, etc. However, in your previous request (4411102), you said "How many times do you find yourself grepping through files trying to find where a message came from." This implies that exposing line numbers is better than logging informative text strings; I do not buy this argument. Now, the VM has source file and line number information, and exposes it when a Throwable is created. We're unlikely to add another reflective interface to the VM to obtain the same info. Consequently, literals in the language would merely compile to creating a Throwable then parsing getStackTrace(). (This may be slow, but I'm not concerned about debug code being fast.) Since you can call Throwable.getStackTrace() yourself, I see no need to add __LINE__-style literals to the JLS. Further, there are no comparable literals in the JLS today.
17-11-2006