LowerProfile
From APIDesign
Profiles are the best thing in JDK8 (in spite they are not as famous as lambdas). Now when they are here with us, it is essential that we will want our applications, frameworks and libraries to be ready for them. Here is few tricks that will help you refactor your code for JDK8.
Contents |
Being Ready for JDK Profiles
What does it mean: to be ready for profiles? Well, first and foremost, it means to be able to run on smaller profile than the whole JDK. In older JDKs, up to JDK7, one always had the whole rt.jar. As such it did not matter whether your code used pieces from Swing (like ChangeListener) - all the classes were always available.
The situation changes with profiles. The smallest JDK8 profile is compact 1 and be sure, it does not contain a single class from Swing (neither JNDI or XML parsers). But as an owner of a library or running system you may be interested in in such small profile. Why? Because then your library can be used on small devices (phones, Raspberry PI) or in a headless environment (on servers). For example Grizzly was not ready to run on compact 2 up until I submitted a patch which made a dependency on JNDI conditional since Grizzly revision 2.3.2.
Lower Your Profile Patterns
The need to run on smaller profiles is clear. Let's look at patterns how to adapt your API to such limited environment.
Conditional Usage
This is the most trivial change one can make. Not really beautiful, but effective. A form of clueless solution that was used in case of Grizzly as well. If there is a call to an API not always available, surround it with a catch statement:
Object ref = null; try { ref = new javax.naming.InitialContext().lookup("x"); } catch (LinkageError err) { // well, too small of profile. Give up. }
Effective, especially if the amount of such calls is not huge (nobody wants to pollute own code base with linkage catch statements). Requires compilation against the whole JDK and as such it is a runtime-only solution and possibly can regress any time (unless one actually runs tests on the small profile). On the other hand, it does not require any changes to build infrastructure or use of JDK8 during the build. In short this is the most cluelessness solution.