I have a Java application which runs on an embedded device. Because different devices run different versions of the device SDKs I must build against ~5 different device SDK combinations.
One of these combinations does not support a specific method on an existing object and omits another object entirely from the SDK.
I use this method and object in my program, but only in a certain configuration, so I would like to just fallback to another configuration on devices which do not support it.
I would be happy to do this fallback behavior at compile or run time.
What’s going to be the easiest way to conditionally remove this code?
The code is otherwise identical so I would prefer not to create two separate branches of code for the two SDKs.
I build my application using an Ant script.
My application has to build against a rather old version of the JDK (1.1.8/1.2) if that’s relevant.
One idea is to create two classes with the same interface, a full implementation and a stub, both with an isSupported() method and having all other methods in the stub throw an UnsupportedOperationException. Then I could conditionally include the correct class during compilation in my Ant script.
I elected to go with this rather than the more dynamic approach @Stephen C because it works perfectly well for our purposes and our access to the device (including the device filesystem) is quite limited, making it difficult to deploy multiple jars, set classpaths, etc.
What I ended up doing was as follows:
mypackagenamestuband copy the files to be stubbed into it. Make sure that your IDE does not change the package declarations for the files to be stubbed.omit.unsupportedmode(paraphrased) and set it to true where appropriate.javac, e.g.**/MyPackageStub/**or**/MyPackage/**based onomit.unsupportedmode.