Java News from Thursday, September 11, 2008 has published my latest article, The Open Road: javax.annotation. Java 5 introduced annotations to the language. Since then, several important third-party libraries and frameworks have taken advantage of them to good effect, including Guice, Hibernate, and JUnit 4. Libraries designed around annotations are often simpler and easier to work with than the equivalent libraries that use reflection or naming conventions to achieve the same result.

However, the core of Java itself has made relatively little use of annotations. In fact, only three are built into the language as of Java 6: @Override, @Deprecated, and @SuppressWarnings. These are certainly useful, but they hardly exhaust the space of information we may wish to annotate in our Java programs.

The JSR 305 effort being led by Bill Pugh of the University of Maryland plans to change this in Java 7, and dramatically increase the number of annotations used in standard Java code. By adding annotations to classes, methods, fields, and variables, you'll be able to tell static and dynamic analysis tools how you think the code is supposed to behave. Then the tools can warn you when they suspect the code will behave in some other fashion. This improves on the current situation, where static analysis tools need to guess not only what the code does, but what it's supposed to do. Such tools will still generate many false positives; but they'll find many more true positives—that is, genuine errors—in annotated code than in non-annotated code.

Continued in the article...