博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Core Java Volume II—Using annotations
阅读量:5258 次
发布时间:2019-06-14

本文共 4253 字,大约阅读时间需要 14 分钟。

Annotations are tags that you insert into your source code so that some tool can process them. The tools can operate on source level or they can process class files into which the compiler has placed annotations.

Uses for annotations:

  • Automatic generation of auxiliary files such as deployment descriptors or bean information classes
  • Automatic generation of code for testing, logging, transaction semantics, and so on

Junit 4 testing tool calls all methods that are labeled @test when testing a class.

Annotations can be defined to have elements which can be processed by the tools that read the annotations.

An annotation can be put anywhere that you could put a modifier.

You can annotate packages, parameter variables, type parameters, and type uses.

Each annotation must be defined by an annotation interface. The methods of the interface correspond to the elements of the annotation.

The @interface declaration creates an actual Java interface.

Annotation Syntax:

An annotation is defined by a annotation interface:

modifiers @interface AnnotationName{    elementDeclaration1;    elementDeclaration2;    …}

 

Each element declaration has the form

type elementName();

or

type elementName() default value;

You cannot extend annotation interfaces.

The type of an annotation element is one of the following:

  • A primitive type
  • String
  • Class
  • An enum type
  • An annotation type
  • An array of the preceding types(an array of arrays is not a legal element type)

Each element has the format

@AnnotationName(elementName1 = value1, elementName2 = value2, …) // the order of elements does not matter.

The default value of the declaration is used if an element value is not specified.

If no elements are specified, you don't need to use parentheses.

@AnnotationName

The single value annotation

If an element has the special name value and no other element is specified, you can omit the element name and the = symbol.

An item can have multiple annotations, if an annotation is repeatable, you can repeat same annotation multiple times.

An annotation element can never be set to null. You can find other defaults such as "" or Void.class.

2 main use of annotation: declarations and type uses.

Declaration annotations can appear at the declaration of:

  • Packages
  • Classes
  • Interfaces
  • Methods
  • Constructors
  • Instances fields (including enum constants)
  • Local variables
  • Parameter variables
  • Type parameters

For classes and interfaces, put the annotations before the class or interface keyword; for variables, put them before the type; for type parameters <@AnnotationName V>; for packages, the package-info.java contains only the package statement preceded by annotations.

Type use annotations can appear in the following places:

  • With generic type arguments
  • In any position of an array
  • With superclasses and implemented interfaces:
  • With constructor invocations
  • With instanceof checks and casts
  • With exception specifications
  • With method and constructor references: eg. @Annotation Message::getText

Custom:

  • Put type use annotations after other modifiers;
  • Put declaration annotations before other modifiers.

An annotation placed on the constructor describes a property of the constructed object.

Annotation

Purpose

@Generated

Code generator tools

@PostConstruct

 

@PreDestroy

In environments that control the life-circle of objects—methods tagged with these annotations should be invoked immediately after an object has been constructed or before is being removed.

@Resource

Resource injection

@Target({ElementType.X, ElementType.Y, …})

Apply to an annotation, retricting the items to which the annotation applies.

@Rentention

Specifies how long an annotation is retained (SOURCE, CLASS, RUNTIME).

@Documented

The documentation of each annotated method/… contains the annotation.

@Inherited

Only applies to classes, all of the subclasses automatically have the same annotation.

Java SE 8 -> Legal to apply the same annotation type multiple times to an item. For backward compatibility, the implementor of a repeatable annotation needs to provide a container annotation that holds the repeated annotations in an array.

Whenever the user supplies 2 or more @TestCase annotations, they are automatically wrapped into a @TestCases annotation.

转载于:https://www.cnblogs.com/Hu-Yan/p/8977892.html

你可能感兴趣的文章
typedef 用户自定义的数据类型取一个新的名字
查看>>
TOMCAT内存大小调整
查看>>
打开指定文件或文件夹
查看>>
tomcat 配置图片虚拟路径不起作用解决办法
查看>>
如何查询收发的短信息
查看>>
UVA 111 简单DP 但是有坑
查看>>
Mysql 数据库操作
查看>>
Python OS模块
查看>>
Python item的使用
查看>>
Java关键字:transient,strictfp和volatile简介
查看>>
SQL表中的自连接定义与用法示例
查看>>
hdu 1032 The 3n + 1 problem
查看>>
static关键字
查看>>
转:linux终端常用快捷键
查看>>
009.栈实现队列
查看>>
A-Softmax的总结及与L-Softmax的对比——SphereFace
查看>>
关于软件盘覆盖住布局
查看>>
Unity3D 控制物体移动、旋转、缩放
查看>>
UVa 11059 最大乘积
查看>>
UVa 12545 比特变换器
查看>>