Weekend free to work, want to study the knowledge of annotations, have seen several times, have forgotten, this study, and write an article record,
1, Yuan annotation
Meta annotations are annotations of annotations. Contains @Retention @Target @Document @Inherited four kinds.
1.1. @Retention: Define retention policies for annotations
Java code
Copy the code code such as the following:
@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 execution time.
@Retention (retentionpolicy.runtime)//annotations are present in the class bytecode file and can be obtained through reflection at execution time.
1.2. @Target: Define the purpose of the annotations
Java code
Copy the code code such as the following:
@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 variable
@Target (Elementtype.annotation_type)//annotations
@Target (Elementtype.package)///Package
ElementType can have multiple, one annotation can be class, method, field, etc.
1.3. @Document: Note that the note will be included in the Javadoc
1.4, @Inherited: The description subclass can inherit this annotation from the parent class
2, the annotations of their own definition
Java code
Copy the code code such as the following:
3, the use of annotations, test class
Java code
Copy the code code such as the following:
4. Analytical Annotations
The reflection mechanism of Java can help, get annotations, code such as the following:
Java code
Copy the code code such as the following:
See, very easy to see the Java programming idea there is a complex point of the sample, we go over it, he is using annotations to implement a database creation sample.
First, write a database name annotation to read the database name:
Package Com.bin.annotation;import java.lang.annotation.*; @Target (elementtype.type) @Retention ( retentionpolicy.runtime) Public @interface dbtable {public String name () default "";}
Create a field of type integer:
Package Com.bin.annotation;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target; @Target (Elementtype.field) @Retention ( retentionpolicy.runtime) public @interface Sqlinteger {String name () default "";//Default Empty constraints constraints () defaults @Constraints; Other type definitions for the field}
Create a field of type string:
Package Com.bin.annotation;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target; @Target (Elementtype.field) @Retention ( retentionpolicy.runtime) public @interface SQLString {int value () default 0; String name () default ""; Constraints Constraints () default @Constraints;}
Some other definitions
Package Com.bin.annotation;import java.lang.annotation.*; @Target (Elementtype.field) @Retention ( retentionpolicy.runtime) Public @interface Constraints {boolean primaryKey () default False;boolean allownull () default True;boolean unique () default false;}
Create a bean for annotations
Package Com.bin.annotation, @DBTable (name= "MEMBER") public class MEMBER {@SQLString (+) String firstName; @SQLString (50 string lastName; @SQLInteger Integer age; @SQLString (Value=30,[email protected] (primarykey=true)) string handle; static int membercount;public String toString () {return handle.tostring ();} Public String Getfirstname () {return firstName;} public void Setfirstname (String firstName) {this.firstname = FirstName;} Public String Getlastname () {return lastName;} public void Setlastname (String lastName) {this.lastname = LastName;} Public Integer Getage () {return age;} public void Setage (Integer age) {this.age = age;} Public String GetHandle () {return handle;} public void SetHandle (String handle) {this.handle = handle;}}
Finally, let's parse this note:
Package Com.bin.annotation;import Java.lang.annotation.annotation;import Java.lang.reflect.field;import Java.util.arraylist;import Java.util.list;public class Tablecreator {public static void main (string[] args) throws classnotfoundexception{class<?> cl =member.class;dbtable dbtalbe=cl.getannotation (DBTable.class); String tableName = Dbtalbe.name (), if (Tablename.length () <1) Tablename=cl.getname (). toUpperCase (); list<string> columndefs = new arraylist<string> (); for (Field Filed:cl.getDeclaredFields ()) {String ColumnName = null; annotation[] anns = filed.getdeclaredannotations (); if (Anns.length < 1) continue;if (Anns[0] instanceof SQLInteger) { Sqlinteger sInt = (sqlinteger) anns[0];if (Sint.name (). Length () < 1) ColumnName = Filed.getname (). toUpperCase (); Elsecolumnname = Sint.name (); Columndefs.add (ColumnName + "INT" + getconstraints (sint.constraints ()));} if (Anns[0] instanceof SQLString) {SQLString sstring = (SQLString) anns[0];if (Sstring.name (). Length () <1) {ColumnName = Filed.getname (). toUpperCase ();} Elsecolumnname =sstring.name (); Columndefs.add (ColumnName + "VARCHAR (" +sstring.value () + ")" +getconstraints ( Sstring.constraints ()));}} StringBuilder CreateCommand = new StringBuilder ("Create TABLE" +tablename + "("); for (String columndef:columndefs) CREATE Command.append ("\ n" +columndef+ ","); String tablecreate = createcommand.substring (0,createcommand.length ()-1) + "\ n);"; System.out.print (tablecreate);} private static String getconstraints (Constraints con) {string Constraints = ""; if (!con.allownull ()) {Constraints + = "not NULL ";} if (!con.primarykey ()) {constraints + = "PRIMARY KEY";} if (!con.unique ()) {constraints + = "unique";} return constraints;}}
Java Annotation Learning