Android -- Annotation

Source: Internet
Author: User

Android -- Annotation
Override Annotation @ Overridepublic void onCreate (Bundle savedInstanceState) {}; concept An annotation is a form of metadata, that can be added to Java source code. classes, methods, variables, parameters and packages may be annotated. annotations have no direct effect on the operation of the code they annotate. classification Standard Annotation Override, Deprecated, SuppressWarnings meta Annotation @ Retention, @ Target, @ Inh Erited, @ brief ented custom Annotation indicates the Annotation you need to define according to your needs. The above meta-Annotation is used for definition. Custom call

   @DBTableY(name = "YYD__DB")   public class DB {       @DBColumnY(name = "column11xx")       public String column1;       @DBColumnY(name = "column22xx")       public String column2;

 

} Definition
   @Retention(RetentionPolicy.RUNTIME)   @Target(ElementType.TYPE)   public @interface DBTableY {       String name() default "YUYIDONG11";   }   @Retention(RetentionPolicy.RUNTIME)   @Target({ElementType.FIELD})   public @interface DBColumnY {       String name() default "YUYIDONG";   }

 

Through @ interface definition, the annotation name is the method name of the annotation class for the custom annotation name annotation configuration parameter name, And:. all methods have no method bodies, no parameters, no modifiers, and only public & abstract modifiers are allowed. The default value is public, and exception B is not allowed. method return values can only be basic types, String, Class, annotation, enumeration, or their one-dimensional array c. if there is only one default attribute, you can use the value () function directly. None of the attributes indicate that the Annotation is Mark Annotation. You can add default to indicate whether the default Annotation @ incluented will be saved to the Javadoc document. The optional value is SOURCE (when SOURCE code is used ), CLASS (compile time), RUNTIME (RUNTIME), default CLASS, SOURCE mostly Mark Annotation, this type of Annotation is mostly used for verification, such as Override, SuppressWarnings function: indicates the level at which the annotation information needs to be saved. It is used to describe the life cycle of the annotation (that is, the scope of the description Description in which the annotation is valid) values (RetentionPoicy) include: 1. SOURCE: valid in the SOURCE file (that is, the SOURCE file is retained) 2. CLASS: valid in the class file (that is, the class is retained) 3. RUNTIME: valid at RUNTIME (that is, reserved at RUNTIME) @ Targ Et can be used to modify program elements, such as TYPE, METHOD, CONSTRUCTOR, FIELD, and PARAMETER. If not specified, all program elements can be modified. Purpose: Describe the scope of use of the annotation (that is, where the description can be used) value (ElementType): 1. CONSTRUCTOR: used to describe the CONSTRUCTOR 2. FIELD: used to describe the domain 3. LOCAL_VARIABLE: used to describe local variables 4. METHOD: used to describe METHOD 5. PACKAGE: used to describe the PACKAGE 6. PARAMETER: used to describe PARAMETER 7. TYPE: used to describe the class, interface (including annotation TYPE), or whether the enum declaration @ Inherited can be Inherited. The default value is false. When @ Inherited annotation TYPE annotation, the Retention of annotation is RetentionPolicy. RUNTIME, the reflection API enhances this inheritance. If we use java. lang. when reflect queries an annotation of the @ Inherited annotation type, the reflection code check starts: Check the class and its parent class until the specified annotation type is found, or you can reach the top layer of the class inheritance structure. When the annotation uses the @ interface custom Annotation, the java. lang. annotation. Annotation interface is automatically inherited, and other details are automatically completed by the compiler. You cannot inherit other annotations or interfaces when defining annotations. @ Interface is used to declare an annotation. Each method actually declares a configuration parameter. The method name is the parameter name, and the return value type is the parameter type (the return value type can only be basic type, Class, String, or enum ). You can use default to declare the default value of a parameter. Public @ interface the annotation name {definition body} supports the following data types: 1. all basic data types (int, float, boolean, byte, double, char, long, short) 2. string type 3. class type 4. enum type 5. annotation type 6. all the above types of array parsing RUNTIME Annotation refers to Annotation whose @ Retention is RUNTIME
Method. getAnnotation (AnnotationName. class); method. getAnnotations (); method. isAnnotationPresent (AnnotationName. class); getAnnotation (AnnotationName. class) indicates to get the information of an Annotation of the Target. Because one Target can be modified by multiple Annotation getAnnotations (), it means to get all AnnotationisAnnotationPresent (AnnotationName. class) indicates whether the Target is modified by an Annotation Class clazz = DB. class; if (clazz. isAnnotationPresent (DBTableY. class) {DBTableY dbTableY = (DBTableY) clazz. getAnnotation (DBTableY. class); Log. I ("yyd", "dbTableY. name () ---> "+ dbTableY. name (); Field [] fields = clazz. getFields (); for (Field field: fields) {DBColumnY dbColumnY = field. getAnnotation (DBColumnY. class); Log. I ("yyd", "dbColumnY. name () ---> "+ dbColumnY. name (); Log. I ("yyd", "field. getName () ---> "+ field. getName ());}}

 

Annotation during compilation parse Annotation during compilation refers to Annotation with @ Retention being CLASS, which is automatically parsed by the compiler. The process function compiler automatically searches for all classes inherited from AbstractProcessor during compilation and calls their process method for processing.
    SupportedAnnotationTypes({ "com.yydcdut.test.annotation.DBColumnY" })      public class DBColumnProcessor extends AbstractProcessor {                @Override          public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {              for (TypeElement te : annotations) {                  for (Element element : env.getElementsAnnotatedWith(te)) {                      DBColumnY dbColumnY = element.getAnnotation(DBColumnY.class);                     Log.i("yyd",element.getEnclosingElement().toString());                  }              }              return false;          }      }

 

SupportedAnnotationTypes indicates the Annotation name to be processed by the Processor. In the process function, the annotations parameter indicates the Annotations to be processed, and the env parameter indicates the current or previous running environment. The return value of the process function indicates whether this group of annotations is accepted by this Processor. if it accepts the subsequent sub-rocessor, it will not process this Annotations.

Related Article

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.