19.Java Annotations1.Java built-in annotation----annotation code
@Deprecated//deprecated outdated methods
@Deprecatedpublic void Badmethod () { System.out.println ("I am a old function");}
@Override//Must be a function that overrides the parent class (interface)
@Overridepublic String ToString () { return "override ToString ()";}
@SuppressWarnings//close inappropriate compile-time warnings
2. Custom annotations
A. Annotations that do not have any elements---tag annotations
B. General annotation definition, use of ordinary annotations
/** * is the class */public @interface isclass { String value (); Boolean IsClass () default true;}
Explanation: The annotation has two elements of value and the purpose of the Isclass,isclass element is to mark whether the object is a class, and the element defaults to True.
When the annotation element has a default value, the element named value is assigned without a write name
3. Meta-annotations
Note: meta-annotations are annotations that are used to modify annotations, limit the annotation scope of annotations, the "source level, runtime level, class file level" of the annotation's life cycle, whether it can be inherited, whether a document is created, etc.
4. Runtime annotations
Annotations like spring are very much related to run-time annotations, the following is a table name with annotations, table names, row names for tables, table column values three properties
4.1 Defining run-time annotations
- Table is a tabular name for a two-dimensional table
- Columns is a column of a two-dimensional table
- Key is a row of a two-dimensional table
Import java.lang.annotation.*;/** * Created by Yangyun on 2016/12/27. */@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documentedpublic @interface Column { String Value ();} @Target (Elementtype.field) @Retention (retentionpolicy.runtime) @interface key{ String value (); @Target (Elementtype.type) @Retention (retentionpolicy.runtime) @Documented @interface Table { String value ();// Name of the table}
4.2 Using annotations and writing related annotation information get the class
- The tables class contains the information we need, table annotation annotations on the table class, key and column annotations on field
- The GetInformation class is an operation class that gets the relevant annotation information, using the reflection mechanism of Java
Import java.util.list;/** * Created by Yangyun on 2016/12/27. */@Empty (field = "Not Empty") @IsClass (the value of the ' Value property ') public class Annotation {public static void main (string[] args) { System.out.format ("tablename:%s\n", Getinformation.getname (Tables.class)); try {System.out.format ("key:%s\n", Getinformation.getkey (Tables.class)); } catch (Nosuchfieldexception e) {e.printstacktrace (); } System.out.format ("columns:%s\n", Getinformation.getcols (Tables.class)); }} @Table ("Students") class getinformation{public static String GetName (class<tables> tablesclass) {string Strtablename=null; if (Tablesclass.isannotationpresent (Table.class)) {table tablename=tablesclass.getannotation (Table.class);//Table name Strtablename=tablename.value (); } return strtablename; } public static String GetKey (class<tables> tablesclass) throws nosuchfieldexception {Field Field=tablesc Lass.Getdeclaredfield ("key");//Gets the private property String Strkey=null; if (field!=null) {Key key=field.getannotation (key.class); if (key!=null) {strkey=key.value (); }} return strkey; } public static list<string> Getcols (class<tables> tablesclass) {list<string> columns=new Arra Ylist<string> (); Field[] Fields=tablesclass.getdeclaredfields (); if (fields!=null) {for Field field:fields) {if (Field.isannotationpresent (Column.class)) { Columns.Add (Field.getannotation (Column.class). value ()); }}} return columns; }} @Table ("Students") class tables{@Key ("Id") private String Key; @Column ("name") private String col1; @Column ("Sex") private String col2;
4.3 Introduction to the main API (not the knowledge of reflection)
- 1.myclass.getannotations ()---Get all the annotations for this class
- 2.myclass.getannotation (Class annotationtype)-Get Specific annotation types
- 3.myclass.isannotationpresent (Class annotationtype)-Determine if there is a specific type of annotation
- 4.myclass.getdeclareannotations ()-Gets all annotations of the class excluding annotations inherited from the parent class
19.Java annotations