[Go] Use and instance of annotations in Java

Source: Internet
Author: User
Tags deprecated

Annotations are currently very popular, and many mainstream frameworks support annotations, and when you write your own code, you try to use annotations as much as possible, but the code is more concise.

The syntax of annotations is simple, except for the use of the @ symbol, which is basically consistent with the Java intrinsic syntax. There are three standard annotations built into the Java SE5:

@Override that indicates that the current method definition overrides a method in the superclass.

@Deprecated, the element compiler that uses annotations for it will issue a warning because the annotations @Deprecated are deprecated code that is disapproved of.

@SuppressWarnings, turn off improper compiler warning messages.

How much of the above three annotations we are going to have when writing code. Java also provides 4 of annotations, specifically responsible for the creation of new annotations.

@Target

indicates where the annotation can be used, Possible ElementType parameters are:

CONSTRUCTOR: Declaration of the constructor

Field: Domain declaration (including enum instance)

local_ VARIABLE: local variable declaration

method: Method declaration

package: Package declaration

parameter: parameter declaration

type: Class, interface (including annotation type) 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 the run, so the information of the annotations can be read through the reflection mechanism.

@Document

To include annotations in the Javadoc

@Inherited

Allow subclasses to inherit annotations from parent class

  How to define an annotation:

@Target (Elementtype.method)@Retention (retentionpolicy.runtime) public@interface Test { 5}    

In addition to the @ symbol, annotations are much like an interface. When defining annotations, you need to use the meta-annotations, which use @Target and @RetentionPolicy, and their meanings are given near to the table above.

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 UseCase {  Public String ID ();  Default "no description";  6}        

The annotations are defined and must be used.

1PublicClasspasswordutils {2 @UseCase (id = description = "Passwords must contain at least one numeric")3Publicboolean ValidatePassword (String password) { 4 return (Password.matches ("\\w*\\d\\w*"  5 } 6  7 @UseCase (id = 48< Span style= "color: #000000;" >) public String Encryptpassword (String password) {return new StringBuilder (password). reverse (). toString (); }11}       

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.

PublicStaticvoidMain (string[] args) {list<integer> usecases =New arraylist<integer>(); Collections.addall (Usecases, 47, 48, 49, 50); Trackusecases (usecases, Passwordutils.Class); }PublicStaticvoid trackusecases (list<integer> usecases, Class<?> CL) {for Class); if (UC! = null Uc.description ()); Usecases.remove (new Integer (Uc.id ()));} for (int i:usecases) {System.out.println ("Warning: Missing use case-"+ i); } }

Found use case:47 passwords must contain at least one numeric

Found Use case:48 No description

Warning:missing Use case-49

Warning:missing Use case-50

The above three snippet of code is a simple example of tracking a use case in a project.

Write here bloggers think of combining enumerations, annotations, reflections, interceptors and other content, can write a set of user rights verification?

The user right is enumerated, the annotation element indicates that a method must have certain permissions to invoke, the interceptor intercepts the request method, whether the user has permission to call the method, according to different permissions of the user different processing. Welcome to the discussion!

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

[Go] Use and instance of annotations in Java

Related Article

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.