JSR 199: The Compiler API
The addition of the compiler API (JSR 199) into Java SE 6 presents new possibilities within the language. For instance, my previous article
mentioned a small project that used this API. The code I wrote generated and compiled classes used to access an application's database tables. Since then, another interesting implementation of this API has caught my attention. It's called Project Gosling
Named after the main character in the book The Ugly Duckling, Gosling rewrites Ant to use Java code in place of XML files for performing Ant-related tasks, such as project builds. Gosling, itself written in Java, leverages the Compiler API to be able to compile and then execute Java-based Ant scripts. Listing 2 contains the Ant-script Java file to build Gosling.
The compiler API consists of new javax packages, such as javax.lang.model, javax.annotation, and javax.annotation.processing. The ability to use Gosling and Java build scripts greatly enhances the Ant build environment. For Java developers, using the Java programming language to perform build tasks should be more natural than using XML. Simply checking dependencies, performing looping operations, and making decisions are much more obvious and understandable in Java.
Beyond database-access code and Java build files, you can use the compiler API to implement dynamic language-processing tools, such as those that compile JSP files, and other dynamic languages such as PHP or JRuby. Sun declares that this API is not meant for the everyday compiler, but I submit that all developers should think of ways to extend the power of Java through the use of Java itself and the new compiler API.
The All-Purpose Web Development Language
In February, I declared Java SE 6 a desktop winner. I now would add that it will empower everyday developers to build better tools and frameworks that will catapult Java further as the language for serious enterprise Web development. The addition of the scripting API and the compiler APIcombined with becoming a GPL v2 licensed open-source projectmeans that Java will remain the right tool for any Web-related job. The choice to integrate with otherwise alternative languages such as Ruby ensures that Java will be only augmented by them, as opposed to replaced by them.