I'm trying to add package level annotations but I don't have a clue on how to do it. Examples are appreciated.
Creating a package-info file is fairly simple: we can create it manually or seek IDE help for generating the same. In IntelliJ IDEA, we can right-click on the package and select New-> package-info. java: Eclipse's New Java Package option allows us to generate a package-info.
package-info. java is a way to apply java annotations at the package level. In this case Jaxb is using package-level annotations to indicate the namespace, and to specify namespace qualification for attributes (source). Follow this answer to receive notifications.
The package-info. java is a Java file that can be added to any Java source package. It is used to provide info at a "package" level as per its name. It contains documentation and annotations used in the package.
A few examples of where types are used are class instance creation expressions (new), casts, implements clauses, and throws clauses. This form of annotation is called a type annotation [...].
In eclipse
Since package-info.java isn't a valid identifier for a class it cannot be created as a class in Eclipse.
I found that when you create a new package there is a check box to check if you want a package-info.java.
To create a package-info.java file in an existing package:
Summary from the article here
In package-info.java:
@PackageLevelAnnotation package blammy; // package with a package level annotation. import blammy.annotation.PackageLevelAnnotation;
In PackageLevelAnnotation.java
package blammy.annotation; @Retention(RetentionPolicy.CLASS) @Target(ElementType.PACKAGE) public @interface PackageLevelAnnotation { // stuff as required. }
Edit: more package level info. Here is a link to the package chapter in the Java Language Spec: packages
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With