Core Java Volume ii-using annotations

Source: Internet
Author: User
Tags modifiers java se

Annotations is tags that I 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 to 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 is labeled @test when testing a class.

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

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

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

Each annotation must is 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 are defined by a annotation interface:

Modifiers @interface  annotationname{    elementDeclaration1;    ElementDeclaration2;    ...}

Each element declaration have 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 are 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 a element value is not specified.

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

@AnnotationName

The Single value annotation

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

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

An annotation element can never is 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
    • Span style= "font-family: Microsoft Jacob Black; Font-size:10pt ">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 in 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 is invoked immediately after a object has been constr Ucted 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 a 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 has 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 is automatically wrapped into a @TestCases annotation.

Core Java Volume ii-using annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.