Reputation: 1075
I'm trying to add package level annotations but I don't have a clue on how to do it. Examples are appreciated.
Upvotes: 52
Views: 57629
Reputation: 157
package-info.java
The package-info.java
is a Java file that can be added to any Java source package. Its purpose is to provide a home for package level documentation and package level annotations.
Simply create the package-info.java
file. Add the package declaration in the file. In fact, the only thing the package-info.java file must contain is the package declaration.
Example of a minimal package info file:
package com.example.myapp.backend.data;
Example of a package-level annotation, @ParametersAreNonnullByDefault
:
@ParametersAreNonnullByDefault
package com.example.myapp.backend.data;
import javax.annotation.ParametersAreNonnullByDefault;
For more info, see the Java specifications, in The Java® Language Specification, section 7.4.1 Named Packages.
Upvotes: 5
Reputation: 28737
Open explorer, go to src/your package folder.
right click -> Create new textfile: name it package-info.java.
Go back to eclipse and edit and add the desired content.
Upvotes: 3
Reputation: 7159
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:
Upvotes: 41
Reputation: 38300
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
Upvotes: 42