Discussion on alternative package-info.java documents

Source: Internet
Author: User

Look at the previous notes, see a special Java file: pacakge-info.java, although there are records, but not all, just try to track the problem, share the results of a sequential ledger.

First, it cannot be created.In eclipse, the package-info file cannot be created at will. The "type name is notvalid" error is reported, and the class name is invalid. The Java variable definition specification is: letters, numbers, and underscores, there is also the less commonly used $ symbol (by the way, Java is a variable that supports Chinese names. If you are used to the challenge, you can try and share your experience in this area ), how can I create this file?

It's easy to use NotePad to create one, copy it, and then change it. The more direct way is to copy one from another project, which is more convenient.

Second, the service objects are very special.A class is the description of a class or a group of things. For example, the dog class describes wangcai. What is the description of the package-Info class? It always has an object to be described or presented. It isDescribe and record the package information.

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

In fact, there are several special aspects, such as the inability to inherit, the absence of interfaces, and the absence of inter-class relationships (associations, combinations, and aggregation.

The particularity of this file is over. Let's talk about its role. It has three roles:

1To facilitate Annotation on the package;

2, Declare the friendly class and package constant;

3Provides the overall description of the package.

Let's create a project to demonstrate these three functions. Create a Java project of package-Info and three classes in the com. Company package:Package-info.javaIs our focus,Pkgannotation. JavaIs an annotation definition labeled on the package,Client. JavaSimulate business operations. Its structure is as follows:

 

 

 

Convenience for labeling Annotation on the package

First, define the annotation of a package type. It can only be placed on one package:

 

Java code
  1. /**
  2. * Defines annotations that can only be labeled on a package.
  3. */
  4. @ Target (elementtype. Package)
  5. @ Retention (retentionpolicy. runtime)
  6. Public @ interface pkgannotation {
  7. }

 

 

 

Define a package-Info class. This is a special class. first look at the Code:

 

Java code
  1. @ Pkgannotation
  2. Package com. Company;

It is very simple. There is nothing in this file. In these two statements, there is no class or constant variable declaration. Next, write a simulated transaction class. The Code is as follows:

 

Java code
  1. Public class client {
  2. Public static void main (string [] ARGs ){
  3. // You can obtain the package name through I/O operations or configuration items.
  4. String pkgname = "com. Company ";
  5. Package PKG = package. getpackage (pkgname );
  6. // Get the Annotation on the package
  7. Annotation [] annotations = PKG. getannotations ();
  8. // Traverse the annotation Array
  9. For (annotation an: Annotations ){
  10. If (an instanceof pkgannotation ){
  11. System. Out. println ("Hi, I'm the pkgannotation, Which is be placed on package! ");
  12. /*
  13. * Annotation operation
  14. * Myannotation myann = (pkgannotation);
  15. * You can also operate on all classes under the annotation package, such as initialization and check.
  16. * Similar to struts @ namespace, you can place it on the package name to indicate the namespace path of a package.
  17. */
  18. }
  19. }
  20. }
  21. }

The running result is as follows:

 

 

Hi, I'm the pkgannotation, Which is be placed on package!

 

 

Declare friendly classes and package Constants

This is simple and practical. For example, if a package contains a lot of internal access classes or constants, you can put them in the package-Info class in a unified manner, which is convenient and centrally managed, to reduce the number of walking around the friendly class, see the example below:

 

Java code
  1. @ Pkgannotation
  2. Package com. Company;
  3. // Here is the package class. It declares the public class used by a package and emphasizes the package access permission.
  4. Class pkgclass {
  5. Public void test (){
  6. }
  7. }
  8. // The package constant, which only runs in-package access and is suitable for development by "package"
  9. Class pkgconst {
  10. Static final string pacakge_const = "ABC ";
  11. }

 

 

 

Provide the overall description of the package

If it is divided into "packages" for development, that is to say, a package implements a business logic or function point, or module, or component, you need to have a good description of a package, it indicates what the package is doing, what is the role, version changes, special instructions, and so on, as follows:

 

Java code
  1. /**
  2. * <B> package-info is not a common class and has three functions: </B> <br>
  3. * 1. Convenience of Annotation on the package; <br>
  4. * 2. Declare the private class and constant of the package. <br>
  5. * 3. Provide an overall description of the package. <Br>
  6. */
  7. Package com. Company;

 

 

 

The API documentation generated through javadoc is as follows:

 

 

Pipeline can also be done, Do not argue, it is recommended that Java 1.5 and later versions use package-info.java to comment.

 

Problems related to package-Info

In project development, common annotations that can be placed on the package include struts @ namespace, Hibernate @ filterdef and @ typedef. Under the package, add these annotations before the package name in any class, eclipse will prompt "package annotations must be in file package-info.java", create a package-info.java file under the package, move the annotation here.

When using the checkstyle plug-in for code check, a warning "missing package-info.java file." is also caused by this package-info file, you can create one under each package.

 

 

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

Related Article

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.