Explanation of the xutils mechanism of the Android fire frame

Source: Internet
Author: User

In the previous article on Android's hottest high-speed development framework Xutils introduced the basic usage of xutils, this article describes the annotation principle in xutils.

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, it is the content of Java core. The Java EE should be very familiar. A lot of annotations are used like the famous spring framework. So what exactly is an annotation? Here is a detailed explanation of the Java annotations:

Annotations (Annotation) provide a formalized way for us to include information in our code. Is that we are able to use this data conveniently at a later time (by parsing annotations to use this data), and there are several common functions:

    • Generate the document.

      This is the most common, and also the earliest annotations that Java provides. Frequent use of @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. Today's frameworks basically use this configuration to reduce the number of configuration files. Also

    • A format check is made at compile time. If the @override is placed before the method. Let's say that you're not overriding the superclass approach. It can be checked out at compile time.

The package java.lang.annotation includes all the original annotations and interfaces needed to define your own definition annotations. such as interface java.lang.annotation. Annotation is an interface that is inherited by all annotations, and is self-inheriting and does not need to be specified when defined, similar to the fact that all classes themselves inherit object.


Java annotations are some of the meta-information that is attached to the code, used by some tools to parse and use during compilation and execution, to illustrate and configure functionality. annotations do not and cannot affect the actual logic of the code. Only play an auxiliary role.

Included in the Java.lang.annotation package.

annotation type in the parameters of how to set:  
   First, can only be modified with public or default access rights. For example, String value (); This method is set to Defaul default type.  
   Second, The parameter members can only use the basic type 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 member is string. 
   third, Assuming that there is only one member, it is best to set the parameter name to "value", followed by parentheses.

1, Yuan annotation

Meta annotations are annotations of annotations.

Contains @Retention @Target @Document @Inherited four kinds.

1.1. @Retention: Define retention policies for annotations

@Retention (Retentionpolicy.source)//annotations exist only in the source code. @Retention (Retentionpolicy.class)//default retention policy is not included in the class bytecode file, the annotations exist in the class bytecode file but cannot be @Retention when executed ( Retentionpolicy.runtime)//annotations are present in the class bytecode file and can be obtained through reflection at execution time.
1.2. @Target: Define the purpose of the annotations
The source code for the definition is:    @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)///packageamong the @interface is a keyword. When designing a annotations, you must define a type as @interface, not class or Interfacekeyword .by the above source code can know that his elementtype can have multiple, an annotation can be for the class, method of. fields, 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, Java annotations of their own definition
here is a sample of your own definition of annotations

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

@Target ({elementtype.type,elementtype.method}) so this annotation can be a class annotation. can also be the annotation of the method

Such an annotation is defined by itself, and of course the members of the annotation can be the primary data type. can also be for data, object, etc.

probably understand the Java annotation mechanism, the following is said Xutils in the use of annotations, as well as 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 the Contentview note, some declarations, the number of references.

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 annotation object function inside the viewutils, which uses the Contentview annotation declared above. Getannotation is the object that gets the annotations. Handler is the pointer that our activity passes in. Get class type by pointer(This is the class class)The Handlertype. Handlertype GetMethod dynamically loaded into Setcontentview,setcontentview everyone is very familiar with the loading layout function of Android. Then we get a method to reflect the mechanism and implement the function loading.

Setcontentviewmethod.invoke (Handler, Contentview.value ()); This sentence can also be understood. That's the way handler has Setcontentviewmethod. the Setcontentviewmethod of this method is Contentview.value ().

That makes it clear why.

@ContentView (R.layout.main)
The public class MyActivity extends Fragmentactivity is able to implement the loading layout, and the other xutils annotation operations are similar.

Here is a simple flowchart:


Please leave a message if you have any questions. Reproduced in the source. http://blog.csdn.net/rain_butterfly/article/details/37931031


Explanation of the xutils mechanism of the Android fire frame

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.