Java Learning Annotations Annotation

Source: Internet
Author: User

first, What is an annotation (Annotation)?

1. concept:

Annotation are special tags in the code that can be read during compilation, class loading, runtime, and handled accordingly. By using annotations, program developers can embed some supplemental information in the source file without changing the original Logic.

2. function:

1) is not the program itself, you can explain the program

2) can be read by other programs

3. format

Annotations are present in the code with the "@ comment name", and some parameter values can be added, for example: @SuppressWarnings (value= "unchecked");

4. Where is annotation used?

Can be used for packages, classes, constructors, methods, member variables, parameters, Local variables of the declaration, the equivalent of adding additional information to them, we can be programmed through the reflection mechanism to achieve access to the metadata

second, Basic annotation,5 Basic annotation are under the Java.lang package

 1) @Override: This comment applies only to the adornment method, which indicates that a method declaration intends to override the declaration of another method in the superclass;

2) @Deprecated: used to indicate that a program element (class, method, and so On) is obsolete and the compiler will give a warning when other programs use an obsolete class or method. Indicates that programmers are not encouraged to use to classes, METHODS.

3) @SuppressWarnings: indicates that the specified compiler warning is deselected by the program element that is decorated by the annotation (and all child elements of the program element). Suppresses Compile-time warning Messages. Different from the first two comments, you need to add a parameter to use correctly, these parameters have been defined, we choose to do, the parameters are as Follows:

4) @SafeVarargs: "heap pollution" in the Java7, modify the method or constructor that raises the warning, class suppresses "heap pollution" warning;

Heap pollution: This "heap pollution" often occurs when a Non-generic object is assigned to a variable with a generic Type.

5) @FunctionallInterface: JAVA8 Specifies that if there is only one abstract method in the interface, the interface is a functional interface. @FunctionallInterface is used to specify that an interface must be a functional interface.

third, Custom Annotation

1. @interface used to declare an annotation

Format: public @interface annotation name {definition body}

2. Each of these methods actually declares a configuration parameter

1) The method name is the parameter name

2) The return value type is the type of the parameter (the return value type can only be base type, String, Class, Enum)

3) the default value of the parameter can be declared by default (1 for the parameter, indicating that the argument does not exist)

4) If there is only one parameter member, the general parameter name is value   

@Retention (value=retentionpolicy.runtime) @Target (value={elementtype.method,elementtype.type})   public @Interface  myannotation {    default "";     int default 0;     int default // does not exist }

3. Meta-annotations of jdk: used to decorate other annotation definitions. Role: responsible for annotating other annotations

1) @Retention: used to specify how long the modified annotation can be retained, @Retention contains a retentionpolicy type of value parameter configuration, therefore, You must specify a value for the value parameter when using @retention;

    

[email protected] (retentionpolicy.source): will be discarded directly by the compiler

2) @Target: used to specify which program units the modified annotation can use to modify, that is, the scope of application of the annotations; @Target contains a value parameter configuration of type elementtype, therefore, You must specify a value for the value parameter when using @target;

    

[email protected] (elementtype.field): can only modify member variables

3) @Documented: the annotation class used to specify that the annotation is modified by the Javadoc tool is extracted as a document, and if the annotation class is defined with @documentedx adornments, The annotation description will be included in the API documentation for all program elements that use the annotation adornment;

4) @Inherited: Specifies that the annotation that it modifies will have inheritance---if a class uses a @xxx annotation (@inherited adornment is used when defining the annotation), its subclasses are automatically decorated with @xxx.

4. Using reflection to read annotations: using annotations to complete the mapping of classes and table structures

    

1) Annotations of table names

@Retention (retentionpolicy.runtime) @Target (elementtype.type)  public @Interface  MyTable {    String value ();}

2) Annotation of attributes

@Retention (retentionpolicy.runtime) @Target (elementtype.field)  public @Interface  MyField {    String columnName ();    String type ();     int length ();}

3) reading annotations with reflection

1  public classDemo03 {2      public Static voidmain (string[] Args) {3         Try {4Class clzz = Class.forName ("com.java.annotation.MyStudent");5                 //get all annotations for this class6annotation[] annotations =clzz.getannotations ();7                  for(Annotation A:annotations) {8 System.out.println (a);9                 }Ten                 //gets the annotations specified by the class oneMyTable MB = (MyTable) clzz.getannotation (MyTable.class); a System.out.println (mb.value ()); -                  -                 //get annotations for the properties of a class theField f = Clzz.getdeclaredfield ("id"); -MyField MyField = f.getannotation (MyField.class); -System.out.println (myfield.columnname () + "---" +myfield.type () + "---" +myfield.length ()); -                  +}Catch(Exception E) { - e.printstacktrace (); +         } a     } at}

Java Learning Annotations Annotation

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.