Conditional Compilation

Hi

is there any way to use conditional compilation in such a way that in the true condition i have compilable code

and in the false condition i have a code which is not compilable at this moment so that the compiler just ignores it completely (Like in C/C++) and not give any compile time error

Thanks in advance

Regards

Varun

[366 byte] By [varun_nayara] at [2007-10-2 2:05:10]
# 1
Yes, JDK 1.5 introduced Metadata: [url= http://java.sun.com/developer/technicalArticles/releases/j2se15/]J2SE 5.0 in a Nutshell[/url]
MartinHilperta at 2007-7-15 19:46:34 > top of Java-index,Developer Tools,Java Compiler...
# 2
You can write "meta java source" with # directivesand pass it through a pre-processorto generate your java source.
tschodta at 2007-7-15 19:46:34 > top of Java-index,Developer Tools,Java Compiler...
# 3

> You can write "meta java source" with # directives

> and pass it through a pre-processor

> to generate your java source.

Hi

Thanks for the reply

can u elaborate on it a bit more or if u have an example

Thanks in advance

Regards

Varun

varun_nayara at 2007-7-15 19:46:34 > top of Java-index,Developer Tools,Java Compiler...
# 4

Of course conditional compilation was left out of java (like multiple inheritance) to make things easier. Gosling merits a large thanks there.

The cases for a kind of conditional compilation can often be better done with

dependency injection or another form of dynamic programming.

You provide an interface XxxService the application uses.

You implement classes DummyXxxServiceImpl and SmartXxxServiceImpl.

Via a config file (properties of xml) you dynamically by java.lang.reflect create either an object of DummyXxxServiceImpl or the latter.

The springframework is which utilises this technique.

joop_eggena at 2007-7-15 19:46:34 > top of Java-index,Developer Tools,Java Compiler...
# 5
> can u elaborate on it a bit more or if u have an exampleI re-read your original post, your question seems to have been answered already.Can you clarify exactly what you would like someone to elaborate on,and exactly what you would wish an example to show?
tschodta at 2007-7-15 19:46:34 > top of Java-index,Developer Tools,Java Compiler...
# 6

http://forum.java.sun.com/thread.jspa?threadID=633884&messageID=3678519

Nobody prevents you from letting a Java source through your favourite preprocessor.

xx.c

public class xx {

private final static String when = __DATE__ ;

public static void main(String args[]) {

System.out.println(when);

}

}

posman@linux:~/ivan> gcc -E -P xx.c >xx.java

posman@linux:~/ivan> javac xx.java

posman@linux:~/ivan> java xx

Jun 7 2005

BIJ001a at 2007-7-15 19:46:34 > top of Java-index,Developer Tools,Java Compiler...