Before in development, always wonder why, when the interface is inherited, there will be @override annotations, and sometimes prompted to write annotations @suppresswarnings?
It turns out that this is Java-specific features, annotations!
So what is an annotation?
An annotation is an instance of a type of annotation that we can label on a class so that when the compiler compiles our files, it compiles the class according to our own set of methods.
What are the annotations? look at this picture below to understand!
The above figure shows that the annotations are broadly divided into three types: tag annotations, general annotations, meta annotations
This override is not tested, because the current eclipse will automatically help us out of the wrong, if the type does not match, there is no way to cover.
In addition to the deprecated annotation, there is no interception function except for multiple strikethrough lines.
The above test, also only for the IDE, if the use of Javac, should be prompted.
The other is not more said, standard yuan annotation is why?
@Documented markup Generation Javadoc
@Inherited Tag Inheritance Relationships
Lifetime of @Retention annotations
@Target the target of the callout
Let's make an annotation for ourselves!
First, you declare an interface, and it does not add annotation content!
1 package testannotation; 2 3 import java.lang.annotation.Documented; 4 import java.lang.annotation.Retention; 5 import Java.lang.annotation.RetentionPolicy; 6 7 @Documented 8 @Retention (retentionpolicy.runtime) 9 public @interface person{10 String name (); int Age (); 12}
The reflection mechanism is then used to view the annotation contents of the class
1 package testannotation; 2 3 @Person (name= "Xingoo", age=25) 4 public class Test3 {5 public static void print (Class C) {6 System.out.pri Ntln (C.getname ()); 7 8 //java.lang.class Getannotation method, and if there is an annotation, the annotation is returned. Otherwise returns null 9 person person = (person) c.getannotation (Person.class), and if (person! = null) { System.out.println ("Name:" +person.name () + "Age:" +person.age ()), }else{14 System.out.println ("person Unknown! "); }16 }17 public static void Main (string[] args) { test3.print (test3.class); }20}
Run the results and read the contents of the annotations
TestAnnotation.test3name:xingoo age:25
Http://www.cnblogs.com/xing901022/p/3966799.html
Preface: Two days to read the contents of Java custom annotations, and then according to my own understanding of the two code, it is very interesting, this article includes three parts: the basis of the annotation, the assignment through annotations (combined with the factory method mode), through the annotations to verify.
First, the basis of the annotations
1. Definition of annotations: Java files are called annotation, denoted by @interface.
2. Meta-Annotations: @interface above as needed to annotate some things, including @retention, @Target, @Document, @Inherited four kinds.
3. Retention policies for annotations:
@Retention (Retentionpolicy.source)//annotations only exist in the source code and are not included in the class bytecode file
@Retention (Retentionpolicy.class)//default retention policy, annotations exist in the CLASS bytecode file, but are not available at runtime
@Retention (retentionpolicy.runtime)//annotations are present in the class bytecode file and can be obtained through reflection at run time
4. Purpose of the annotations:
@Target (Elementtype.type)//interfaces, classes, enumerations, annotations
@Target (Elementtype.field)//fields, constants for enumerations
@Target (Elementtype.method)//method
@Target (Elementtype.parameter)//Method parameters
@Target (Elementtype.constructor)//constructor
@Target (elementtype.local_variable)//local variables
@Target (Elementtype.annotation_type)//annotations
@Target (Elementtype.package)//Package
5. Annotations are included in the Javadoc:
@Document
6. Annotations can be inherited:
@Inherited
7. Annotation parser: Used to parse custom annotations.
Ii. Assigning values through annotations (combined with factory method mode)
1. Custom annotations
Package Lwp.annotation;import Java.lang.annotation.documented;import Java.lang.annotation.elementtype;import Java.lang.annotation.inherited;import Java.lang.annotation.retention;import Java.lang.annotation.RetentionPolicy Import java.lang.annotation.target;/** * Init.java * * @author Liang WP July 10, 2014 */@Documented @inherited@target ({ Elementtype.field, Elementtype.method}) @Retention (retentionpolicy.runtime) public @interface init{public String value () Default "";}
2. Using annotations in the data model
Package Lwp.model;import lwp.annotation.init;/** * User.java * * @author Liang WP July 10, 2014 */public class user{ Priv Ate String name; Private String age; Public String getName () { return name; } @Init (value = "Liang") public void SetName (String name) { this.name = name; } Public String getage () { return age; } @Init (value = "setage") public void (String age) { this.age = age; }}
3. Use "Construction factory" to act as "annotation parser"
Package Lwp.factory;import Java.lang.reflect.method;import Lwp.annotation.init;import lwp.model.User;/** * Userfactory.java * * @author Liang WP July 10, 2014 */public class userfactory{public static User create () { User user = new user (); Get all methods in the User class (Getdeclaredmethods) method[] methods = User.class.getMethods (); Try { for method Method:methods) { //If this method has annotations, assign the data inside the annotation to the user object if ( Method.isannotationpresent (Init.class)) { init init = method.getannotation (init.class); Method.invoke (User, Init.value ()); }}} catch (Exception e) { e.printstacktrace (); return null; } return user; }}
4. Running the Code
Package Lwp.app;import Java.lang.reflect.invocationtargetexception;import Lwp.factory.userfactory;import lwp.model.user;/** * Test.java * * @author Liang WP July 10, 2014 */public class test{public static void Main (string[] Ar GS) throws Illegalaccessexception, illegalargumentexception, invocationtargetexception { User user = Userfactory.create (); System.out.println (User.getname ()); System.out.println (User.getage ());} }
5. Running Results
Liang23
Third, the verification through the annotations
1. Custom annotations
Package Lwp.annotation;import Java.lang.annotation.documented;import Java.lang.annotation.elementtype;import Java.lang.annotation.inherited;import Java.lang.annotation.retention;import Java.lang.annotation.RetentionPolicy Import java.lang.annotation.target;/** * Validate.java * * @author Liang WP July 11, 2014 */@Documented @inherited@target ( {Elementtype.field, Elementtype.method}) @Retention (retentionpolicy.runtime) public @interface validate{public int min () default 1; public int max () default ten; public boolean isnotnull () default true;}
2. Using annotations in the data model
Package Lwp.model;import lwp.annotation.validate;/** * User.java * * @author Liang WP July 11, 2014 */public class user{ @Validate (min = 2, max = 5) private String name; @Validate (Isnotnull = false) private String age; Public String getName () { return name; } public void SetName (String name) { this.name = name; } Public String getage () { return age; } public void Setage (String age) { this.age = age; }}
3. Annotation Parser
Package Lwp.check;import Java.lang.reflect.field;import Lwp.annotation.validate;import lwp.model.User;/** * Usercheck.java * * @author liang WP July 11, 2014 */public class usercheck{public static Boolean check (user user) { if (user = = null) {SYSTEM.OUT.PRINTLN ("!! Check object is empty!! "); return false; }//Get all the properties of the User class (if you use GetFields, you cannot get the property to private) field[] fields = User.class.getDeclaredFields (); for (Field field:fields) {//If the attribute has annotations, verify if (Field.isannotationpresent (Validate.class)) {Validate Validate = field.getannotation (Validate.class); if (Field.getname (). Equals ("Age")) {if (user.getage () = = null) { if (Validate.isnotnull ()) {SYSTEM.OUT.PRINTLN ("!!! The age of the null check does not pass: Can not be empty!! "); return false; } else {System.out.println ("age-verifiable pass: can be empty"); Continue }} else {System.out.println ("Age-free Check pass"); } if (User.getage (). Length () < Validate.min ()) { SYSTEM.OUT.PRINTLN ("!! Age Minimum Length check does not pass!! "); return false; } else {System.out.println ("Minimum Age Check Pass"); } if (User.getage (). Length () > Validate.max ()) {System. OUT.PRINTLN ("!! Age max length check does not pass!! "); return false; } else {System.out.println ("Max Age check Pass"); } if (Field.getname (). Equals ("name")) {if (user.getname () = = null) {if (Validate.isnotnull ()) {Sy STEM.OUT.PRINTLN ("!! The name can be null check does not pass: not empty!! "); return false; } else {System.out.println ("the name can be null verified by: can be empty"); Continue }} else {System.out.println ("the name can be null verified through"); } if (User.getname (). Length () < Validate.min ()) { SYSTEM.OUT.PRINTLN ("!! First name minimum length check does not pass!! "); return false; } else {System.out.println ("First name minimum length check pass"); } if (User.getname (). Length () > Validate.max ()) {System. OUT.PRINTLN ("!! Name Maximum length check does not pass!! "); return false; } else {System.out.println ("Maximum length check pass"); }}}} return true; }}
4. Running the Code
Package Lwp.app;import lwp.check.usercheck;import lwp.model.user;/** * Test.java * * @author Liang WP July 11, 2014 */public Class test{public static void Main (string[] args) { User user = new User (); User.setname ("Liang"); User.setage ("1"); System.out.println (Usercheck.check (user));} }
5. Running Results
The name can be null checked by the name of the minimum length check by the name of the maximum length of the check through the age of the minimum length check through the Age max length Check pass True
Http://www.cnblogs.com/liangweiping/p/3837332.html
"Java Development Series"--Custom annotations (GO)