Use of Java annotations

Source: Internet
Author: User

Use of Java annotations

Original: http://www.cnblogs.com/pepcod/archive/2013/02/16/2913474.html

Annotations (also known as metadata) have a simple syntax that is basically consistent with Java's inherent syntax, in addition to the use of the @ symbol. Java SE5 contains three types of annotations defined in Java.lang:

(1) @Override

(2) @Deprecated

(3) Supresswarings: Improper compiler warning message is turned off.

Java also provides four additional annotations, specifically responsible for the creation of new annotations:

@Target indicates where the annotation can be used, and the possible ElementType parameters are:

CONSTRUCTOR: Declaration of the constructor

Field: Domain declarations (including enum instances)

local_variable: local variable declaration

Method: Methods Declaration

Package: Packet declaration

PARAMETER: Parameter declaration

Type: Class, interface, or enum declaration

@Retention indicates at what level the annotation information needs to be saved. The optional retentionpolicy parameters include:

SOURCE: Annotations are discarded by the compiler

Class: Annotations are available in the class file, but are discarded by the VM

RUNTIME:VM will retain annotations during run time, so the information of annotations can be read through the reflection mechanism .

@Documented to include annotations in the Javadoc

@Inherited allow subclasses to inherit annotations from parent class

How to define an annotation:

@Target (Elementtype.type) @Retention (retentionpolicy.runtime)  public @Interface  Test {}

In addition to the @ symbol, annotations are much like an interface. You need to use meta annotations when defining annotations .

There are generally some elements in the annotations that represent certain values. The annotated element looks like the method of the interface, the only difference being that it can be set to a default value. Annotations without elements are called tag annotations, and the @test above is a markup annotation.

The types of annotations available include the following: All basic types, String, Class, enum, Annotation, and array forms of the above types. An element cannot have an indeterminate value, that is, it has either a default value or a value for the element when the annotation is used. And the element cannot use NULL as the default value. Note In the case where there is only one element and the name of the element is value, you can omit "value=" when using annotations and write the required values directly.

 Here is an annotation that defines the element.

@Target (Elementtype.method) @Retention (retentionpolicy.runtime)  public @Interface  usercase {    public  String ID ();      Public default "No description";}

The annotations are defined and must be used.

 public  class   Passwordutils {@UseCase (id  =, description =" Passwords must contain at Lea St One numeric ""  boolean   ValidatePassword (String password) { return  (password.matches ("\\w*\\d\\w*"  = 48)  public  Span style= "color: #000000;" > string Encryptpassword (string password) { return  new   StringBuilder (password). reverse (). toString (); }  }

The most important part of using annotations is the processing of annotations, then the annotation processor is involved.

In principle, the annotation processor obtains the annotation information on the inspected method through the reflection mechanism, and then carries out the specific processing according to the value of the annotation element.

 Public Static voidMain (string[] args) {List<Integer> usecases =NewArraylist<integer>(); Collections.addall (usecases,47, 48, 49, 50); Trackusecases (Usecases, passwordutils.class); }   Public Static voidTrackusecases (list<integer> usecases, class<?>cl) { for(Method m:cl.getdeclaredmethods ()) {UseCase UC= M.getannotation (usecase.class); if(UC! =NULL) {System.out.println ("Found use case:" + uc.id () + "" +uc.description ()); Usecases.remove (NewInteger (Uc.id ())); }     }      for(inti:usecases) {System.out.println ("Warning:missing use case-" +i); } }

Operation Result:

Found use case:47 passwords must contain at least one numericfounduse case:-49  Case-50

Use of Java 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.