MRC
MRC

Reputation:

Java solution for C++ style compiler directive

I have a Java array:

    String[] myArray = {"1", "2"};

Depending on a condition that is known at compile time I would like to assign different values:

    String[] myArray = {"A", "B", "C"};

In C++ I would use something like

#ifdef ABC
  // ABC stuff here
#else
  // 123 stuff here
#endif

but what to do in Java?

Upvotes: 9

Views: 2889

Answers (7)

Colin
Colin

Reputation: 1

You can't really do it...the best way would be to use a template engine...even some compilers will put the code into the end class file - the better ones would remove the code if it is not required...You must make the constants final and also you must set them to an absolute value for the compiler to be able to remove the code.

public static final boolean ABC = true // This will be enough for the compiler to remove code

public static final boolean ABC = [any method call] // This is not enough to work and will leave the code in there

Again, some compilers may still put the code in there even though it is unreachable (that is why some IDE's will warn you that the code is unreachable so that you remove the code that is not needed)

However,

Directives are nasty and most likely you need to be using an implementation of an interface instead of a directive! good for low level coding when needed (to minimize memory foot print) - certain special devices like pace makers and the like but why would you want to use them in java?

Upvotes: 0

miniMe
miniMe

Reputation: 33

If you use a var/const as a custom compiler directive in Java don't make it final. You might get lots of warnings because of conditions being "always true" and stuff.

public static boolean ABC = true; // or false

Upvotes: 1

Jeff Knecht
Jeff Knecht

Reputation: 2548

Use a template engine (something like Velocity) to execute a preprocessing step. Your build script could run the template applying properties from the build environment to output Java source code that is ultimately compiled. This is, essentially, what C/C++ preprocessors do.

My guess is that someone has already coded an ant task to do this, but I'm too lazy to Google it for you.

Upvotes: 1

stepancheg
stepancheg

Reputation: 4276

String[] myArray;

if (ABC)
myArray = ...
else
myArray = ...

ABC is static final variable, JVM is required to inline condition check.

Upvotes: 1

jjnguy
jjnguy

Reputation: 138922

In Java you would need to do that at runtime.

String[] myArray;
if (something)
    myArray = new String[]{"A", "B"};
else
    myArray = new String[]{"A", "B, "C"}

This is going to be the most similar to C++ code. And, if your condition is guaranteed to be true at compile time, Java will optimize out the call.

There are other options available, but they will look nothing like a C++ version.

Upvotes: 3

dfa
dfa

Reputation: 116372

class Foo {

   static final boolean ABC = true;

   public void someMehod() {
       if (ABC) {  // #ifdef ABC

       } else {    // #else

       }           // #endif
   } 
}

since ABC is both static and final the compiler evaluates it at compile-time, effectively acting like a pre-processor.

Upvotes: 13

Gregory Mostizky
Gregory Mostizky

Reputation: 7261

One approach is to re-generate a Constants.java file in the first phase of your build script (ant,maven or whatever else you use). Depending on how complex that file is you can do it manually or use something more heavy like Velocity.

I used something similar in the past to put version and build info inside java classes.

Upvotes: 1

Related Questions