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


nearby hotel Tim Bray introducing the (J)Ruby panel javaone 2008 goodybag javaone2008 keynote Community One Keynote Moscone Center - JavaOne Rudie Greenland pub Cable Car line alcatraz golden_gate_warning_sign Java + You on a cab Acme Anvile at CommunityOne Keynote Stage being build in the nearby park Golden Gate Hotel room Joshua Bloch at JavaOne2008 sea_lion Scribbled Sun Logo
View more photos >

Categories



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