I want to be able to support multiple versions of Java ME without having to have multiple builds. I already know how to detect the profile/configuration/supported JSRs. My problem is that knowing whether the JSR is supported at run time doesn’t allow me to use all the features as Java-ME does not provide support for reflection.
For if I call a function added in a later version anywhere in the code – even a location that will never be run, then this could cause an error during resolution on some JVMs. Is there any way round this?
Related Questions
If you only need to access the class
Cthrough an interface which you know you will have access to, then it is simple enough:If
Cdoes not have an interface that can be used, then we can instead create a wrapper classSthat will be a member of the interface instead.Shas a static block so that an exception is thrown during class resolution ifCis unavailable. Immediately after loading the class, we callforceExceptionIfUnavailableto make sure that the static block is run immediately. If it doesn’t crash, then we can use the methods inSto indirectly use classC.Alternatively, we can use the method here:
Basically, you create a new package
P, with a public abstract classAand a concrete subclassSprivate to the package.Ahas a static methodgetSthat returns an instance ofSornullif an exception is thrown during instantiation. Each instance ofShas an instance ofCso it will fail to instantiate whenCis unavailable – otherwise it will succeed. This method seems to be a bit safer asS(and hence all theCAPIs) are package private.