Tuesday, May 15, 2012

Java 8: extensible module definitions with JSON

While the Java Modularity JSR [1] is still AWOL (come on, Oracle, I thought that this JSR was planned to be launched by September 2011!), I did some work in OpenJDK Penrose around changing the Java SE 8 module metadata from the .java/.class format into JSON.

JSON is quite nice because
with JSON, the SE 8 Quick Start module org.astro module definition looks like this:

{
  "module" :
  {
    "name" : "org.astro",
    "version" : "1.2",
    "exports" : [ {"name" : "org.astro"} ]
  }
}

and the same module info file can be expanded to contain OSGi metadata, for example to add a version to the exported package like this:

{
  "module" :
  {
    "name" : "org.astro",
    "version" : "1.2",
    "exports" : 
    [
      { 
        "name" : "org.astro", 
        "org.osgi.version" : "1.2.3"
      }
    ]
  }
}

Any OSGi metadata can be added to the JSON module descriptor. I've added a simple test to the Penrose codebase to give an idea what this looks like: hello-json-custom.sh.

This works today in OpenJDK project Penrose. The codebase still supports module-info.java, to allow a gradual migration to JSON-based metadata, however I'd like to see the module-info.java/.class support go away ultimately.

While Jigsaw/Penrose are two variants on a module system for Java SE, other ones are clearly possible. Once the modularity JSR is up in action, it should seriously consider OSGi as a module system for Java SE 8. Apache Harmony has clearly demonstrated in the past that modularizing the Java platform using OSGi metadata works quite well. OSGi has proven to be an excellent module system for Java for the last 12 or so years, so it would be foolish not to take that experience on board...

On the lack of Modularity JSR, I think at that at this stage we can expect that it will delay the release of Java 8, given that modularity is such an important building block for Java, and is certainly not something that can be rushed through.

[1] 'Java Platform Module System' JSR as mentioned in the Java SE 8 umbrella JSR.