Annotations


  • They provide information that you need to fully describe your program, but that cannot be expressed in Java.
  • In general the kind of annotations you add and what you do with them are entirely up to you.
  • Java SE5 contains three generalpurpose built-in annotations.
  • Four additional annotation types support the creation of new annotations.
  • Annotations are true language constructs and hence are structured, and are type-checked at compile time.

Basic syntax

  • Syntactically, annotations are used in much the same way as modifiers.

Defining annotations

  • Annotation definitions look a lot like interface definitions. In fact, they compile to class files like any other Java interface.
  • **@Target** defines where you can apply this annotation (a method or a field).
  • **@Retention** defines whether the annotations are available in the source code (SOURCE), in the class files (CLASS), or at run time (RUNTIME).
  • Elements look like interface methods, except that you can declare default values.

Meta-annotations

  • The meta-annotations are for annotating annotations.
  • Most of the time, you will be defining your own annotations and writing your own processors to deal with them.

Writing annotation processors

  • An important part of the process of using annotations is to create and use annotation processors.
  • Java SE5 provides extensions to the reflection API to help you create these tools.

Annotation elements

  • Note that you are not allowed to use any of the wrapper classes, but because of autoboxing this isn’t really a limitation.
  • You can also have elements that are themselves annotations.

Default value constraints

  • No element can have an unspecified value. This means that elements must either have default values or values provided by the class that uses the annotation.
  • non-primitive type elements are allowed to take null as a value, either when declared in the source code or when defined as a default value in the annotation interface.

Generating external files

  • Annotations are especially useful when working with frameworks that require some sort of additional information to accompany your source code.
  • After defining a Java class, the programmer must undergo the tedium of respecifying information like the name, package and so on—information that already exists in the original class.
  • If you define an element on an annotation with the name value, then as long as it is the only element type specified you don’t need to use the name-value pair syntax; you can just specify the value in parentheses.

Annotations don’t support inheritance

  • You cannot use the extends keyword with **@interfaces**. There doesn’t seem to be any suggestion of annotations supporting inheritance in future releases.

Implementing the processor

  • There is no inheritance of annotation interfaces, using getDeclaredAnnotations( ) is the only way you can approximate polymorphic behavior.

Using apt to process annotations

  • apt is designed to be run on Java source files rather than compiled classes. By default, apt compiles the source files when it has finished processing them.
  • The apt tool can easily group several annotation processors together. It allows you to specify multiple classes to be processed, which is a lot easier than having to iterate through File classes yourself.
  • apt works by using an AnnotationProcessorFactory to create the right kind of annotation processor for each annotation it finds.
  • When you run apt, you specify either a factory class or a classpath where it can find the factories it needs.
  • When you create an annotation processor for use with apt, you can’t use the reflection features in Java because you are working with source code, not compiled classes.

Using the Visitor pattern with apt

  • Processing annotations can become complex.
  • To prevent the complexity from scaling up badly when you have more annotations and more processors, the mirror API provides classes to support the Visitor design pattern.
  • A Visitor traverses a data structure or collection of objects, performing an operation on each one.
  • The data structure need not be ordered, and the operation that you perform on each object will be specific to its type.
  • This decouples the operations from the objects themselves, meaning that you can add new operations without adding methods to the class definitions.
  • When you use the apt tool with the Visitor pattern, you provide a Visitor class which has a method for handling each type of declaration that you visit.

Annotation-based unit testing

  • With annotations we can include the unit tests inside the class to be tested, and thus reduce the time and trouble of unit testing to a minimum.
  • This approach has the additional benefit of being able to test private methods as easily as public ones.

Thinking in Java——笔记(20)的更多相关文章

  1. Java笔记20:迭代器模式

    迭代器模式 所谓Iterator模式,即是Iterator为不同的容器提供一个统一的访问方式.本文以Java中的容器为例,模拟Iterator的原理. 1 定义一个容器Collection接口 pub ...

  2. JAVA自学笔记20

    JAVA自学笔记20 1.递归: 1)方法定义中定义中调用方法本身的现象 2)要有出口,否则就是死递归 次数不能太多.否则内存将溢出 构造方法不能递归使用 //斐波那契数列:1,1,2,3,5,8,1 ...

  3. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  4. 转 Java笔记:Java内存模型

    Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...

  5. Java笔记 —— 继承

    Java笔记 -- 继承 h2{ color: #4ABCDE; } a{ text-decoration: none!important; } a:hover{ color: red !import ...

  6. Java笔记 —— this 关键字

    Java笔记 -- this 关键字 h2{ color: #4ABCDE; } a{ color: blue; text-decoration: none; } a:hover{ color: re ...

  7. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  8. java笔记00-目录

    --2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:

  9. Ext.Net学习笔记20:Ext.Net FormPanel 复杂用法

    Ext.Net学习笔记20:Ext.Net FormPanel 复杂用法 在上一篇笔记中我们介绍了Ext.Net的简单用法,并创建了一个简单的登录表单.今天我们将看一下如何更好是使用FormPanel ...

随机推荐

  1. Ubuntu 14 安装 .Net Core

    .Net Core的安装包的分发地址如下: https://apt-mo.trafficmanager.net/repos/dotnet-release/pool/main/d/ 方法一: 可以分别手 ...

  2. 2013 duilib入门简明教程 -- 界面设计器 DuiDesigner (10)

        上一个教程讲解了怎么布局最大化.最小化.关闭按钮,但是如果手动去计算这三个按钮的位置和大小的话,非常的不直观,也很不方便.     所以这一章准备介绍duilib的UI设计器,由于这个设计器很 ...

  3. Java基础-服务器的发送和接收

    package hanqi.test; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWri ...

  4. HTML5应用缓存机制

    首先先上一张图: 用360浏览器的用户对这张图应该都是耳熟能详了吧,没错,当网络不通畅时使用360浏览器,便会有这张图弹出来.为什么没有网络还能弹出这一副画面呢?这就关乎HTML5的应用缓存机制了. ...

  5. HTML5入门以及新标签

    HTML5 学习总结(一)--HTML5入门与新增标签   目录 一.HTML5概要 1.1.为什么需要HTML5 1.2.什么是HTML5 1.3.HTML5现状及浏览器支持 1.4.HTML5特性 ...

  6. python中协程

    在引出协成概念之前先说说python的进程和线程. 进程: 进程是正在执行程序实例.执行程序的过程中,内核会讲程序代码载入虚拟内存,为程序变量分配空间,建立 bookkeeping 数据结构,来记录与 ...

  7. C#设计模式系列:观察者模式(Observer)

    在软件构建过程中,需要为某些对象建立一种“通知依赖关系”,即一个对象的状态发生改变,所有的依赖对象都需要得到通知. 1.观察者模式简介 1.1>.定义 定义对象间的一种一对多的依赖关系,当一个对 ...

  8. OPEN CASCADE Multiple Variable Function

    OPEN CASCADE Multiple Variable Function eryar@163.com Abstract. Multiple variable function with grad ...

  9. 为什么DOM操作很慢

    转自:http://kb.cnblogs.com/page/534571/ 一直都听说DOM很慢,要尽量少的去操作DOM,于是就想进一步去探究下为什么大家都会这样说,在网上学习了一些资料,这边整理出来 ...

  10. jQuery控制tabs打开的数量