ButterKnife's usage and principles (5), butterknife's exploration
In this article, let's take a look at the APIS provided by ButterKnife. After the previous analysis, we have generated java code. How can we call these methods?
Actually it is called through ButterKnife. bind (); then let's look at this method.
Bind ()
/** * BindView annotated fields and methods in the specified {@link Activity}. The current content * view is used as the view root. * * @param target Target activity for view binding. */ @NonNull @UiThread public static Unbinder bind(@NonNull Activity target) { View sourceView = target.getWindow().getDecorView(); return createBinding(target, sourceView); }
This method calls the createBinding method again.
private static Unbinder createBinding(@NonNull Object target, @NonNull View source) { Class
targetClass = target.getClass(); if (debug) Log.d(TAG, "Looking up binding for " + targetClass.getName()); Constructor
constructor = findBindingConstructorForClass(targetClass); if (constructor == null) { return Unbinder.EMPTY; } //noinspection TryWithIdenticalCatches Resolves to API 19+ only type. try { return constructor.newInstance(target, source); } catch (IllegalAccessException e) { throw new RuntimeException("Unable to invoke " + constructor, e); } catch (InstantiationException e) { throw new RuntimeException("Unable to invoke " + constructor, e); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); if (cause instanceof RuntimeException) { throw (RuntimeException) cause; } if (cause instanceof Error) { throw (Error) cause; } throw new RuntimeException("Unable to create binding instance.", cause); } }
Class <\?> TargetClass = target. getClass (); obtain the class instance, obtain the constructor, and call the constructor. newInstance method .? The constructor of this class uses the findBindingConstructorForClass method.
@ Nullable @ CheckResult @ UiThread private static Constructor
FindBindingConstructorForClass (Class
Cls) {// search in the cache now. If it is found, the Constructor is returned directly.
BindingCtor = BINDINGS. get (cls); if (bindingCtor! = Null) {if (debug) Log. d (TAG, "HIT: Cached in binding map. "); return bindingCtor;} // check the validity of the class name String clsName = cls. getName (); if (clsName. startsWith ("android. ") | clsName. startsWith ("java. ") {if (debug) Log. d (TAG, "MISS: Reached framework class. abandoning search. "); return null;} try {// construct a class. here we can see that the Class name is the auxiliary class generated during information collection, such as MainActivity_ViewBinding Class.
BindingClass = Class. forName (clsName + "_ ViewBinding"); // noinspection unchecked // obtain the Constructor of the generated helper Class (also automatically generated) bindingCtor = (Constructor
) BindingClass. getConstructor (cls, View. class); if (debug) Log. d (TAG, "HIT: Loaded binding class and constructor. ");} catch (ClassNotFoundException e) {if (debug) Log. d (TAG, "Not found. trying superclass "+ cls. getSuperclass (). getName (); bindingCtor = findBindingConstructorForClass (cls. getSuperclass ();} catch (NoSuchMethodException e) {throw new RuntimeException ("Unable to find binding constructor for" + clsName, e);} // Add BINDINGS to the cache. put (cls, bindingCtor); return bindingCtor ;}Unbind ()
ButterKnife removes the unbind method after version 8.4.0 and adopts the interface form to implement the generated class. For example:
public final class SimpleAdapter$ViewHolder_ViewBinding implements Unbinder { @UiThread public SimpleAdapter$ViewHolder_ViewBinding(SimpleAdapter.ViewHolder target, View source) { //... } @Override public void unbind() { //... } }
How can I unbind it? We found that the bind method returns the Unbinder, which can be used completely.
So far, the ButterKnife source code parsing has come to an end. The principle of ButterKnife is that, on the one hand, the main purpose is to turn this principle into a node in your knowledge network; in the next article, we will use the demo to demonstrate how ButterKnife works.