"Android" Butterknife simple principle implementation

Source: Internet
Author: User

Butterknifeprocedure

Brief introduction to the principle of butterknife

Sample Code Butterknifeprocedure

Pluggable Annotation Processing

Annotation processor
Java5 is called apt (Annotation processing Tool), which is normalized to pluggable Annotation processing at the beginning of Java6.

First step (collect information)

Find all the annotated properties or methods, and collect all the information into the corresponding "Class DataSet".

Second step (Generate source file)

Based on each "class DataSet", the corresponding Java source file is generated. Since these files are not generated at run time, there is no need for dynamic compilation, and after the annotation processor is run,
The compiler handles all compilation processes.

Step three (Dynamic injection)

Runtime dynamic injection, the butterknife.bind (activity) of a user's regular call

This step avoids bad calls and uses run-time reflection, but the author caches each class so that it does not have much impact on execution efficiency.

Note

In the latest Butterknife source code (2015.06.08), Butterknife has reconstructed some of the methods:

Butterknife#inject-Butterknife#bind

@FindView @InjectView

And so on, specific changes can be read official documents, * * This document follow-up code with the latest version of the code demo * *.

Minimalist Implementation DemoDemo Code Description
    1. The sample code is simplified by butterknife, some definitions and implementations are censored, only the View field in the Activity can be bound
    2. To avoid the introduction of the Android platform, but also need more intuitive, so mock up Android two classes, Activity and View
    3. To avoid using the jar package requirements in the pluggable Annotation processing process and to ripple through unnecessary Java files, run the demo directly using the command line./run.sh to see the results
    4. Ensure the CLASSPATH contains Tools.jar
First step (collect information)
    1. Find all the fields that are Findview annotated in each class
    2. Each field information that needs to be bound is saved as a Fieldviewbinding object, such as:

      @FindView(100)View vView1;得到:new FieldViewBinding(vView1, android.view.View, 100)
    3. Classify the fields to get the class dataset Bindingclass for each class, for example, the mainactivity corresponding "class DataSet" is as follows:

      MainActivity:    List<FieldViewBinding> fieldViewBindings = new ArrayList<FieldViewBinding>();    fieldViewBindings.add(new FieldViewBinding(vView1, android.view.View, 100))    fieldViewBindings.add(new FieldViewBinding(vView2, android.view.View, 200))
Second step (Generate Bind tool class source file)

To make it easy to instantiate the generated classes during reflection, each generated class implements a Activitybinder interface, so the files generated according to the Mainactivity "class DataSet" are as follows:

package sample;import android.view.View;import android.app.Activity;import butterknife.ButterKnife.ActivityBinder;public class MainActivity$$ViewBinder implements ActivityBinder<sample.MainActivity> {    @Override    public void bind(sample.MainActivity target) {        View view;        view = target.findViewById(100);        target.vView1 = view; //这里要求 vView1 的访问权限为 package 级别        view = target.findViewById(200);        target.vView2 = view;    }}
Step three (Dynamic injection)

The first thing we call Butterknife#bind in Mainactivity is to find the corresponding generated BIND tool class, which follows the naming convention (add $ $ViewBinder suffix after the corresponding class), directly using dynamic loading and instantiation:

Class<?> activityBindingClass = Class.forName(targetClass.getName() + ButterKnifeProcessor.SUFFIX);activityBinder = (ActivityBinder) activityBindingClass.newInstance();

After obtaining the corresponding Activitybinder, bind using Activitybinder#bind, same as manual call Findviewbyid effect

Run

Run:

ButterKnifeProcedure/src$ ./run.sh

Results:

mainActivity.vView1.id = 100mainActivity.vView2.id = 200
Android Share Q Group: 315658668

"Android" Butterknife simple principle implementation

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.