How do I add package level annotations or edit package-info.java?

心已入冬 提交于 2019-11-29 02:49:00

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

javabeangrinder

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:

  1. Right-click on the package where you want a package-info.java.
  2. Select New->Package.
  3. Check the Create package-info.java check box.
  4. Click on Finish.

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.

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 and add the package declaration that it relates to in the file. In fact, the only thing the package-info.java file must contain is the package declaration.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!