ButterKnife想必每一个Android开发者都或多或少使用过,它的功能强大之处就不用多说了。它的原理可以简要概括为:编译时注解(AbstractProcessor)+反射,网上已经有很多ButterKnife源码解析相关的文章了,闲暇之余将ButterKnife工程clone下来又翻了遍源码,当作学习笔记整理下。
ButterKnife使用了编译时注解,入口就是ButterKnifeProcessor这个类,它继承自AbstractProcessor,在跟进去ButterKnifeProcessor源码前,我们先简要概括下注解处理器的概念:
注解处理器(AbstractProcessor)是用来在编译过程中扫描和处理注解的工具,我们在项目中可以为特定的注解注册自己的注解处理器,生成.java 文件,但不能修改已经存在的Java类(即不能向已有的类中添加方法)。而这些生成的Java文件,会同时与其他普通的手写Java源代码一起被javac编译。若大家有对AbstractProcessor还不太了解的童鞋,请先移步至相关文章。
好了,我们跟进去ButterKnifeProcessor首先先看下它的getSupportedAnnotationTypes方法
@Override public Set getSupportedAnnotationTypes() {
Set types = new LinkedHashSet<>();
for (Class extends Annotation> annotation : getSupportedAnnotations()) {
types.add(annotation.getCanonicalName());
}
return types;
}
//ButterKnife支持的注解类型
private Set> getSupportedAnnotations() {
Set> annotations = new LinkedHashSet<>();
annotations.add(BindAnim.class);
annotations.add(BindArray.class);
annotations.add(BindBitmap.class);
annotations.add(BindBool.class);
annotations.add(BindColor.class);
annotations.add(BindDimen.class);
annotations.add(BindDrawable.class);
annotations.add(BindFloat.class);
annotations.add(BindFont.class);
annotations.add(BindInt.class);
annotations.add(BindString.class);
annotations.add(BindView.class);
annotations.add(BindViews.class);
annotations.addAll(LISTENERS);
return annotations;
}
可以看到ButterKnife支持的注解很多,它不但支持我们常用的BindView、OnClick注解,还支持BindColor、BindDrawable等注解。接着我们跟进去ButterKnifeProcessor的process方法看下:
@Override public boolean process(Set extends TypeElement> elements, RoundEnvironment env) {
//1、调用findAndParseTargets方法,处理所有的@BindXX注解
Map bindingMap = findAndParseTargets(env);
//2、遍历bindingMap,生成相应的Java文件
for (Map.Entry 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;
}
关键部分我在上述代码中已经做了标注,我们接着跟进去findAndParseTargets方法,在findAndParseTargets方法中处理了所有支持的注解,由于该方法有点长,这里我们只关注下BindView相关的部分,其他注解原理类似:
private Map findAndParseTargets(RoundEnvironment env) {
Map builderMap = new LinkedHashMap<>();
Set erasedTargetNames = new LinkedHashSet<>();
...
// Process each @BindView element.
//1、处理每个被@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);
}
}
...
//2、调用BindingSet.Builder的build方法,生成相应的BindingSet对象,并put到bindingMap中
Deque> entries =
new ArrayDeque<>(builderMap.entrySet());
Map bindingMap = new LinkedHashMap<>();
while (!entries.isEmpty()) {
Map.Entry entry = entries.removeFirst();
TypeElement type = entry.getKey();
BindingSet.Builder builder = entry.getValue();
TypeElement parentType = findParentType(type, erasedTargetNames, classpathBindings.keySet());
if (parentType == null) {
bindingMap.put(type, builder.build());
} else {
BindingInformationProvider parentBinding = bindingMap.get(parentType);
if (parentBinding == null) {
parentBinding = classpathBindings.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);
}
}
}
//3、最后将bindingMap return掉
return bindingMap;
}
我们接着跟进去1处的parseBindView方法:
private void parseBindView(Element element, Map builderMap,
Set erasedTargetNames) {
一系列校验工作
···
if (hasError) {
return;
}
//1、通过getAnnotation.value()方法,获取到相应控件id,类似于R.id.btn_test
int id = element.getAnnotation(BindView.class).value();
BindingSet.Builder builder = builderMap.get(enclosingElement);
//2、将int类型的id包装成Id对象
Id resourceId = elementToId(element, BindView.class, id);
if (builder != null) {
String existingBindingName = builder.findExistingBindingName(resourceId);
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 {
//3、创建TypeElement相对应的BindingSet.Builder实例,并put到builderMap中
builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
}
String name = simpleName.toString();
TypeName type = TypeName.get(elementType);
boolean required = isFieldRequired(element);
builder.addField(resourceId, new FieldViewBinding(name, type, required));
// Add the type-erased version to the valid binding targets set.
erasedTargetNames.add(enclosingElement);
}
这里我们关注下3处,在调用getOrCreateBindingBuilder方法创建BindingSet.Builder对象的过程中会声明编译生成的.java文件名,相见getBindingClassName方法:
static ClassName getBindingClassName(TypeElement typeElement) {
String packageName = getPackage(typeElement).getQualifiedName().toString();
String className = typeElement.getQualifiedName().toString().substring(
packageName.length() + 1).replace('.', '$');
return ClassName.get(packageName, className + "_ViewBinding");
}
上述代码就印证了通过ButterKnife生成的Java文件名为className + "_ViewBinding"的形式。
好了,我们回到最初的ButterKnifeProcessor,接着看下process方法的2处,遍历bindingMap,生成相应的Java文件,这里生成Java文件用到了开源库javapoet。我们跟进去brewJava方法看下:
JavaFile brewJava(int sdk, boolean debuggable) {
//1、createType方法
TypeSpec bindingConfiguration = createType(sdk, debuggable);
return JavaFile.builder(bindingClassName.packageName(), bindingConfiguration)
.addFileComment("Generated code from Butter Knife. Do not modify!")
.build();
}
跟进去createType方法:
private TypeSpec createType(int sdk, boolean debuggable) {
//1、声明Java文件的类名、修饰符、是否final等,其实就是开源库javapoet的API操作
TypeSpec.Builder result = TypeSpec.classBuilder(bindingClassName.simpleName())
.addModifiers(PUBLIC)
.addOriginatingElement(enclosingElement);
if (isFinal) {
result.addModifiers(FINAL);
}
//重点:createBindingConstructor 创建构造方法
result.addMethod(createBindingConstructor(sdk, debuggable));
...
return result.build();
}
createBindingConstructor方法中又调用到了addViewBinding方法,我们跟进去看下:
private void addViewBinding(MethodSpec.Builder result, ViewBinding binding, boolean debuggable) {
if (binding.isSingleFieldBinding()) {
// Optimize the common case where there's a single binding directly to a field.
FieldViewBinding fieldBinding = requireNonNull(binding.getFieldBinding());
CodeBlock.Builder builder = CodeBlock.builder()
.add("target.$L = ", fieldBinding.getName());
//标记是否需要强转
boolean requiresCast = requiresCast(fieldBinding.getType());
if (!debuggable || (!requiresCast && !fieldBinding.isRequired())) {
if (requiresCast) {
builder.add("($T) ", fieldBinding.getType());
}
//直接拼接findViewById($L) ,$L为占位符,findViewById括号中正好为相应控件id,类似于R.id.btn_test
builder.add("source.findViewById($L)", binding.getId().code);
} else {
//通过Utils类包装了一层,内部也是调用findViewById操作
builder.add("$T.find", UTILS);
builder.add(fieldBinding.isRequired() ? "RequiredView" : "OptionalView");
if (requiresCast) {
builder.add("AsType");
}
builder.add("(source, $L", binding.getId().code);
if (fieldBinding.isRequired() || requiresCast) {
builder.add(", $S", asHumanDescription(singletonList(fieldBinding)));
}
if (requiresCast) {
builder.add(", $T.class", fieldBinding.getRawType());
}
builder.add(")");
}
result.addStatement("$L", builder.build());
return;
}
...
}
方法addViewBinding中简单明了,就是用来拼接我们在Activity或者Fragment中常写的findViewById那行代码。
到这里AbstractProcessor相关的部分我们就分析完毕了,简单讲就是在代码编译期间扫描每个Java文件中的特定的注解,通过开源库javapoet来“拼凑”成ViewBinding文件,该Java文件的命名为className + "_ViewBinding"。那么那么中间文件又是在什么时候被调用的呢?答案就是ButterKnife.bind(this);
我们跟进去ButterKnife.bind方法看下:
@NonNull @UiThread
public static Unbinder bind(@NonNull Activity target) {
View sourceView = target.getWindow().getDecorView();
return bind(target, sourceView);
}
接着跟进去bind方法:
@NonNull @UiThread
public static Unbinder bind(@NonNull Object target, @NonNull View source) {
//1、获取目标class
Class> targetClass = target.getClass();
if (debug) Log.d(TAG, "Looking up binding for " + targetClass.getName());
//2、通过反射获取对应_viewBinding类的构造方法
Constructor extends Unbinder> constructor = findBindingConstructorForClass(targetClass);
if (constructor == null) {
return Unbinder.EMPTY;
}
//noinspection TryWithIdenticalCatches Resolves to API 19+ only type.
try {
//3、创建构造对象,执行findViewById操作(在编译时,我们在构造方法中“拼接”了findViewById代码)
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);
}
}
好了,ButterKnife源码分析到这里就结束了,下一小节我们手动撸一个简易的ButterKnife框架。