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 annotation @deprecated is deprecated code, which is not approved for use.
@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, 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 (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:
1 @Target (Elementtype.method) 2 @Retention (retentionpolicy.runtime) 3 public @interface Test {4 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 in 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.
1 @Target (Elementtype.method) 2 @Retention (retentionpolicy.runtime) 3 public @interface UseCase {4 public String ID ( ); 5 Public String description () default "no description"; 6}
The annotations are defined and must be used.
1 public class Passwordutils {2 @UseCase (id = $, Description = "Passwords must contain at least one numeric") 3 public boolean ValidatePassword (String password) {4 return (password.matches ("\\w*\\d\\w*")); 5 } 6 7 @UseCase (id = $) 8 Public String Encryptpassword (string password) {9 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.
public static void Main (string[] args) { list<integer> usecases = new arraylist<integer> (); Collections.addall (usecases, +,--); Trackusecases (usecases, Passwordutils.class); } public static void Trackusecases (List<integer> usecases, class<?> cl) {for (Method M:CL.GETDECLA Redmethods ()) { UseCase UC = m.getannotation (usecase.class); if (UC! = null) { System.out.println ("Found use case:" + uc.id () + "" + 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!
Use and instance of annotations in Java (i)