views:

587

answers:

10

Hi all,

I'm a longtime C++ programmer, new to Java. I'm developing a Java Blackberry project in Eclipse. Question - is there a way to introduce different configuration sets within the project and then compile slightly different code based on those?

In Visual Studio, we have project configurations and #ifdef; I know there's no #ifdef in Java, but maybe something on file level?

+2  A: 

No, Java doesn't have an exact match for that functionality. You could use aspects, or use an IOC container to inject different implementation classes.

Nathan Hughes
+1 for mentioning implementation class
extraneon
+1  A: 

Can one call that a poor mans ifdef: http://www.javapractices.com/topic/TopicAction.do?Id=64?

The MYYN
A: 

You could use maven's resource filtering in combination mit public static final fields, which will be indeed get compiled conditionally.

private static final int MODE = ${mode};

...

if (MODE == ANDROID) {
    //android specific code here
} else {

}

Now you need to add a property to your maven pom called "mode", which should be of the same value as your ANDROID constant.

The java compiler should (!) remove the if and the else block, thus leaving your android code.

Not testet, so there is no guarantee and i would prefer configuration instead of conditional compilation.

Willi
This is really not suitable for significant amounts of code. Java has interfaces, implementations and inheritance for just this kind of problems.
extraneon
Agreed. This is why i said i would prefer configuration.
Willi
A: 

You can integrate m4 into your build process to effectively strap an analogue to the C preprocessor in front of the Java compiler. Much hand-waving lies in the "integrate" step, but m4 is the right technology for the text processing job.

seh
Java has other answers to the same problems. If you have to fiddle with files for this kind of most common of problems you really are doing something wrong in the design.
extraneon
Note the OP's request: *[I]s there a way to introduce different configuration sets within the project and then compile slightly different code based on those?* To "compile slightly different code", one must feed different source code to the compiler. Yes, I've read about the static final boolean trick, but I don't think that's what the OP was after.
seh
A: 

In eclipse you could use multiple projects

  • Main (contains common code)
  • Version1 (contains version1 code)
  • Version2 (contains version2 code)

    1. Main -> Select Project->Properties->Java Build Path->Projects tab
    2. Select Add...
    3. Add "Version1" xor "Version2" and OK back to the workspace.

Version1 and Version two contain the same files but different implementations. In Main you normally write e.g.

import org.mycustom.Version;

And if you included Version1/Version2 project as reference it will compile with the Version.java file from Version1/Version2 project.

jitter
+3  A: 

You can set up 'final' fields and ifs to get the compiler to optimize the compiled byte-codes.

...
public static boolean myFinalVar=false;
...
if (myFinalVar) { 
 do something ....
 ....
}

If 'myFinalVar' is false when the code is compiled the 'do something....' bit will be missed out of the compiled class. If you have more than one condition - this can be tidied up a bit: shift them all to another class (say 'Config.myFinalVar') and then the conditions can all be kept in one neat place.

This mechanism is described in 'Hardcore Java'.

http://www.amazon.co.uk/Hardcore-Java-Robert-Simmons/dp/0596005687/ref=sr%5F1%5F1?ie=UTF8&s=books&qid=1261064385&sr=8-1-spell

[Actually I think this is the same mechanism as the "poor man's ifdef" posted earlier.]

monojohnny
+2  A: 

you can manage different classpath, for example, implement each 'Action' in a set of distinct directories:

dir1/Main.java
dir2/Action.java
dir3/Action.java

then use a different classpath for each version

javac -sourcepath dir1 -cp dir2 dir1/Main.java

or

javac -sourcepath dir1 -cp dir3 dir1/Main.java
Pierre
+3  A: 

In JDK6, you can do it by using Java's ServiceLoader interface. Check it here.

Shamik
this is a nice feature which I'd never heard of until now.
Joel
this is really not so famous because of so much dominant players are present in the open source market. But I found this extremely useful considering the fact you do not want to use full blown IOC containers and just want to load different implementation of the same interface.
Shamik
This isn't going to work for him as his target is BlackBerry which is J2ME.
Jeremy Raymond
+1  A: 

Besides Maven, Ant and other build tools that provide similar functionality, one would rather build interfaces in Java and switch the implementations at Runtime.
See the Strategy Pattern for more details

In opposite to C/C++ this will not come with a big performance penality, as Javas JIT-compiler optimizes at runtime and is able to inline this patterns in most cases.
The big pro of this pattern is the flexibility - you can change the underlying Implementation without touching the core classes.

You should also check IoC and the Observer Pattern for more details.

Hardcoded
+1  A: 

If you want this specifically for BlackBerry, the BlackBerry JDE has a pre-processor:

You can enable preprocessing for your applications by updating the Eclipse™ configuration file.

In C:\Program Files\Eclipse\configuration\config.ini, add the following line: osgi.framework.extensions=net.rim.eide.preprocessing.hook If you enable preprocessing after you have had a build, you must clean the project from the Project menu before you build the project again.

Then you can do things in the code like:

//#ifdef SOMETHING
// do something here
//#else
// do something else
//#endif

For details see Specifying preprocessor defines

Jeremy Raymond