Is there a macro tool for Java or C #?

Macros are useful.

Therefore, I sometimes complain about the lack of macros in Java and C #. Macros allow me to force embed, but allow me to control non-macro code.

Is there any Java / C # based project / product somewhere where you can use macros efficiently or specify an inline extension.

I think of something like

@macro public void hello (int x) {...}

or when I call the method, the @inline annotation preceding the call will cause the called method to be inserted into the line.

or, if I need to know that I just have to trust the compiler to make the best decision for me, which in the best case analysis can lead to a call.

I hope this question does not lead to a discussion of pro / cons / macro utility.

+4
source share
4 answers

Macros are not part of the standard Java language, and I don’t know which macro preprocessor is supported by the main Java tools, IDEs, etc. Therefore, if you use macros in your Java code, you should expect some β€œpain”. For instance,

  • Source code debuggers won't let you set breakpoints relative to source code.
  • If you share your Java code with a macro, many Java developers are likely to cuddle in them and / or complain about the need to install / use additional tools.

There are many examples of third-party preprocessors 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 embed, but allow me to control non-macro code.

True But the JIT compiler can probably do a better job than you can in determining what should be inlined. He will know (for sure) how big the chunks are, and he will have runtime statistics at the frequency of execution, branch prediction, etc., which are not available to you.

Please note that there are some Hotspot JVM settings that may affect optimizer decisions when embedding; see this page and scan for "inlin". For example, there is one which, apparently, allows you to increase the upper size threshold for the body of the inline method.

+4
source

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

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

+7
source

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

This is an annotation library that allows you to use the following snip-it code - which (I believe) is exactly what you are talking about.

import lombok.extern.slf4j.Log; @Log public class LogExample { public static void main(String... args) { log.error("Something wrong here"); } } @Log(java.util.List.class) public class LogExampleOther { public static void main(String... args) { log.warn("Something might be wrong here"); } } 
+2
source

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

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

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

As for compiler hints for optimization, Java does not have the ability to force code to be inserted.

+1
source

Source: https://habr.com/ru/post/1307204/


All Articles