// Process each @BindView element. //获取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 annotation that corresponds to a listener. //处理监听器注解类型 for (Class<? extendsAnnotation> 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 = newArrayDeque<>(builderMap.entrySet()); Map<TypeElement, BindingSet> bindingMap = newLinkedHashMap<>(); while (!entries.isEmpty()) { //从队列中取出第一个元素 Map.Entry<TypeElement, BindingSet.Builder> entry = entries.removeFirst(); //获取对应的key和value TypeElementtype= entry.getKey(); BindingSet.Builderbuilder= entry.getValue(); //查找当前元素的父类元素 TypeElementparentType= findParentType(type, erasedTargetNames); if (parentType == null) { //没找到父类,重新放入队列 bindingMap.put(type, builder.build()); } else { //获取父类Element对应的BindingSet BindingSetparentBinding= bindingMap.get(parentType); if (parentBinding != null) { //设置父类BindingSet并添加到Map中 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); } } }
// Start by verifying common generated code restrictions. //校验对象,不能使用private和static修饰 booleanhasError= isInaccessibleViaGeneratedCode(BindView.class, "fields", element) || isBindingInWrongPackage(BindView.class, element);
// Verify that the target type extends from View. TypeMirrorelementType= element.asType(); if (elementType.getKind() == TypeKind.TYPEVAR) { TypeVariabletypeVariable= (TypeVariable) elementType; elementType = typeVariable.getUpperBound(); } //获取类名(包名+类名) NamequalifiedName= enclosingElement.getQualifiedName(); //获取成员变量名称 NamesimpleName= element.getSimpleName(); //判断当前对象是否为view的子类或者接口,不是的话抛出异常 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. //获取注解的value资源id(控件id值) intid= element.getAnnotation(BindView.class).value(); //获取父类的BindingSet.Builder BindingSet.Builderbuilder= builderMap.get(enclosingElement); //将id和对象进行绑定 QualifiedIdqualifiedId= elementToQualifiedId(element, id); if (builder != null) { //查询是否重复绑定 StringexistingBindingName= 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 { //将父类和BindingSet.Builder进行绑定并添加到builderMap中,并返回这个binder对象 builder = getOrCreateBindingBuilder(builderMap, enclosingElement); }