Butterknife 8.8.1源碼解析

一、本文需要解決的問題

我研究Butterknife源碼的目的是為了解決以下幾個我在使用過程中所思考的問題:

  1. 在很多文章中都提到Butterknife使用編譯時注解技術,什么是編譯時注解?
  2. 是完全不調用findViewById()等方法了嗎?
  3. 為什么綁定各種view時不能使用private修飾?
  4. 綁定監(jiān)聽事件的時候方法命名有限制嗎?

二、初步分析

基于Butterknife 8.8.1版本。
為了更好地分析代碼,我寫了一個demo:
MainActivity.java:

public class MainActivity extends Activity {

    @BindView(R.id.text)
    TextView textView;

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

    @OnClick(R.id.text)
    public void textClick() {
        Toast.makeText(MainActivity.this, "textview clicked", Toast.LENGTH_LONG);
    }
}

我們從Butterknife.bind()方法,即方法入口開始分析:
ButterKnife#bind():

@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;
}

代碼還是比較清晰的,bind()方法的流程:

  1. 首先獲取當前activity的sourceView,其實就是獲取Activity的DecorView,DecorView是整個ViewTree的最頂層View,包含標題view和內容view這兩個子元素。我們一直調用的setContentView()方法其實就是往內容view中添加view元素。
  2. 然后調用createBinding() --> findBindingConstructorForClass(),重點是
Class<?> bindingClass = cls.getClassLoader().loadClass(clsName + "_ViewBinding");
bindingCtor = (Constructor<? extends Unbinder>) bindingClass.getConstructor(cls, View.class);
BINDINGS.put(cls, bindingCtor);

按照所寫的代碼,這里會加載一個MainActivity_ViewBinding類,然后獲取這個類里面的雙參數(shù)(Activity, View)構造方法,最后放在BINDINGS里面,它是一個map,主要作用是緩存。在下次使用的時候,就可以從緩存中獲取到:

Constructor<? extends Unbinder> bindingCtor = BINDINGS.get(cls);
if (bindingCtor != null) {
    if (debug) Log.d(TAG, "HIT: Cached in binding map.");
    return bindingCtor;
}

三、關于編譯時注解

在上面分析過程中,我們知道最后我們會去加載一個MainActivity_ViewBinding類,而這個類并不是我們自己編寫的,而是通過編譯時注解(APT - Annotation Processing Tool)的技術生成的。
這一節(jié)將會介紹一下這個技術。

1、什么是注解

注解其實很常見,比如說Activity自動生成的onCreate()方法上面就有一個@Override注解


image.png
  • 注解的概念:
    能夠添加到 Java 源代碼的語法元數(shù)據(jù)。類、方法、變量、參數(shù)、包都可以被注解,可用來將信息元數(shù)據(jù)與程序元素進行關聯(lián)。
  • 注解的分類:
    • 標準注解,如Override, Deprecated,SuppressWarnings等
    • 元注解,如@Retention, @Target, @Inherited, @Documented。當我們要自定義注解時,需要使用它們
    • 自定義注解,表示自己根據(jù)需要定義的 Annotation
  • 注解的作用:
    • 標記,用于告訴編譯器一些信息
    • 編譯時動態(tài)處理,如動態(tài)生成java代碼
    • 運行時動態(tài)處理,如得到注解信息
2、運行時注解 vs 編譯時注解

一般有些人提到注解,普遍就會覺得性能低下。但是真正使用注解的開源框架卻很多例如ButterKnife,Retrofit等等。所以注解是好是壞呢?
首先,并不是注解就等于性能差。更確切的說是運行時注解這種方式,由于它的原理是java反射機制,所以的確會造成較為嚴重的性能問題。
但是像Butterknife這個框架,它使用的技術是編譯時注解,它不會影響app實際運行的性能(影響的應該是編譯時的效率)。
一句話總結:

  • 運行時注解就是在應用運行的過程中,動態(tài)地獲取相關類,方法,參數(shù)等信息,由于使用java反射機制,性能會有問題;
  • 編譯時注解由于是在代碼編譯過程中對注解進行處理,通過注解獲取相關類,方法,參數(shù)等信息,然后在項目中生成代碼,運行時調用,其實和直接運行手寫代碼沒有任何區(qū)別,也就沒有性能問題了。
    這樣我們就解決了第一個問題。
3、如何使用編譯時注解技術

這里要借助到一個類:AbstractProcessor

public class TestProcessor extends AbstractProcessor  
{    
    @Override  
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)  
    {  
        // TODO Auto-generated method stub  
        return false;  
    }    
}  

重點是process()方法,它相當于每個處理器的主函數(shù)main(),可以在這里寫相關的掃描和處理注解的代碼,他會幫助生成相關的Java文件。后面我們可以具體看一下Butterknife中的使用。

四、進一步分析MainActivity_ViewBinding

我們了解了編譯時注解的基本概念之后,我們先看一下MainActivity_ViewBinding類具體實現(xiàn)了什么。
在編寫完demo之后,需要先build一下項目,之后可以在build/generated/source/apt/debug/包名/下面找到這個類,如圖所示:



接上面的分析,到最后會通過反射的方式去調用MainActivity_ViewBinding的構造方法。我們直接看這個類的構造方法:

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

    View view;
    // 1
    view = Utils.findRequiredView(source, R.id.text, "field 'textView' and method 'textClick'");
    // 2
    target.textView = Utils.castView(view, R.id.text, "field 'textView'", TextView.class);
    // 3
    view2131165290 = view;
    view.setOnClickListener(new DebouncingOnClickListener() {
        @Override
        public void doClick(View p0) {
            target.textClick();
        }
    });
}
1、findRequiredView()
public static View findRequiredView(View source, @IdRes int id, String who) {
    View view = source.findViewById(id);
    if (view != null) {
      return view;
    }
    String name = getResourceEntryName(source, id);
    throw new IllegalStateException("Required view '"
        + name
        + "' with ID "
        + id
        + " for "
        + who
        + " was not found. If this view is optional add '@Nullable' (fields) or '@Optional'"
        + " (methods) annotation.");
  }

看到這里我們已經(jīng)解決了第二個問題:到最后還是會調用findViewById()方法,并沒有完全舍棄這個方法,這里的source代表著在上面代碼中傳入的MainActivity的DecorView。大家可以嘗試一下將Activity轉化為Fragment的情況~

2、Util.castView

在這里,我們解決了第三個問題,綁定各種view時不能使用private修飾,而是需要用public或default去修飾,因為如果采用private修飾的話,將無法通過對象.成員變量方式獲取到我們需要綁定的View。
Util#castView():

public static <T> T castView(View view, @IdRes int id, String who, Class<T> cls) {
    try {
      return cls.cast(view);
    } catch (ClassCastException e) {
      String name = getResourceEntryName(view, id);
      throw new IllegalStateException("View '"
          + name
          + "' with ID "
          + id
          + " for "
          + who
          + " was of the wrong type. See cause for more info.", e);
    }
}

這里直接調用Class.cast強制轉換類型,將View轉化為我們需要的view(TextView)。

3、
view2131165290 = view;
view.setOnClickListener(new DebouncingOnClickListener() {
    @Override
    public void doClick(View p0) {
        target.textClick();
    }
});

這里會生成一個成員變量來保存我們需要綁定的View,重點是下面它會調用setOnClickListener()方法,傳入的是DebouncingOnClickListener:

/**
 * A {@linkplain View.OnClickListener click listener} that debounces multiple clicks posted in the
 * same frame. A click on one button disables all buttons for that frame.
 */
public abstract class DebouncingOnClickListener implements View.OnClickListener {
    static boolean enabled = true;

    private static final Runnable ENABLE_AGAIN = new Runnable() {
        @Override public void run() {
            enabled = true;
        }
    };

    @Override 
    public final void onClick(View v) {
        if (enabled) {
            enabled = false;
            v.post(ENABLE_AGAIN);
            doClick(v);
        }
    }

    public abstract void doClick(View v);
}

這個DebouncingOnClickListener是View.OnClickListener的一個子類,作用是防止一定時間內對view的多次點擊,即防止快速點擊控件所帶來的一些不可預料的錯誤。個人認為這個類寫的非常巧妙,既完美解決了問題,又寫的十分優(yōu)雅,一點都不臃腫。
這里抽象了doClick()方法,實現(xiàn)代碼中是直接調用了target.textClick(),這里解決了第四個問題:綁定監(jiān)聽事件的時候方法命名是沒有限制的,不一定需要嚴格命名為onClick,也不一定需要傳入View參數(shù)。

五、MainActivity_ViewBinding的生成

上文提到,MainActivity_ViewBinding類是通過編譯時注解技術生成的,我們找到Butterknife相關的繼承于AbstractProcessor的類,ButterKnifeProcessor,我們直接看process()方法:

public final class ButterKnifeProcessor extends AbstractProcessor {
    @Override 
    public boolean process(Set<? extends TypeElement> elements, RoundEnvironment env) {
        // 1
        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, debuggable);
            try {
                javaFile.writeTo(filer);
            } catch (IOException e) {
                error(typeElement, "Unable to write binding for type %s: %s", typeElement, e.getMessage());
            }
        }

        return false;
    }
}

1、findAndParseTargets()
這個方法的作用是處理所有的@BindXX注解,我們直接看處理@BindView的部分:

private Map<TypeElement, BindingSet> findAndParseTargets(RoundEnvironment env) {
    // 省略代碼
    // 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);
        }
    }
    // 省略代碼
}

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

代碼邏輯是處理獲取相關注解的信息,比如綁定的資源id等等,然后通過獲取BindingSet.Builder類的實例來創(chuàng)建一一對應的關系,這里有一個判斷,如果builderMap存在相應實例則直接取出builder,否則通過getOrCreateBindingBuilder()方法生成一個新的builder,最后調用builder.addField()方法。

后續(xù)的話返回到findAndParseTargets()方法的最后一部分:

private Map<TypeElement, BindingSet> findAndParseTargets(RoundEnvironment env) {
    // bindView()     

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

這里會生成一個bindingMap,key為TypeElement,代表注解元素類型,value為BindSet類,通過上述的builder.build()生成,BindingSet類中存儲了很多信息,例如綁定view的類型,生成類的className等等,方便我們后續(xù)生成java文件。最后回到process方法:

@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, debuggable);
      try {
        javaFile.writeTo(filer);
      } catch (IOException e) {
        error(typeElement, "Unable to write binding for type %s: %s", typeElement, e.getMessage());
      }
    }

    return false;
}

最后通過brewJava()方法生成java代碼。
這里使用到的是javapoet。javapoet是一個開源庫,通過處理相應注解來生成最后的java文件,這里是項目地址傳送門,具體技術不再分析。

這篇文章會同步到我的個人日志,如有問題,請大家踴躍提出,謝謝大家!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評論 25 708
  • 俗話說的好“不想偷懶的程序員,不是好程序員”,我們在日常開發(fā)android的過程中,在前端activity或者fr...
    蛋西閱讀 5,074評論 0 14
  • 本文主要介紹Android之神JakeWharton的一個注解框架,聽說現(xiàn)在面試官現(xiàn)在面試都會問知不知道JakeW...
    Zeit丶閱讀 1,074評論 4 6
  • 我在歸途中,而你,正在離開。我們終究不是一個世界的人,我們的距離也越來越遠……先把我拽進河里的是你,掉在河里出不來...
    00素安閱讀 152評論 0 1

友情鏈接更多精彩內容