Package-info. JAVA Role and usage

Source: Internet
Author: User

Package-info.java for programmers who often use external packages should be familiar with strangers. Because programmers who are not specialized in developing packages seldom need to pay attention to it, and often see him in other packages, the eyes are familiar. What are the characteristics and functions of it and how to use it? Programmers pay attention to mouth, why not create a try. Open Eclipse,new class, Oh! Creation failed, '-' as special characters are not allowed to appear in the class name, what condition. Originally it was a special case, of course, to create, Notepad, good success. I like inquisitive, but also what special, Google Bar, sure enough to have ancestors, turn around to collect.

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

Characteristics:

    • 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 a variable that supports Chinese names, and students who are accustomed to the challenge can try to share the experience.
    • 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. To facilitate the labeling of Annotation on a package;
    2. Declare friendly classes and package constants;
    3. Provides an 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 main concern, Pkgannotation.java is a annotation definition on a package, Client.java simulates a business operation class. Its structure is as follows:


1, for labeling on the package annotation to provide convenience

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

Java code

1/** 2     * Defines 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:

1 @PkgAnnotation  

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, the code is as follows

Java code:

1 public  class Client {      2 public         static void Main (string[] args) {   3             ///Can get package name by I/O operation or configuration item   4             String pkgname = "Com.company";        5 package             pkg = package.getpackage (pkgname);   6             //Get annotations on the package   7             annotation[] annotations = Pkg.getannotations ();   8             //Traversal annotation array   9 for             (Annotation an:annotations) {  ten                 if (an instanceof pkgannotation) {  11                     System.out.println ("Hi,i ' m The Pkgannotation");  * * *                      Note operation *                      myannotation Myann = (pkgannotation) an;                      * You can also manipulate all classes under the note package, such as initialization, checking, etc.                      * Similar to Struts @namespace, can be placed on the package name, indicating a package of the Namespace path                      * * * +}  21}     }  

The results of the operation are as follows:

Hi,i ' m The pkgannotation!

2, 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, declaring a package to use the public class, emphasizing the package access rights   4 class pkgclass{   5 public     Void Test () {   6     }   7}   8 Kanetsune volume, only run in-package access, applicable to sub-"package" Development   9 class pkgconst{     static final String pacakge_const= "ABC";  11}

3, provide the overall comment description of 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 has three:</b><br> 3      * 1, for labeling on the package annotation provide convenience;<br> 4      * 2, declare the package's private class and constant;<br> 5      * 3, provide the package's overall comment 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.

Package-info. JAVA Role and usage

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.