Java conditional compilation: how to prevent code chunks from being compiled? -
my project requires java 1.6 compilation , running. have requirement make working java 1.5 (from marketing side). want replace method body (return type , arguments remain same) make compiling java 1.5 without errors.
details: have utility class called os
encapsulates os-specific things. has method
public static void openfile(java.io.file file) throws java.io.ioexception { // open file using java.awt.desktop ... }
to open files double-click (start
windows command or open
mac os x command equivalent). since cannot compiled java 1.5, want exclude during compilation , replace method calls run32dll
windows or open
mac os x using runtime.exec
.
question: how can that? can annotations here?
note: use ant, , can make 2 java files os4j5.java
, os4j6.java
contain os
class desired code java 1.5 , 1.6 , copy 1 of them os.java
before compiling (or ugly way - replace content of os.java
conditionally depending on java version) don't want that, if there way.
elaborating more: in c use ifdef, ifndef
, in python there no compilation , check feature using hasattr
or else, in common lisp use #+feature
. there similar java?
found this post doesn't seem helpful.
any appreciated. kh.
nope there isn't support conditional compilation in java.
the usual plan hide os specific bits of app behind interface
, detect os type @ runtime , load implementation using class.forname(string)
.
in case there no reason why can't compile both os*
(and infact whole app) using java 1.6 -source 1.5 -target 1.5
in factory method getting hold of os
classes (which interface) detect java.awt.desktop
class available , load correct version.
something like:
public interface os { void openfile(java.io.file file) throws java.io.ioexception; } public class osfactory { public static os create(){ try{ class.forname("java.awt.desktop"); return new osj6(); }catch(exception e){ //fall return new osj5(); } } }
Comments
Post a Comment