Core Java Volume II—Using annotations

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

时间: 2024-10-11 23:52:23

Core Java Volume II—Using annotations的相关文章

Core Java Volume I — 3.10. Arrays

3.10. ArraysAn array is a data structure that stores a collection of values of the same type. You access each individual value through an integer index. For example, if a is an array of integers, then a[i] is the ith integer in the array.Declare an a

Core Java Volume I — 3.3. Data Types

3.3. Data TypesJava is a strongly typed language(强类型语音). This means that every variable must have a declared type(每个变量都必须声明类型). There are eight primitive types in Java(Java有8种原始类型). Four of them are integer types; two are floatingpoint number types;

Core Java Volume I — 4.4. Static Fields and Methods

4.4. Static Fields and MethodsIn all sample programs that you have seen, the main method is tagged with the static modifier. We are now ready to discuss the meaning of this modifier.4.4.1. Static FieldsIf you define a field as static, then there is o

Core Java Volume I — 4.1. Introduction to Object-Oriented Programming

4.1. Introduction to Object-Oriented ProgrammingObject-oriented programming, or OOP for short, is the dominant programming paradigm these days, having replaced the "structured," procedural programming techniques that were developed in the 1970s.

2015.5.21 Core Java Volume 1

如果你只想用一次的话  就是 String s = new Date(); 如果想用多次的话 就是 Date birthday = new Date();

Core Java (十一) Java 继承,类,超类和子类

Core Java (十一) Java 继承,类,超类和子类 标签: javaJavaJAVA 2013-01-22 17:08 1274人阅读 评论(0) 收藏 举报  分类: java(58) 读书笔记(46)  版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 继承关系 两个类之间存在三种关系: 依赖,uses-a,如果一个类的方法操纵另一个对象,我们就说一个类依赖于另一个类. 聚合(关联),has-a,一个对象包含另外一个对象,聚合关系意味着类A的对象包含类B的对象

Core Java 学习笔记——1.术语 环境配置/Eclipse汉化字体快捷键/API文档

今天起开始学习Java,学习用书为Core Java.之前有过C的经验.准备把自己学习这一本书时的各种想法,不易理解的,重要的都记录下来.希望以后回顾起来能温故知新吧.也希望自己能够坚持把自己学习这本书的整个过程记录下来. I want to put a ding in the universe. 基本术语:       Object Oriented Programming——OOP——面向对象编程 Application Programming Interface——API——应用程序编程接

Core Java 笔记

Core Java 的读书笔记,持续更新中... Core Java笔记 1.对象与类 Core Java笔记 2.继承 Core Java笔记 3.反射

Core Java的那点事儿之ArrayList

Core Java的那点事儿之ArrayList 万丈高楼平地起,Java基础要拿起.今天就从我看的Core Java里找了些小基础点来分享一下. 首先隆重介绍一下专业级龙套演员---Employee类(PS:我可是专注龙套30年),下面会有多次出场,因此先在此介绍一下: 1 class Employee{ 2 private String name; 3 private double salary; 4 private int id; 5 6 //下面是set.get方法 7 } ArrayL