Reputation: 90180
I understand the purpose of class annotations, thanks to How and where are Annotations used in Java?. What is the purpose of package annotations, as described in this blog post and §7.4.1 of the Java Language Specification?
Why would you want to associate metadata with a package? What kinds of things could you do?
Upvotes: 41
Views: 16046
Reputation: 6627
Upvotes: 45
Reputation: 7057
Test metadata -- that is metadata around test packages (unit tests or otherwise). You can ascribe various pieces of test metadata that are appropriate at the package level, such as: features, owners, versions, bugs/issues, etc. These could be refined at the class or method level, but having package-level definitions or defaults could be handy for brevity. I've utilized a variant of this approach (before the benefit of annotations).
A similar argument could be made for generalized code metadata around the same sorts of things: features, ownership, defects, historical information, etc.
Upvotes: 0
Reputation: 308259
JAXB for example allows most annotations that are normally used on a type to be equally well applied to a package. The meaning in that case would be to specify the default for all classes in that package.
For example, if you want all properties of all classes in a package that are exposed via getter/setters to be mapped in XML you could specify @XmlAccessorType(XMLAccessType.PROPERTY)
on each class or simply specify it on the package.
Upvotes: 2
Reputation: 74800
This is not the real purpose, but I'm using them as a workaround to avoid recompilation of the package-info.java files.
The problem is that javac
(and the Ant task <javac>
) creates no class file for the package-info.java if there is only documentation (the reason for their existence) and the package bla;
statement, and that the ant task recompiles every file for which there is no (or an older) corresponding .class
file.
Adding a dummy annotation there (like SuppressWarnings
) had the effect that a package-info.class
is produced and thus the file is not recompiled until changed again.
(Ant 1.8.0 solved this by creating an empty package-info.class, even if there was no annotation, but I'm using an older ant
here.)
Upvotes: 1
Reputation: 1429
Two reasons that I can think of:
Upvotes: 4
Reputation: 262834
I suppose @Deprecated
would make sense. And maybe something like @Generated
if the whole package was generated by some tool from non-Java source. Or @Internal
if this package is not part of a public API.
Maybe OSGi tools (where you need to declare the versions of your packages, and the packages you depend on) could make use of this, too.
Has anyone seen those in the wild?
Upvotes: 7