Conditional Compilation in Java

We’ve hit something of a stumbling block with Hecl, and it’s proving tricky. Far from being “write once run anywhere”, Java has many versions, when you take into considerations the wide array of devices that run one of several configurations roughly lumped together as “J2ME”. My cell phone’s implementation doesn’t have floats, for instance. This necessitates that the J2ME and J2SE versions contain different code, but Java doesn’t make this easy, because there are no macros or other ways of conditionally compiling code.

The solution we’ve adapted so far is problematic because it’s not very fine grained: we have two separate directories, one for ‘core’ Hecl (sans floats) and another that contains the exact same classes, but with floating point. For instance:

BasicMathCmd.java – no floating point

BasicMathCmd.java – the floating point version.

As you can imagine, this means that code is replicated in two places, and each change has to be done twice, carefully, keeping track of the actual differences between the two files. This can only get worse as the code grows.

I’ve seen some hacky ways of doing macros and things like that, but they don’t look very satisfying. I’m too much of a Java novice to think of anything better than what we’ve got, though.

Update 2006-04-20: we chose to use Antenna

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s