5 Replies Latest reply: Jul 15, 2011 5:58 PM by 802316 RSS

    Some Reflections Information may be Compile Time information in javac.

    866364
      Hi All,

      I have recently look at a rather comprehensive language extension from C++, It is call D.
      http://www.digitalmars.com/d/2.0/lex.html

      Special Tokens
      __FILE__
      __LINE__

      Assuming there is NO syntax error in the source code and every thing compiled nicely.
      Some of the Runtime Class Information are rather static and may be ALSO
      implemented by using Compiler AST information. The advantages of using such syntax clearly reduces extra duplicated code everywhere
      that need a developer to process the Reflection API.n Since these information are frequently used by developer from various library.

      I have also though about how to make some of the reflection API easy to be used by a junior developer and gave this comment.

      These 2 tokens are very useful for debugging and also trouble shooting during exceptions time and even implementing logging api similar to log4j:
      I do understand that this are compile time information and they are static.

      __FILE__ // prints something like D:\User\Project\...\MyMod.d
      I do NOT like this full path format because it might give hacker some idea when of server directory layout structure for webbase cgi / layout, accidentally by junior developer because of mis handled exceptions.
      __LINE__ // some line number.

      How about keeping those 2 and also adding some new compile time information's tokens:
      1) __FILENAME__ // prints only ByteArrayInputStream.java
      2) __MODULE__ // prints the full module name. eg: java.io
      3) __FUNCNAME__ // prints the function name. eg: read(byte[],int,int)
      4) __BLOCKNAME__ // perhaps another name, but prints class/struct/interface/template... name. ByteArrayInputStream. If Anonymous, print parent type.
      5) __DEBUGLINE__ // prints __MODULE__ + __BLOCKNAME__ + __FUNCNAME__ + __LINE__

      The reasons for those are:
      1) to make the life of coder simple instead of having to every time call a template function/function to trim those information.
      2) Some time the code does gets move around within a large project due to re-factoring and remodeling. Having only the file name & line number might loose the

      I am rather sure that the compiler will have some Abstract Syntax Tree in memory to map value for such tokens.

      RUNTIME SPECIAL TOKEN
      It would be even more cool if there is some immutable runtime special tokens:
      1) __LOGIN__ // Current user login id, eg: johnchia
      2) __UID__ // User login number, eg: 0, 304,1234,...
      3) __PID__ // Current program process id
      4) __PPID__ // Parent process id
      5) __STIME__ // Start time, date time,2011-02-23 14:45, to see if there is a long over due program.
      6) __CMDLINE__ // prints full path name C:\Program Files\Notepad++\notepad.exe

      These runtime tokens will be very useful for developer to do secure coding to check for some runtime Access Control Information or preventing denial of
      service and maybe anti buffer over flowing attacks.

      Perhaps the runtime special information are already available. If not, that might be in the future version.

      Edited by: user5167423 on Jun 3, 2011 2:36 PM
        • 1. __FILE__ and __LINE__ macros
          BIJ001
          You do not need D to have the __FILE__ and __LINE__ macros:

          http://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html
          • 2. Re: __FILE__ and __LINE__ macros
            EJP
            You don't need an Abstract Syntax Tree either, for any of them.
            • 3. Re: Some Reflections Information may be Compile Time information in javac.
              jtahlborn
              matt_idea wrote:
              RUNTIME SPECIAL TOKEN
              It would be even more cool if there is some immutable runtime special tokens:
              1) __LOGIN__ // Current user login id, eg: johnchia
              2) __UID__ // User login number, eg: 0, 304,1234,...
              3) __PID__ // Current program process id
              4) __PPID__ // Parent process id
              5) __STIME__ // Start time, date time,2011-02-23 14:45, to see if there is a long over due program.
              6) __CMDLINE__ // prints full path name C:\Program Files\Notepad++\notepad.exe

              These runtime tokens will be very useful for developer to do secure coding to check for some runtime Access Control Information or
              all of this information (well, some is system type specific) is available in the jvm. you could write your own SecurityManager which utilized this information. and, you'd have to do this within a SecurityManager because a jvm is inherently insecure without one.
              preventing denial of service
              not sure what any of this does to prevent DOS.
              and maybe anti buffer over flowing attacks.
              java proper doesn't suffer from buffer overflow attacks. (there have been issues within the jvm impl itself, but that's a separate issue).

              Edited by: jtahlborn on Jul 14, 2011 12:41 PM
              • 4. Re: Some Reflections Information may be Compile Time information in javac.
                EJP
                One of the things I love about Java is that it doesn't have any of this crud. Magic constants that are really variables, or indeed method calls in many cases. Most of the information listed here is available already one way or the other via proper method calls.

                And all this peripheral stuff about ASTs and denial of service and buffer overflows is just flummery. I'm not sure the OP really knows what he's talking about frankly.
                • 5. Re: Some Reflections Information may be Compile Time information in javac.
                  802316
                  In C/C++ most of this information is only available at compile time, and all the optimisation is only performed by the compiler.

                  However in Java, this information is available at runtime when most of the optimisation is performed (by the JIT) The javac compiler does next to no optimisations.