Android's hottest rapid development framework Xutils annotation mechanism

Source: Internet
Author: User

In the previous article on Android's most popular rapid development framework Xutils briefly introduced the basic use of xutils, this article said xutils inside the annotation principle.

First look at the Xutils inside the demo code:

@ViewInject (r.id.tabhost)    private fragmenttabhost mtabhost; @ViewInject (r.id.big_img)    private ImageView Bigimage;

maybe a lot of people say this is a what, in fact, this is the Java core content, do Java EE should be very familiar with, like the famous spring framework with a lot of annotations. So what exactly is an annotation? The following is a detailed explanation of Java annotations:

Annotations (Annotation) provide a formalized way for us to add information to our code, which we can use at a later time (by parsing annotations to use the data), with the following common functions:

    • Generate the document. This is the most common, and also the earliest annotations that Java provides. Commonly used @see @param @return, etc.
    • Tracking code dependencies, implementing alternative profile features. The most common is the annotation-based configuration that starts with spring 2.5. The function is to reduce the configuration. Now the framework basically uses this configuration to reduce the number of configuration files. Also
    • A format check is made at compile time. such as @override before the method, if you do not override the method of the superclass method, compile time can be checked out.

The package java.lang.annotation contains all the original annotations and interfaces needed to define the custom annotations. such as interface java.lang.annotation. Annotation is an interface that is inherited by all annotations, and is automatically inherited, and is not required to be specified when defined, similar to all classes that automatically inherit object.


Java annotations are some of the meta-information that is attached to the code, used by some tools to parse and use at compile and run time, to illustrate and configure functionality. annotations do not and do not affect the actual logic of the code, only the ancillary role. Included in the Java.lang.annotation package.

annotation The parameters of the type of how to set:  
   First, This can only be decorated with public or default access rights. For example, String value (); This method is set to Defaul default type.  
   Second, Parameter members can only use basic types Byte,short,char,int,long,float,double,boolean eight basic data types and      string,enum,class. Annotations data types, as well as these types of arrays. For example, String value (), where the parameter member is string. 
   third, If there is only one parameter member, it is best to set the parameter name to "value", followed by parentheses.

1, Yuan annotation

Meta annotations are annotations of annotations. including @Retention @Target @Document @Inherited four kinds.

1.1. @Retention: Define retention policies for annotations

@Retention (Retentionpolicy.source)//annotations only exist in the source code, in the CLASS bytecode file does not include @Retention (retentionpolicy.class)//default retention policy, Annotations exist in a class bytecode file, but the runtime cannot get @Retention (retentionpolicy.runtime)//annotations are present in the class bytecode file and can be obtained through reflection at run time
1.2. @Target: Define the purpose of the annotations
The source code is defined as:    @Documented
@Retention (Retentionpolicy.runtime)
@Target (Elementtype.annotation_type)
Public @interface Target {
Elementtype[] Value ();
}

@Target (Elementtype.type)//interfaces, classes, enumerations, annotations

@Target (Elementtype.field)//fields, constants for enumerations@Target (Elementtype.method)//Method@Target (Elementtype.parameter)//Method parameters@Target (Elementtype.constructor)//constructor@Target (elementtype.local_variable)//local variable@Target (Elementtype.annotation_type)//annotations@Target (elementtype.package)///packagethe @interface is a keyword that must be defined as @interface when designing a annotations, and not with a class or interface keyword. by the above source code can know, his elementtype can have multiple, an annotation can be class, method, field and so on.
1.3. @Document: Note that the note will be included in the Javadoc 1.4, @Inherited: The description subclass can inherit this annotation from the parent class
2. Customization of Java annotations
Here is an example of a custom annotation

@Retention (retentionpolicy.runtime) The annotation defined is that the annotations exist in the class bytecode file and can be obtained through reflection at run time.

@Target ({elementtype.type,elementtype.method}) so this annotation can be a class annotation, or it can be an annotation of a method.

Such an annotation is customized, of course, the members of the annotation can be the basic data type, but also for the data, object, and so on

About the Java annotation mechanism, let's talk about the annotations used in Xutils, and the thinking process:

Package Com.lidroid.xutils.view.annotation;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target;@ Target (Elementtype.type) @Retention (retentionpolicy.runtime) public @interface contentview {    int value ();}
The above is Contentview's annotations, some declarations, parameters.

private static void Injectobject (Object handler, viewfinder Finder) {        class<?> handlertype = Handler.getclass ( );        Inject Contentview        Contentview contentview = handlertype.getannotation (contentview.class);        if (Contentview! = null) {            try {                Method Setcontentviewmethod = Handlertype.getmethod ("Setcontentview", Int.class);                Setcontentviewmethod.invoke (Handler, Contentview.value ());            } catch (Throwable e) {                logutils.e (E.getmessage (), E);}}}        

The above is a static annotated object function inside the viewutils, which uses the Contentview annotation declared above, getannotation is to get the annotated object, handler is the pointer that our activity passes in, The Handlertype,handlertype of the class type (the class of classes) is dynamically loaded Setcontentview through GetMethod, Setcontentview Everyone is familiar with the function of loading layout in Android, then get a method to reflect the mechanism to implement function loading.

Setcontentviewmethod.invoke (Handler, Contentview.value ()); This sentence can also be understood, that is, Handler has Setcontentviewmethod this method, Setcontentviewmethod The parameter of this method is Contentview.value ().

That's why it's so clear.

@ContentView (R.layout.main)
The public class MyActivity extends fragmentactivity can be implemented to load the layout, and other xutils annotation operations are similar.

Here is a simple flowchart:


If you have any questions, please leave a message, reproduced the source.


Android's hottest rapid development framework Xutils annotation mechanism

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.