Bnd is now Java5 aware

Update: Turns out not long after I wrote this, Bnd did gain awareness of Java 5 constructs. I found this out recently when rebuilding some code for the first time since late 2007. Maven as it is wont, updated its plugins to the latest versions, including the maven-bundle-plugin (which uses Bnd) and code which has not been changed in over a year suddenly stopped building. The Culprit? Undeclared dependencies on annotations, which previously went unnoticed by Bnd, were now being spotted by Bnd and duely reported as errors.

Peter Krien’s Bnd utility is a nice piece of work, one feature is that it can pretty accurately compute the correct set of packages for the Import-Package directive of a bundle. Just be aware that it is designed for Java 1.4 and earlier and therefore does not examine new language constructs introduced in Java5 such as annotations and static imports.

If you forget this then you won’t see any problems until run-time (in the case of annotations with run-time retention), at the time when whatever introspection code consumes your annotated classes. Since the packages where the annotations were declared were omitted from the Import-Package declaration they will not be visible in the bundle’s class loading scope. Rather than failing to load the annotated class the JVM will instantiate the class as normal, but omit the annotation reflection data, i.e. Class.getAnnotation() will return null.

The solution is of course to add the packages where the missing annotations are declared to the Import-Package directive, you need to use an explicit package name, a wild-card pattern will not suffice (since Bnd attempts to refine all wild card patterns down to the subset of actual uses).I generally use a statement something like:

Import-Package: com.foo.annotations.pkg, *

This makes Bnd add the explicit import for the missing annotation package and leaves it to compute all other imports for you

Advertisements