Are there macro facility for Java or C#?

前端 未结 4 467
情话喂你
情话喂你 2021-01-13 13:33

Macros are useful.

Therefore, I occasionally bemoan the absence of macros in Java and C#. Macros allow me to force in-line but allow me the code-manageability of non

相关标签:
4条回答
  • 2021-01-13 14:14

    You can write your own annotations ... http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html

    You could also use a macro language like m4 and then invoke it with make. http://www.gnu.org/software/m4

    You could also use the C preprocessor. The C preprocessor (cpp) is the preprocessor for the C programming language. In many C implementations, it is a separate program invoked by the compiler as the first part of translation. The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if). The language of preprocessor directives is agnostic to the grammar of C, so the C preprocessor can also be used independently to process other types of files

    As far as suggesting tips to the compiler for optimization, Java does not have a facility to force code to be inlined.

    0 讨论(0)
  • 2021-01-13 14:18

    Take a look at project Lombok - http://projectlombok.org/features/Log.html

    It is an annotation library that allows the following code snip-it - which (I believe) is exactly what you are speaking of.

     import lombok.extern.slf4j.Log;
    
     @Log
     public class LogExample {
    
       public static void main(String... args) {
         log.error("Something's wrong here");
       }
     }
    
     @Log(java.util.List.class)
     public class LogExampleOther {
    
       public static void main(String... args) {
         log.warn("Something might be wrong here");
       }
     }
    
    0 讨论(0)
  • 2021-01-13 14:22

    I would recommend trusting the JIT compiler to make the decision for you when it comes to inlining.

    However, if you are just after macros for other purposes, in C#/.NET, there are other options. Much of what can be done with macros for utility purposes can be done via T4 (Text Template Transformation Toolkit). This is the basis for many ORM packages, for example.

    0 讨论(0)
  • 2021-01-13 14:26

    Macros are not part of the standard Java language, and I'm not aware of any macro preprocessor being supported by mainstream Java tools, IDEs and so on. So if you use macros in your Java code you should expect to experience some "pain". For example,

    • Source code debuggers won't let you set breakpoints relative to your original source code.
    • If you share your Java-with-macros code, many Java developers are likely to turn up their noses at it, and/or complain about having to install/use extra tools.

    There are quite a few examples of third-party macro pre-processors for Java; e.g. Jatha, OpenJava, PrintMacroJ, JavaMacros, and so on ... (But have you ever come across a project that uses any of them?)


    Macros allow me to force in-line but allow me the code-manageability of non-macro code.

    True. But the JIT compiler can probably do a better job than you can in determining what should be inlined. It will know (for sure) how big the chunks are, and it will have runtime stats on execution frequency, branch prediction, etc that are not available to you.

    Note that there are some Hotspot JVM tuning options that can influence the optimizer's decisions on inlining; see this page, and scan for "inlin". For instance, there is one that seems to allow you to increase the upper size threshold for an inlined method body.

    0 讨论(0)
提交回复
热议问题