Package level annotations

While setting up JAXB2 for converting object graphs to XML I came across a not-so-nice part of the annotations specification. While looking for a way to define a package level annotation (never needed to do this before) I found the solution a bit surprising:

To define a package-level annotation one has to create a file called 'package-info.java' in the root of the package and put the annotations on the package:

JAVA:
  1. @XmlJavaTypeAdapter(type = AVTimePoint.class, value = AVTimePointAdapter.class)
  2. package nl.cinema.domain.media;
  3.  
  4. import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

'package-info.java'?? Grrr... since it doesn't contain a type definition it doesn't even pop-up in Eclipses' source browser!

But hey... need to put them somewhere!

When the compiler encounters package-info.java file, it will create a synthetic interface, package-name.package-info. The interface is called synthetic because it is introduced by the compiler and does not have a corresponding construct in the source code. This synthetic interface makes it possible to access package-level annotations at runtime. The javadoc utility will use the package-info.java file if it is present, instead of package.html, to generate documentation.... I guess I should have known that ;) I do however still prefer the package.html convention.


0 Responses to “Package level annotations”

  1. No Comments

Leave a Reply





About

Welcome to the weblog of Peter Maas. Here you'll find various posts related to stuff I like (like my kids and espresso) and stuff I do (like developing software).

JavaOne 2008 Pictures


Greenland Okke en Rudie golden_gate_warning_sign pub Stage being build in the nearby park Scribbled Sun Logo Golden Gate Moscone Center - JavaOne sea_lion Acme Anvile at CommunityOne Keynote alcatraz javaone2008 keynote nearby hotel Joshua Bloch at JavaOne2008 Tim Bray introducing the (J)Ruby panel Community One Keynote smashmouth Rudie Stretched Limo Cable Car line
View more photos >

Categories



Meld u aan voor PayPal en begin direct met het accepteren van creditcardbetalingen.