Android Butterknife 框架源码解析(3)——Butterknife 8.7.0源码分析

前文链接地址:

《Android Butterknife 框架源码解析(1)——ButterKnife的使用》 http://blog.csdn.net/bit_kaki/article/details/74926076

《Android Butterknife 框架源码解析(2)——谈谈Java的注解》 http://blog.csdn.net/bit_kaki/article/details/75039597


  前两篇分别说了下ButterKnife的用法以及讲了下什么是注解,接下来就是要说一下ButterKnife的源码了,通过源码来看ButterKnife是如何实现的。本章的ButterKnife基于目前最新的8.7.0.。ButterKnife的github地址为https://github.com/JakeWharton/butterknife


      我们继续使用第一篇中ButterKnife的使用例子,其代码为:

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.textView)
    TextView textView;

    @OnClick(R.id.button)
    public void click(){
        textView.setText("我被点击了一下");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_knife );
        ButterKnife.bind(this);
    }
}


      这段代码很简单,就是执行了两个Button和一点Button点击事件的绑定。我们就以这段代码为例,来看看Butterknife的源码和执行的原理。


注解的源码分析

  首先我们看看@BindView注解的源码:

@Retention(CLASS) @Target(FIELD)
public @interface BindView {
  /** View ID to which the field will be bound. */
  @IdRes int value();
}


   还有@OnClick的源码:

@Target(METHOD)
@Retention(CLASS)
@ListenerClass(
    targetType = "android.view.View",
    setter = "setOnClickListener",
    type = "butterknife.internal.DebouncingOnClickListener",
    method = @ListenerMethod(
        name = "doClick",
        parameters = "android.view.View"
    )
)
public @interface OnClick {
  /** View IDs to which the method will be bound. */
  @IdRes int[] value() default { View.NO_ID };
}

   通过上一篇注解的学习,我们可以看到这两个注解的结构,但是我们也看到了它们的@Retention的值是CLASS,也就是说它们存活时间在编译器里,当程序运行时候jvm将会抛弃他们。上一篇我们举的通过注解实现@BindView的例子是在程序运行时,通过反射找到被注解的控件,实现了绑定的方法,很明显在ButterKnife里它并不是这样实现的。接下来我们继续看代码。


ButterKnife的源码分析

  我们也知道注解也是一个标记,并不实际运行内容,于是看看ButterKnife的使用里,真正运行的是ButterKnife.bind(this)这个方法。点开ButterKnife类,我们可以看到它的源码是:

/**
 * 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);
}

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<? extends Unbinder> 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);
  }
}

@Nullable @CheckResult @UiThread
private static Constructor<? extends Unbinder> findBindingConstructorForClass(Class<?> cls) {
  Constructor<? extends Unbinder> bindingCtor = BINDINGS.get(cls);
  if (bindingCtor != null) {
    if (debug) Log.d(TAG, "HIT: Cached in binding map.");
    return bindingCtor;
  }
  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 {
    Class<?> bindingClass = cls.getClassLoader().loadClass(clsName + "_ViewBinding");
    //noinspection unchecked
    bindingCtor = (Constructor<? extends Unbinder>) 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);
  }
  BINDINGS.put(cls, bindingCtor);
  return bindingCtor;
}


      这两个是绑定的具体方法,看着有点乱,于是我们把log日志和特殊情况判定去掉看就是:

Class<?> targetClass = target.getClass();
Constructor<? extends Unbinder> bindingCtor = BINDINGS.get(targetClass);String clsName = targetClass.getName();Class<?> bindingClass = targetClass.getClassLoader().loadClass(clsName + "_ViewBinding");bindingCtor = (Constructor<? extends Unbinder>) bindingClass.getConstructor(targetClass, View.class);BINDINGS.put(cls, bindingCtor);return bindingCtor;

      可以看到其实就是通过反射去查询获取一个名称为传入类的类名后加上“_ViewBinder”的类,并获取对应的ViewBinder。举例子,我bind的是MainActivity,那么对应的ViewBinder就是MainAcitivity$$ViewBinder。然后将这个类的实例放入到一个map里以供获取。


ButterKnifeProcessor的源码分析

      然后呢,好像代码里就没啥了。但是这个className+“_ViewBinder”是怎么来的呢。这里需要说明下,对于注释在编译时候解析是通过我们自定义一个继承 AbstractProcessor的类来完成的。所以我们找找ButterKnife的源码,在butterknife-compiler module里找到了该类,如图所示:




      接下来看一下该类的核心代码:

@Override public boolean process(Set<? extends TypeElement> elements, RoundEnvironment env) {
  Map<TypeElement, BindingSet> bindingMap = findAndParseTargets(env);

  for (Map.Entry<TypeElement, BindingSet> entry : bindingMap.entrySet()) {
    TypeElement typeElement = entry.getKey();
    BindingSet binding = entry.getValue();
    JavaFile javaFile = binding.brewJava(sdk);
    try {
      javaFile.writeTo(filer);
    } catch (IOException e) {
      error(typeElement, "Unable to write binding for type %s: %s", typeElement, e.getMessage());
    }
  }

  return false;
}


          可以看出这个类主要就是做了两件事,首先是通过findAndParseTargets方法找出该类里所有注解,然后通过binding.brewJava方法将这些注解进行处理。

      首先来看看findAndParseTargets方法:

private Map<TypeElement, BindingSet> findAndParseTargets(RoundEnvironment env) {
  Map<TypeElement, BindingSet.Builder> builderMap = new LinkedHashMap<>();
  Set<TypeElement> erasedTargetNames = new LinkedHashSet<>();

  scanForRClasses(env);

  // Process each @BindAnim element.
  for (Element element : env.getElementsAnnotatedWith(BindAnim.class)) {
    if (!SuperficialValidation.validateElement(element)) continue;
    try {
      parseResourceAnimation(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindAnim.class, e);
    }
  }

  // Process each @BindArray element.
  for (Element element : env.getElementsAnnotatedWith(BindArray.class)) {
    if (!SuperficialValidation.validateElement(element)) continue;
    try {
      parseResourceArray(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindArray.class, e);
    }
  }

  // Process each @BindBitmap element.
  for (Element element : env.getElementsAnnotatedWith(BindBitmap.class)) {
    if (!SuperficialValidation.validateElement(element)) continue;
    try {
      parseResourceBitmap(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindBitmap.class, e);
    }
  }

  // Process each @BindBool element.
  for (Element element : env.getElementsAnnotatedWith(BindBool.class)) {
    if (!SuperficialValidation.validateElement(element)) continue;
    try {
      parseResourceBool(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindBool.class, e);
    }
  }

  // Process each @BindColor element.
  for (Element element : env.getElementsAnnotatedWith(BindColor.class)) {
    if (!SuperficialValidation.validateElement(element)) continue;
    try {
      parseResourceColor(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindColor.class, e);
    }
  }

  // Process each @BindDimen element.
  for (Element element : env.getElementsAnnotatedWith(BindDimen.class)) {
    if (!SuperficialValidation.validateElement(element)) continue;
    try {
      parseResourceDimen(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindDimen.class, e);
    }
  }

  // Process each @BindDrawable element.
  for (Element element : env.getElementsAnnotatedWith(BindDrawable.class)) {
    if (!SuperficialValidation.validateElement(element)) continue;
    try {
      parseResourceDrawable(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindDrawable.class, e);
    }
  }

  // Process each @BindFloat element.
  for (Element element : env.getElementsAnnotatedWith(BindFloat.class)) {
    if (!SuperficialValidation.validateElement(element)) continue;
    try {
      parseResourceFloat(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindFloat.class, e);
    }
  }

  // Process each @BindFont element.
  for (Element element : env.getElementsAnnotatedWith(BindFont.class)) {
    if (!SuperficialValidation.validateElement(element)) continue;
    try {
      parseResourceFont(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindFont.class, e);
    }
  }

  // Process each @BindInt element.
  for (Element element : env.getElementsAnnotatedWith(BindInt.class)) {
    if (!SuperficialValidation.validateElement(element)) continue;
    try {
      parseResourceInt(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindInt.class, e);
    }
  }

  // Process each @BindString element.
  for (Element element : env.getElementsAnnotatedWith(BindString.class)) {
    if (!SuperficialValidation.validateElement(element)) continue;
    try {
      parseResourceString(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindString.class, e);
    }
  }

  // Process each @BindView element.
  for (Element element : env.getElementsAnnotatedWith(BindView.class)) {
    // we don't SuperficialValidation.validateElement(element)
    // so that an unresolved View type can be generated by later processing rounds
    try {
      parseBindView(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindView.class, e);
    }
  }

  // Process each @BindViews element.
  for (Element element : env.getElementsAnnotatedWith(BindViews.class)) {
    // we don't SuperficialValidation.validateElement(element)
    // so that an unresolved View type can be generated by later processing rounds
    try {
      parseBindViews(element, builderMap, erasedTargetNames);
    } catch (Exception e) {
      logParsingError(element, BindViews.class, e);
    }
  }

  // Process each annotation that corresponds to a listener.
  for (Class<? extends Annotation> listener : LISTENERS) {
    findAndParseListener(env, listener, builderMap, erasedTargetNames);
  }

  // Associate superclass binders with their subclass binders. This is a queue-based tree walk
  // which starts at the roots (superclasses) and walks to the leafs (subclasses).
  Deque<Map.Entry<TypeElement, BindingSet.Builder>> entries =
      new ArrayDeque<>(builderMap.entrySet());
  Map<TypeElement, BindingSet> bindingMap = new LinkedHashMap<>();
  while (!entries.isEmpty()) {
    Map.Entry<TypeElement, BindingSet.Builder> entry = entries.removeFirst();

    TypeElement type = entry.getKey();
    BindingSet.Builder builder = entry.getValue();

    TypeElement parentType = findParentType(type, erasedTargetNames);
    if (parentType == null) {
      bindingMap.put(type, builder.build());
    } else {
      BindingSet parentBinding = bindingMap.get(parentType);
      if (parentBinding != null) {
        builder.setParent(parentBinding);
        bindingMap.put(type, builder.build());
      } else {
        // Has a superclass binding but we haven't built it yet. Re-enqueue for later.
        entries.addLast(entry);
      }
    }
  }

  return bindingMap;
}

      代码挺长的,其实就是将该类里所有的注解按照不同的注解类型做出对应的处理,然后分类保存起来。以我们用到的BindView为例子,其对应的代码是:

for (Element element : env.getElementsAnnotatedWith(BindView.class)) {
  // we don't SuperficialValidation.validateElement(element)
  // so that an unresolved View type can be generated by later processing rounds
  try {
    parseBindView(element, builderMap, erasedTargetNames);
  } catch (Exception e) {
    logParsingError(element, BindView.class, e);
  }
}


      然后看看parseBindView方法:

private void parseBindView(Element element, Map<TypeElement, BindingSet.Builder> builderMap,
    Set<TypeElement> erasedTargetNames) {
  TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();

  // Start by verifying common generated code restrictions.
  boolean hasError = isInaccessibleViaGeneratedCode(BindView.class, "fields", element)
      || isBindingInWrongPackage(BindView.class, element);

  // Verify that the target type extends from View.
  TypeMirror elementType = element.asType();
  if (elementType.getKind() == TypeKind.TYPEVAR) {
    TypeVariable typeVariable = (TypeVariable) elementType;
    elementType = typeVariable.getUpperBound();
  }
  Name qualifiedName = enclosingElement.getQualifiedName();
  Name simpleName = element.getSimpleName();
  if (!isSubtypeOfType(elementType, VIEW_TYPE) && !isInterface(elementType)) {
    if (elementType.getKind() == TypeKind.ERROR) {
      note(element, "@%s field with unresolved type (%s) "
              + "must elsewhere be generated as a View or interface. (%s.%s)",
          BindView.class.getSimpleName(), elementType, qualifiedName, simpleName);
    } else {
      error(element, "@%s fields must extend from View or be an interface. (%s.%s)",
          BindView.class.getSimpleName(), qualifiedName, simpleName);
      hasError = true;
    }
  }

  if (hasError) {
    return;
  }

  // Assemble information on the field.
  int id = element.getAnnotation(BindView.class).value();

  BindingSet.Builder builder = builderMap.get(enclosingElement);
  QualifiedId qualifiedId = elementToQualifiedId(element, id);
  if (builder != null) {
    String existingBindingName = builder.findExistingBindingName(getId(qualifiedId));
    if (existingBindingName != null) {
      error(element, "Attempt to use @%s for an already bound ID %d on '%s'. (%s.%s)",
          BindView.class.getSimpleName(), id, existingBindingName,
          enclosingElement.getQualifiedName(), element.getSimpleName());
      return;
    }
  } else {
    builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
  }

  String name = simpleName.toString();
  TypeName type = TypeName.get(elementType);
  boolean required = isFieldRequired(element);

  builder.addField(getId(qualifiedId), new FieldViewBinding(name, type, required));

  // Add the type-erased version to the valid binding targets set.
  erasedTargetNames.add(enclosingElement);
}


      我们依然去掉一些特殊情况的判断看看主要代码为:

private void parseBindView(Element element, Map<TypeElement, BindingSet.Builder> builderMap,
    Set<TypeElement> erasedTargetNames) {
  TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
  TypeMirror elementType = element.asType();
  
  Name qualifiedName = enclosingElement.getQualifiedName();
  Name simpleName = element.getSimpleName();
    
  int id = element.getAnnotation(BindView.class).value();

  BindingSet.Builder builder = builderMap.get(enclosingElement);
  QualifiedId qualifiedId = elementToQualifiedId(element, id);
  builder = getOrCreateBindingBuilder(builderMap, enclosingElement);

  String name = simpleName.toString();
  TypeName type = TypeName.get(elementType);
  boolean required = isFieldRequired(element);

  builder.addField(getId(qualifiedId), new FieldViewBinding(name, type, required));
  // Add the type-erased version to the valid binding targets set.
  erasedTargetNames.add(enclosingElement);
}


       可以看出这段代码的核心就是创建一个BindingSet抽象类,然后将我们注解信息处理后保存在里面。

     findAndParseTargets()方法的分析到此结束,简单来说就是遍历该类里所有的注解,并进行分类预处理保存。


     接下来是binding.brewJava()方法了,binding就是刚刚findAndParseTargets()方法所获取的BindingSet,我们看看接下来的代码是什么:

JavaFile brewJava(int sdk) {
  return JavaFile.builder(bindingClassName.packageName(), createType(sdk))
      .addFileComment("Generated code from Butter Knife. Do not modify!")
      .build();
}
private TypeSpec createType(int sdk) {
  TypeSpec.Builder result = TypeSpec.classBuilder(bindingClassName.simpleName())
      .addModifiers(PUBLIC);
  if (isFinal) {
    result.addModifiers(FINAL);
  }

  if (parentBinding != null) {
    result.superclass(parentBinding.bindingClassName);
  } else {
    result.addSuperinterface(UNBINDER);
  }

  if (hasTargetField()) {
    result.addField(targetTypeName, "target", PRIVATE);
  }

  if (isView) {
    result.addMethod(createBindingConstructorForView());
  } else if (isActivity) {
    result.addMethod(createBindingConstructorForActivity());
  } else if (isDialog) {
    result.addMethod(createBindingConstructorForDialog());
  }
  if (!constructorNeedsView()) {
    // Add a delegating constructor with a target type + view signature for reflective use.
    result.addMethod(createBindingViewDelegateConstructor());
  }
  result.addMethod(createBindingConstructor(sdk));

  if (hasViewBindings() || parentBinding == null) {
    result.addMethod(createBindingUnbindMethod(result));
  }

  return result.build();
}


      可以看出这一段的代码采用了JavaPoet,一个非常强大的代码生成工具。根据我们的注解内容,通过TypeSpec类和MethodSpec类构造出对应的方法,然后根据之前创建BindingSet抽象类时候创建的新类名:

ClassName bindingClassName = ClassName.get(packageName, className + "_ViewBinding");

      通过JavaFile创建出新的类。而这个新生成的className+_ViewBinding“”类,我们可以在ButterKnife.bind时候获取到它,然后就通过这个类里的方法,实现我们控件和监听方法的绑定了。


新生成的_ViewBinder类

      好了,源码分析完了,那让我们来看看这个新生成类的内容吧。我们先运行下程序,然后可以看到我们生成的MainActivity_ViewBinding类:


      

      点开看看它的代码为:

public class MainActivity_ViewBinding implements Unbinder {
  private MainActivity target;

  private View view2131427415;

  @UiThread
  public MainActivity_ViewBinding(MainActivity target) {
    this(target, target.getWindow().getDecorView());
  }

  @UiThread
  public MainActivity_ViewBinding(final MainActivity target, View source) {
    this.target = target;

    View view;
    view = Utils.findRequiredView(source, R.id.button, "field 'button' and method 'click'");
    target.button = Utils.castView(view, R.id.button, "field 'button'", Button.class);
    view2131427415 = view;
    view.setOnClickListener(new DebouncingOnClickListener() {
      @Override
      public void doClick(View p0) {
        target.click();
      }
    });
    target.textView = Utils.findRequiredViewAsType(source, R.id.textView, "field 'textView'", TextView.class);
  }

  @Override
  @CallSuper
  public void unbind() {
    MainActivity target = this.target;
    if (target == null) throw new IllegalStateException("Bindings already cleared.");
    this.target = null;

    target.button = null;
    target.textView = null;

    view2131427415.setOnClickListener(null);
    view2131427415 = null;
  }
}


      其中这个@UiThread是相当于AsyncTask的onPostExecute方法,而里面的控件都是通过内部方法实现findViewById。


总结:

      通过三篇博客,我们对ButterKnife的源码实现进行了具体的分析,可以得到以下一些结论:

  1. ButterKnife使用上是通过注解来实现控件和响应事件的绑定,让代码的阅读性更高,书写更加方便;
  2. ButterKnife是通过在编译时候生成对应的className_ViewBinder类来辅助实现className类的控件和响应事件的绑定,所有绑定相关方法都在className_ViewBinder类里实现;
  3. ButterKnife是程序运行到ButterKnife.bind(this)的方法时候执行的,先通过反射获取对应的className_ViewBinder类,然后实现控件和响应事件的绑定;
  4. ButterKnife不能将控件和方法设置为private或者static,是因为在className_ViewBinder类会直接调用该控件和方法进行赋值。

参考文章:
      http://blog.csdn.net/Charon_Chui/article/details/51908109
      http://blog.csdn.net/f2006116/article/details/51921288
      http://www.jianshu.com/p/da288e9ee3eb

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值