How does xUtils encapsulate FindViewById through annotations? xutilsfindviewbyid

Source: Internet
Author: User

How does xUtils encapsulate FindViewById through annotations? xutilsfindviewbyid

Previously, I introduced the basic usage of xUtils. Today I will introduce the ViewUtils module in xUtils in detail.

In the ViewUtils module, we first see that it adopts an annotation Method for declaration, so we will first understand what annotation is.

Annotation (Annotation) is very important. Future development models are based on Annotation, JPA is based on Annotation, Spring2.5 and above are based on Annotation, and Hibernate3.x is based on Annotation, currently, part of Struts2 is based on annotations. Annotations are a trend. Many people have started to use annotations. Annotations are new features only after jdk1.5.

Three annotations provided internally after JDK1.5

@ Deprecated: "obsolete"

@ Override indicates "Rewriting and overwriting"

@ SuppressWarnings: "compression warning"

First, let's take a look at a difficult SuppressWarnings in the annotations provided by JDK, which is the value of SuppressWarnings found in the jdk1.5 document.

all  - suppress all warnings from this code  

deprecation  - suppress warnings from using deprecated code  

unchecked - suppress warnings from an unchecked call or an unchecked cast  

fallthrough - suppress warnings if a switch falls through without finding a valid case (and no default)  

serial - suppress warnings if a Serializable class does not define a serialVersionUID  

finally - suppress warnings from return within a finally (which will ignore return with the try) 

  Example:

· @ SuppressWarnings ("unchecked ")

Tells the compiler to ignore unchecked warning information, such as warnings generated without parameterization, such as using List and ArrayList.

· @ SuppressWarnings ("serial ")

If The compiler prompts a warning: The serializable class WmailCalendar does not declare a static final serialVersionUID field of type long
Use this annotation to remove the warning information.

· @ SuppressWarnings ("deprecation ")

If @ Deprecated annotation is used, the compiler displays a warning message.
Use this annotation to remove the warning information.

· @ SuppressWarnings ("unchecked", "deprecation ")

Tells the compiler to ignore both unchecked and deprecation warnings.

· @ SuppressWarnings ("unused ")

Telling the compiler to ignore unused variables at the same time

  For example

When defining a variable, a warning will appear that The local variable jerehedu is never read. Warning that our variables have not been used. When we add annotation @ SuppressWarnings ("unused ")

The warning is gone. This is the function of annotation.

After learning about the functions of annotations, let's prepare an annotation by ourselves. First, let's take a look at how the annotation is written in the ViewUtils module. Take @ ViewInject as an example.

This is the source code of viewinject. We can see it in our ViewUtils.

// inject view        Field[] fields = handlerType.getDeclaredFields();        if (fields != null && fields.length > 0) {            for (Field field : fields) {                ViewInject viewInject = field.getAnnotation(ViewInject.class);                if (viewInject != null) {                    try {                        View view = finder.findViewById(viewInject.value(), viewInject.parentId());                        if (view != null) {                            field.setAccessible(true);                            field.set(handler, view);                        }                    } catch (Throwable e) {                        LogUtils.e(e.getMessage(), e);                    }                } else {                    ResInject resInject = field.getAnnotation(ResInject.class);                    if (resInject != null) {                        try {                            Object res = ResLoader.loadRes(                                    resInject.type(), finder.getContext(), resInject.id());                            if (res != null) {                                field.setAccessible(true);                                field.set(handler, res);                            }                        } catch (Throwable e) {                            LogUtils.e(e.getMessage(), e);                        }                    } else {                        PreferenceInject preferenceInject = field.getAnnotation(PreferenceInject.class);                        if (preferenceInject != null) {                            try {                                Preference preference = finder.findPreference(preferenceInject.value());                                if (preference != null) {                                    field.setAccessible(true);                                    field.set(handler, preference);                                }                            } catch (Throwable e) {                                LogUtils.e(e.getMessage(), e);                            }                        }                    }                }            }        }

First, obtain the object annotation as ViewInject.

ViewInject viewInject = field. getAnnotation (ViewInject. class );

Encapsulate the findViewById we usually write here

FindviewbyidView view = finder. findViewById (viewInject. value (), viewInject. parentId ());

As for how to encapsulate it in our ViewFinder, We can find

This is how our xUtils uses annotations to complete secondary encapsulation of the findViewById method.

 

For questions or technical exchanges, please join the official QQ group: (452379712)

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.

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.