发现距离上一次在这里写博客已经三个多月了。。。说好的笔耕不辍呢=.=

Anyway,今天(确切说是昨天晚上)在code review中被组里的QA II问到在一个叫做package-info.java的java文件中的一些字符串能不能定义成constant。这里先介绍一下这个叫做package-info.java的java文件的大概情况:有若干annotation,有package,有import,但是没有任何的class定义。我试了一下发现如果想要在这里定义一个class,因为class名中不允许有“-”,所以是行不通的,当然也可以在外面新建一个java class然后放上static final string,不过显然为了两三个只在一个package里面用到的string而搞一个java class有点overkill了。。。于是解释说好像不是个好主意,然后cr过了。上班时由于sprint有任务时间稍紧,所以也没太追究这个名字首字母小写且没有任何class定义的java file到底是何方神圣。

下班之后又意外发现在intellij里面如果右键new东西的话居然有个选项是new一个package-info.java。。。简直醉了,既然这么有缘分那咱就google一下看看你到底是神马玩意吧!

这一google才发现原来这玩意不是我司内部的玩意,在http://www.intertech.com/Blog/whats-package-info-java-for/ 中作者详细介绍了这个package-info.java到底是什么,干了什么,以及为什么。原文截取如下:

package-info.java’s purpose

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.

1
package com.intertech.services;

The package-info.java file above must sit in the com.intertech.services package.

Package Documentation

Prior to Java 5, package level documentation (the documentation shown in Javadocs for a package) was placed in package.html.  Today, the description and other related documentation for a package can be written up in the package-info.java file and it gets used in the production of the Javadocs.  As a demonstration, the example package-info.java…

 /**
* DOMAIN CLASSES USED TO PRODUCE THE JSON AND XML OUTPUT FOR THE RESTFUL SERVICES.
* <P>
* THESE CLASSES CONTAIN THE JAXB ANNOTATIONS.
*
* @SINCE 1.0
* @AUTHOR JWHITE
* @VERSION 1.1
*/
package com.intertech.cms.domain;

… results in the following Javadocs.

Package Annotations

Perhaps more importantly to today’s annotation driven programmer, the package-info.java file contains package level annotations. An annotation with ElementType.PACKAGE as one of its targets is a package-level annotation and there are many of them.  Using your favorite IDE’s code assistant (shown in Eclipse below) in a package-info.java file and you will find a number package annotation options.

For example, perhaps you want to deprecate all the types in a package. You could annotate each individual type (the classes, interfaces, enums, etc. defined in their .java files) with @Deprecated (as shown below).

 @Deprecated
public class Contact {
}

Or, you could use the @Deprecated on the package declaration in package-info.java.  This has the effect of deprecating everything in the package in one fell swoop.

@Deprecated
package com.intertech.cms.domain;

package-info.java到底是什么的更多相关文章

  1. paip.自动import的实现跟java.lang.SecurityException Prohibited package name java

    paip.自动import的实现跟java.lang.SecurityException Prohibited package name java #-----自动import 因为java.lang ...

  2. Java 到底是值传递还是引用传递

    作者:Intopass链接:https://www.zhihu.com/question/31203609/answer/50992895来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...

  3. Java报错信息 java.lang.SecurityException: Prohibited package name: java.xxx

    package java.yun.System; public class SystemOut { public static void main(String[] args) { System.ou ...

  4. 188W+程序员关注过的问题:Java到底是值传递还是引用传递?

    在逛 Stack Overflow 的时候,发现了一些访问量像阿尔卑斯山一样高的问题,比如说这个:Java 到底是值传递还是引用传递?访问量足足有 188万+,这不得了啊!说明有很多很多的程序员被这个 ...

  5. [转帖]Stack Overflow上188万浏览量的提问:Java 到底是值传递还是引用传递?

    Stack Overflow上188万浏览量的提问:Java 到底是值传递还是引用传递? http://www.itpub.net/2019/12/03/4567/   在逛 Stack Overfl ...

  6. 面试官:兄弟,说说Java到底是值传递还是引用传递

    二哥,好久没更新面试官系列的文章了啊,真的是把我等着急了,所以特意过来催催.我最近一段时间在找工作,能从二哥的文章中学到一点就多一点信心啊! 说句实在话,离读者 trust you 发给我这段信息已经 ...

  7. Java到底是解释型还是编译型语言

    Java到底是解释型还是编译型语言? 定义 回答这个问题,我们首先来看下概念: 开发人员编写代码,语言是人类可理解的方式,是具有语义的,然而计算机无法理解和执行,因此需要做一层转换. 解释型语言: 运 ...

  8. 死磕面试系列,Java到底是值传递还是引用传递?

    Java到底是值传递还是引用传递? 这虽然是一个老生常谈的问题,但是对于没有深入研究过这块,或者Java基础不牢的同学,还是很难回答得让人满意. 可能很多同学能够很轻松的背出JVM.分布式事务.高并发 ...

  9. java classpath import package 机制 @Java的ClassPath, Package和Jar

    java classpath import package 机制   從一個簡單的例子談談package與import機制 基本原則:為什麼需要將Java文件和類文件切實安置到其所歸屬之Package ...

随机推荐

  1. Oracle数据库有用函数

    有用函数 DECODE 语法例如以下: DECODE(value, if1, then1, if2,then2,if3,then3, . . . else )  Value 代表某个表的不论什么类型的 ...

  2. hdu 1548

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  3. 取消CentOS 的图形界面 开机直接进入命令行模式(转)

    (1)具体操作   #vi /etc/inittab          –编辑/etc/inittab文件 找到下面语句: # Default runlevel. The runlevels used ...

  4. css-装饰

    css -在标签上设置style样式 background-color:#2356a1 height:48px -编写样式方法 1.标签的style属性 2.鞋子head里面,style标签中编写 - ...

  5. 对A-Star寻路算法的粗略研究

    首先来看看完成后的效果: 其中灰色代表路障,绿色是起点和移动路径,红色代表终点   // = openArray[i+1].F) { minNode = openArray[i+1]; } } sta ...

  6. IOS UIWebView 随记

    UIWebView中加载的网页尺寸太大,如何让网页适应屏幕大小 webview.scalesPageToFit = YES;

  7. IOS 汤姆猫核心代码

    // // MJViewController.m // 03-Tom // // Created by apple on 13-11-24. // Copyright (c) 2013年 itcast ...

  8. return和exit的差别

    #include<stdio.h> #include<sys/types.h> #include<sys/wait.h> #include<unistd.h& ...

  9. SAM4E单片机之旅——10、UART与MCK之PLL

    为使用更更高的波特率,则需要更更高的外设时钟的频率.这个时候就需要用到锁相环(PLL)了.锁相环可以对输入的时钟进行分频.升频后进行输出.MCK可以使用的锁相环为PLLA,而PLLA的输入时钟为MAI ...

  10. 九度OJ 1110:小白鼠排队 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1734 解决:1054 题目描述: N只小白鼠(1 <= N <= 100),每只鼠头上戴着一顶有颜色的帽子.现在称出每只白鼠的 ...