Discussion on the alternative Package-info.java file

Source: Internet
Author: User

Original address: http://strong-life-126-com.iteye.com/blog/806246

Look at the previous notes, see a special Java file: Pacakge-info.java, although there are records, but not all, try to track down the problem, share the results of journal-style.

first, it cannot be created casually. in Eclipse, the Package-info file cannot be created casually, the "type name is Notvalid" error, the class name is invalid, the Java variable definition specification is: Letters, numbers, underscores, and the less commonly used $ symbol (incidentally, Java is to support the Chinese name of the variable, the habit of the challenge of the classmate can try to share the experience of this, the line can no longer be listed, how to create this file?

Very simple, create one with Notepad, and then copy it and change it, the more straightforward way is to copy it from another project, which is more convenient.

Secondly, the object of service is very special. A class is a description of a category or group of things, such as the Dog class, which describes the package-info, what does this class describe? It always has to have a descriptive or stated object, which is a description and a record of the package information .

Finally, the class cannot have public, private access permissions. Package-info.java How special, also a class file, will also be compiled into Package-info.class, but in Package-info.java can only declare the default access rights of the class, that is, the friendly class.

In fact, there are a few special places, such as can not inherit, no interface, no inter-class relationship (association, composition, aggregation, etc.).

The particularity of this document is finished, then say what it does, it has three functions:

1 , for labeling on the package annotation to provide convenience;

2 , declaration-friendly classes, and package constants;

3 , providing the overall comment description for the package.

Let's build a project to demonstrate these three roles, build a package-info Java project, Com.company package three classes:Package-info.java is our focus, Pkgannotation.java is a callout definition that is labeled on a package,Client.java simulates a business operation class. Its structure is as follows:

Provides convenience for labeling annotation on the package

First define a package-type annotation, which can only be placed on one package:

Java code
    1. /**
    2. * Define annotations that can only be labeled on the package
    3. */
    4. @Target (Elementtype.package)
    5. @Retention (Retentionpolicy.runtime)
    6. Public @interface Pkgannotation {
    7. }

Then define a Package-info class, which is a special class that looks at the code first:

Java code
    1. @PkgAnnotation
    2. Package com.company;

Very simple, just such a file, there is nothing inside, just these two words, no class, no constant variable declaration. Then write a simulated trading class with the following code:

Java code
  1. public class Client {
  2. public static void Main (string[] args) {
  3. Package names can be obtained through I/O operations or configuration items
  4. String pkgname = "Com.company";
  5. Package pkg = Package.getpackage (pkgname);
  6. Get annotations on a package
  7. annotation[] annotations = pkg.getannotations ();
  8. Iterating through an array of annotations
  9. for (Annotation an:annotations) {
  10. if (an instanceof pkgannotation) {
  11. System.out.println ("Hi,i ' m the Pkgannotation, which is being placed on package!");
  12. /*
  13. * Annotation operation
  14. * Myannotation Myann = (pkgannotation) an;
  15. * You can also manipulate all classes under the note package, such as initialization, checking, etc.
  16. * @namespace similar to struts, can be placed on the package name, indicating the Namespace path of a package
  17. */
  18. }
  19. }
  20. }
  21. }

The results of the operation are as follows:

Hi,i ' m The Pkgannotation, which is being being placed on package!

Declaring friendly classes and package constants

This is relatively simple, and very practical, such as a package has a lot of internal access to the class or constant, it can be unified into the Package-info class, so convenient, and centralized management, reduce the friendly class around the situation, see examples:

Java code
    1. @PkgAnnotation
    2. Package com.company;
    3. Here is the package class, which declares a common class used by a package, emphasizing the package access rights
    4. Class pkgclass{
    5. public void Test () {
    6. }
    7. }
    8. Kanetsune volume, only run in-package access, suitable for sub-"package" development
    9. Class pkgconst{
    10. Static final String pacakge_const= "ABC";
    11. }

Provides an overall comment description for the package

If it is a "package" development, that is, a package to implement a business logic or function points, or modules, or components, you need a package has a good description of what the package is dry, what role, version changes, special instructions, and so on, as follows:

Java code
    1. /**
    2. * <b>package-info is not a normal class, its role is three:</b><br>
    3. * 1, for labeling on the package annotation to provide convenience;<br>
    4. * 2, declare the private classes and constants of the package;<br>
    5. * 3, provide the package's overall note description. <br>
    6. */
    7. Package com.company;

The API documentation generated through Javadoc is as follows:

This is no different from the placement of package.htm under the package, just package-info can better maintain the integrity of the document in the Code, and can implement the code and document synchronization update, package.htm can do, do not argue, the recommendation is Java More than 1.5 versions are annotated with Package-info.java.

Issues related to Package-info

In project development, common annotations that can be placed on packages are: Struts @namespace, Hibernate @filterdef and @typedef, and so on. Under the package, add these annotations to the package name in any class, and Eclipse will prompt "Annotations must is in file Package-info.java", Create the Package-info.java file under the package and move the annotations here.

When using the Checkstyle plugin for code checking, a warning "Missing Package-info.java file" is reported. This package-info file is also a curse, create one under each package.

Discussion on the alternative Package-info.java file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.