1

So the idea is to support #Macro in java. Not only in the final result but also during compile time. So all the injections and code completion will work properly.

To add the macro functionality to java we can use CPP.

src/Test.java:

#define READINT (new java.util.Scanner(System.in).nextInt())

class Test {
    public static void main(String[] args) {
        int i = READINT;
    }
}

cpp command:

$ cpp -P src/Test.java preprocessed/Test.java

Result:

class Test {
    public static void main(String[] args) {
        int i = (new java.util.Scanner(System.in).nextInt());
    }
}

Compile:

$ javac preprocessed/Test.java

Tnx to aioobe for the original idea.

How ever this will only allows us to compile macros, we need a way to use it in code time, so if I do some thing like this:

#define PASTE2(a, b) a##b
#define PASTE(a, b) PASTE2(a, b)
#define setParameter(type,name) private type name;public type PAST(get,name)(){return name;}

Usage:

setParameter(int, value)

Will produce:

int value;public int getvalue(){return value;}

I want the compile to autocomplete this method. So it will know the class contains a getter getvalue() from type int

Also there are java annotations that can work in compile time, but I am not sure that it's possible with them.

Community
  • 1
  • 1
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
  • This sounds to me like you would need the IDEs themselves to support macros... – awksp Jun 03 '14 at 20:33
  • 3
    One of the reasons that Java programs are relatively easy to maintain is the _lack_ of a preprocessor as the code is guaranteed to behave as you would expect. This is deliberately. That said, you should look into a project Lombok. http://projectlombok.org – Thorbjørn Ravn Andersen Jun 03 '14 at 20:36
  • Nonono. Please use OOP. No code generation. If you need to have a common method in a bunch of places just use inheritance. – dimoniy Jun 03 '14 at 20:44
  • @ThorbjørnRavnAndersen please add this as an answer and explain to us this even possible. – Ilya Gazman Jun 03 '14 at 20:51
  • 7
    Java programs manage to be just as bad as C programs even without a preprocessor. – Puppy Jun 03 '14 at 20:56
  • Wow, it's not like anyone's recommending the overuse/abuse of OOP at all... – Lightness Races in Orbit Jun 03 '14 at 21:00
  • @dimoniy Don’t use inheritance for code reuse. In most cases an actual design suffices. –  Jun 03 '14 at 21:01
  • 3
    If you want a language that runs on the JVM and has a powerful macro system, you should look at [Clojure](http://clojure.org/). –  Jun 03 '14 at 21:02
  • @rightfold Lisp macros are quite different from preprocessor macros. – Thorbjørn Ravn Andersen Jun 04 '14 at 05:54
  • @ThorbjørnRavnAndersen In that they are actually sane and powerful, indeed. Never use preprocessor macros. –  Jun 04 '14 at 07:55
  • @rightfold and they are two very different things. Lisp Macros are not a replacement for CPP Macros. Consider having a look at http://www.paulgraham.com/avg.html – Thorbjørn Ravn Andersen Jun 04 '14 at 09:23
  • @Ilya_Gazman I'll leave writing a full answer to others. If what you want is simply to have shorthand notation for getters/setters then look at Lombok. – Thorbjørn Ravn Andersen Jun 04 '14 at 09:47
  • @ThorbjørnRavnAndersen you know there is no thank you here, my of doing this is approving your answer. That what I wanted to tell you before: Tnx, this is it! – Ilya Gazman Jun 04 '14 at 13:11

0 Answers0