All say that the use of annotation is very simple, but the real we need to achieve, sometimes do not know where to start, why we feel very simple things, but can not do?
There is only one reason, and we use it very little, but for the people who write the framework, annotation is used very often. Since we usually use very little, I write this blog is meaningless, indeed, then I would like to do a review of the documents for everyone. But I still hope that we can through this blog, to strengthen the understanding of annotation, this blog omitted to annotation unnecessary knowledge points, like MU class net said only learn useful.
Enter the topic:
First, analyze the source code:
Just need to know four classes.
1.
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { RetentionPolicy value(); }
The function of this annotation is that when we need to declare the custom annotation is, need to use this annotation, but also need to set the value of values, the role of value, it is necessary to tell the second class, the use of the method, you can refer to the class itself, Add @retention (retentionpolicy.runtime) directly to the custom annotation, or another notation @retention (value = retentionpolicy.runtime), The effect is the same, it is important to note that annotation can use their own to annotate themselves.
2.
publicenum RetentionPolicy { /** * Annotation is only available in the source code. */ SOURCE, /** * Annotation is available in the source code and in the class file, but not * at runtime. This is the default policy. */ CLASS, /** * Annotation is available in the source code, the class file and is * available at runtime. */ RUNTIME }
This is an enumeration type, the function is to control the level of annotation, the higher the value, the higher the permissions, the greater the scope of the annotation, the following respectively explain the scope of the three levels:
Source: Only valid in this annotation of the original file, that is, the annotation that the permission is declared as source is invalid as long as the code has been compiled;
CLASS: The file or code has been compiled, this time is still valid, when the code ran up, there is no effect;
Runtime: This is equivalent to the maximum permissions, remember can be used at any time, and we are writing code, we want to use at runtime, so, generally we are declared as runtime permissions.
3.
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); }
This is another annotation class, the role of this class is also in our custom annotation declaration, and then determine the role of the custom annotation is that place, its value is the next class to say, the use of the same as the retention annotation, To be exact, all annotation are used in this way.
4.
public Enum ElementType {/** * class, interface or enum declaration. */ TYPE, /** * Field declaration. */ FIELD, /** * Method declaration. */ METHOD, /** * Parameter declaration. */ PARAMETER, /** * Constructor declaration. */ CONSTRUCTOR, /** * Local variable declaration. */ local_variable, /** * Annotation type declaration. */ Annotation_type, /** * Package declaration. */ Package}
This class is to determine the function position of the annotation, each different value, the corresponding position is not the same
/**
* Acting on classes, interfaces, or enumerations
*/
TYPE,
/**
* Acts on a field, that is, a member variable.
*/
FIELD,
/**
* Acting on methods.
*/
METHOD,
/**
Parameters.
*/
PARAMETER,
/**
* Construction method.
*/
CONSTRUCTOR,
/**
* Local variables.
*/
Local_variable,
/**
Annotations.
*/
Annotation_type,
/**
* Package name.
*/
Package
I just translated the English once, please correct me if it is wrong.
Basic usage is finished, and then you start customizing annotation
Second, custom annotation
Create the annotation class from Android studio, you can specify the annotation type directly when creating the class, so the file created is as follows:
/** * Created by moon.zhong on 2015/2/14. */ public @interface TestAnnotation { }
and interface very much like, just a more @, according to just said, need to add @retention (retentionpolicy.runtime) to determine the size of the permissions, here directly with the maximum permissions, and specify the type of action @target ( Elementtype.method), where the specified action is above the method
The custom annotation is complete.
Third, the use of annotation
Writing any code is not meant to be seen, so we must know how to use it.
Annotation is often used in conjunction with reflection, we are in the study of reflection, we know that many ways to get annotation value in reflection, you can also come to such a conclusion, used in the case of annotation, basically used the reflection, we often say a word, No reflection, no frame, that's why, but most frameworks use annotation for a lot of reasons
This is also used to illustrate the use of annotation with reflective Lezilai.
Write a custom annotation, and let it be scoped to the class,
/** * Created by moon.zhong on 2015/2/14. */ @Retention(value = RetentionPolicy.RUNTIME) @Target(value = ElementType.TYPE) public @interface ClassAnnotation { }
Class that defines the good one bean, plus annotations
/** * Created by moon.zhong on 2015/2/14. */ @ClassAnnotation publicclass Bean { }
Use reflection to see if annotations are working properly
public class mainactivity extends actionbaractivity { @Override protected void oncreate (Bundle savedinstancestate) {super . OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); class<?> mclass = Bean.class; Annotation annotation[] = mclass.getannotations (); for (Annotation a:annotation) {LOG.V ( "Zgy" , "====getannotations====" +a.an Notationtype ()); } } }
Last log message printed: Zgy:====getannotations====interface moon.annotationdemo.ClassAnnotation
Let's see how you can set the value for a custom annotation, like @retention (Retentionpolicy.runtime), followed by a value. In the above look at the log information, you found that more than print a interface, and followed by the annotation's full name, in fact, annotation province is a kind of interface, so when declaring variables, just define an abstract method can, do not need to implement, Full reference to public @interface Retention
/** * Created by Moon.zhong on 2015/2/ */ @Retention (value = retentionpolicy.runtime) @Target (value = elementtype.type) public @interface classannotation { String print (); } /** * Created by Moon.zhong on 2015/2/14. */ @ClassAnnotation (print = "annotation" ) public class bean {}
The
uses the same same @interface Retention, with @classannotation (print = "annotation") above the Bean's class name, here's a little bit to be aware of, not written @classannotation ("annotation"), because the method defined here is print () and can be omitted if it is replaced with value ().
/** * Created by Moon.zhong on 2015/2 /14. */ @Retention (value = retentionpolicy.runtime) @Target (value = elementtype.type) public @ interface classannotation { String value (); } /** * Created by Moon.zhong on 2015/2/14. */ @ClassAnnotation ( "annotation" ) Span class= "Hljs-keyword" >public class bean {}
来测试下: for (Annotation a : annotation){ Log.v("zgy","====print===="+((ClassAnnotation)a).print()) ; }打印log的信息 zgy﹕ ====print====annotation说明我们已经拿到了所设置的值,那么接下来要实现什么要的功能就看大家的智慧了。
Iv. Summary:
The usual, like the cooking of the last to be poured juice, here to do a summary, in the definition of annotation, you need to pay attention to two points, one, through the @retention (retentionpolicy.runtime) annotations to determine the annotation of the function of the rights, It is generally set to the maximum; second, the @target (elementtype.annotation_type) annotation is used to determine who the object of this annotation is. It is also important to note that the custom annotation if the abstract method is defined, if the abstract method name is value, the use of the time can be omitted, other times cannot be omitted, personal suggestions, all added.
Anatomy and use of Android annotation annotations