19.Java annotations

Source: Internet
Author: User

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
    • @Target---------------used to describe the scope of annotations
        • Eg:type: Used to describe classes, interfaces (including annotation types), or enum declarations
    • @Retention-----------used to describe the life cycle of annotations
        • Source: Valid in source file (that is, source file reserved)---compile-time function
        • Runtime: Valid at run time (that is, run-time retention)----run-time function
        • Class: Valid in class file (that is, class reserved) [default]
    • @Documented ———— modified annotations are written to Javadoc (tag annotations)
    • @Inherited---allow subclasses to inherit annotations from the parent class [annotations themselves are not inheritable] can use getannotations () reflection to get the parent class to be
    • @Target (Elementtype.type) @Documented @inherited@retention (retentionpolicy.runtime) public @interface IsClass {    String value ();    Boolean IsClass () default true;}

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

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.