Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Compilation - Is there a way to tell the compiler to ignore parts of my code?

I maintain a Java Swing application.

For backwards compatibility with java 5 (for Apple machines), we maintain two codebases, 1 using features from Java 6, another without those features.

The code is largely the same, except for 3-4 classes that uses Java 6 features.

I wish to just maintain 1 codebase. Is there a way during compilation, to get the Java 5 compiler to 'ignore' some parts of my code?

I do not wish to simply comment/uncomment parts of my code, depending on the version of my java compiler.

like image 893
Merv Avatar asked Sep 16 '08 16:09

Merv


People also ask

Does Java have conditional compilation?

So Java does have its own conditional compilation mechanism. What if we want to use debugging code similar to this, but have the condition applied at runtime? We can use System. properties (Section 2.3) to fetch a variable.

What is Xlint?

-Xlint. Enable all recommended warnings. In this release, enabling all available warnings is recommended. -Xlint:all. Enable all recommended warnings.

Does whitespace matter Java?

In Java, white space does not matter. In fact, Java regards the following program as being identical to that of Figure 1.2. While humans may not care, however, white space can be very useful to humans in indicating the structure of a program, particularly if used systematically.

Does Java have Ifdef?

Java does not include any kind of preprocessor like the C cpp preprocessor. It may seem hard to imagine programming without #define, #include, and #ifdef, but in fact, Java really does not require these constructs.


1 Answers

The suggestions about using custom class loaders and dynamically commented code are a bit incredulous when it comes to maintenance and the preservation of the sanity of whichever poor soul picks up the project after you shuffle to pastures new.

The solution is easy. Pull the affected classes out into two separate, independent projects - make sure the package names are the same, and just compile into jars that you can then consume in your main project. If you keep the package names the same, and the method signatures the same, no problems - just drop whichever version of the jar you need into your deployment script. I would assume you run separate build scripts or have separate targets in the same script - ant and maven can both easily handle conditionally grabbing files and copying them.

like image 85
Ian Avatar answered Oct 24 '22 10:10

Ian