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

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. Android-->状态栏高度,导航栏高度,Window高度,DecorView高度,heightPixels

    1:DecorView的高度 DecorView的高度代表的是: 整个装饰窗口的高度, 这个高度包括:状态烂的高度和导航栏的高度.(状态栏和导航栏通常叫做装饰窗口, 而ActionBar不属于装饰窗口 ...

  2. 投影纹理映射(Projective Texture Mapping) 【转】

    摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人”  投影纹理映射( Projective ...

  3. C - The C Answer (2nd Edition) - Exercise 1-16

    /* Revise the main routine of the longest-line program so it will correctly print the length of arbi ...

  4. basePath 方便

    String path = request.getContextPath()+"/";String basePath = request.getScheme() + ": ...

  5. maximum-depth-of-binary-tree——找出数的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. D堆的实现

    实现上一篇博客(http://blog.csdn.net/buleriver/article/details/38469977)说的D堆.假设把mD设置成2.D堆就退化成二叉堆,也就是说.二叉堆是D堆 ...

  7. Django-缓存的配置

    缓存的介绍 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一次的的后台操作,都会消耗 ...

  8. anaconda的所有版本大全--下载地址

    地址: https://repo.continuum.io/archive/ 内容: Anaconda installer archive Filename Size Last Modified MD ...

  9. Java泛型擦除

    Java泛型擦除: 什么是泛型擦除? 首先了解一下什么是泛型?我个人的理解:因为集合中能够存储随意类型的对象.可是集合中最先存储的对象类型一旦确定后,就不能在存储其它类型的对象了,否则,编译时不会报错 ...

  10. 调试LD_PRELOAD注入的代码

    LD_PRELOAD提供了平民化的注入方式固然方便,同一时候也有不便:注入库出错后调试比較困难. 我琢磨了几天找到了可行的调试方法,当然未必是最有效的办法.抛出陋文,希望引来美玉~ 首先.写一段代码作 ...