Thinking in Java——笔记(20)
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)的更多相关文章
- Java笔记20:迭代器模式
迭代器模式 所谓Iterator模式,即是Iterator为不同的容器提供一个统一的访问方式.本文以Java中的容器为例,模拟Iterator的原理. 1 定义一个容器Collection接口 pub ...
- JAVA自学笔记20
JAVA自学笔记20 1.递归: 1)方法定义中定义中调用方法本身的现象 2)要有出口,否则就是死递归 次数不能太多.否则内存将溢出 构造方法不能递归使用 //斐波那契数列:1,1,2,3,5,8,1 ...
- java笔记整理
Java 笔记整理 包含内容 Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...
- 转 Java笔记:Java内存模型
Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...
- Java笔记 —— 继承
Java笔记 -- 继承 h2{ color: #4ABCDE; } a{ text-decoration: none!important; } a:hover{ color: red !import ...
- Java笔记 —— this 关键字
Java笔记 -- this 关键字 h2{ color: #4ABCDE; } a{ color: blue; text-decoration: none; } a:hover{ color: re ...
- Effective Java笔记一 创建和销毁对象
Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...
- java笔记00-目录
--2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:
- Ext.Net学习笔记20:Ext.Net FormPanel 复杂用法
Ext.Net学习笔记20:Ext.Net FormPanel 复杂用法 在上一篇笔记中我们介绍了Ext.Net的简单用法,并创建了一个简单的登录表单.今天我们将看一下如何更好是使用FormPanel ...
随机推荐
- 【效率】专为Win7系统设计的极简番茄计时器 - MiniPomodoro (附源码)
时光飞逝,一转眼坚持使用番茄工作法已经快3年了!能坚持这么长时间,主要还是得益于它的简单.但是令人纠结的是,这么长时间以来,换了7款不同的番茄计时器,仍然没有找到非常满意的: ■ 机械的噪音太大,会妨 ...
- [ASP.NET MVC 小牛之路]13 - Helper Method
我们平时编程写一些辅助类的时候习惯用“XxxHelper”来命名.同样,在 MVC 中用于生成 Html 元素的辅助类是 System.Web.Mvc 命名空间下的 HtmlHelper,习惯上我们把 ...
- Atitit 数据库的事件机制--触发器与定时任务attilax总结
Atitit 数据库的事件机制--触发器与定时任务attilax总结 1.1. 事件机制的图谱1 2. 触发器的类型2 3. 实现原理 After触发器 Vs Instead Of触发器2 3.1. ...
- WCF学习之旅—WCF第二个示例(七)
三.创建客户端应用程序 若要创建客户端应用程序,你将另外添加一个项目,添加对该项目的服务引用,配置数据源,并创建一个用户界面以显示服务中的数据. 在第一个步骤中,你将 Windows 窗体项目添加到解 ...
- Java接口和抽象类的区别
今天看到项目中,写了一个抽象类,里面有很多方法继承了这类,当调用这个接口时,采用的是这个抽象类去调方法的,当时一想,这个不就是我们说的Java的多态的特征: 继承:存在继承关系的子类和父类 重写:子类 ...
- python django基础(一)
Django简介:Django是一个开放源代码的Web应用框架,由Python写成.采用了MVC的框架模式,即模型M,视图V和控制器C.不过在Django实际使用中,Django更关注的是模型(Mod ...
- AngularJS之ng-class(十一)
前言 本节我们来讲讲指令中的ng-class,通过添加ng-class来生成对应的样式,有时候得根据不同的条件来选择对应的样式,本节我们来看看ng-class的灵活性用法. ng-class使用字符串 ...
- 【前端攻略】最全面的水平垂直居中方案与flexbox布局
最近又遇到许多垂直居中的问题,这是Css布局当中十分常见的一个问题,诸如定长定宽或不定长宽的各类容器的垂直居中,其实都有很多种解决方案.而且在Css3的flexbox出现之后,解决各类居中问题变得更加 ...
- 使用nginx解决跨域问题(flask为例)
背景 我们单位的架构是在api和js之间架构一个中间层(python编写),以实现后端渲染,登录状态判定,跨域转发api等功能.但是这样一个中间会使前端工程师的工作量乘上两倍,原本js可以直接ajax ...
- 如何使用免费控件将Word表格中的数据导入到Excel中
我通常使用MS Excel来存储和处理大量数据,但有时候经常会碰到一个问题—我需要的数据存储在word表格中,而不是在Excel中,这样处理起来非常麻烦,尤其是在数据比较庞大的时候, 这时我迫切地需要 ...