Search in sources :

Example 1 with BindFloat

use of butterknife.BindFloat in project butterknife by JakeWharton.

the class ButterKnifeProcessor method 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)) {
        // 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)) {
        // 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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) LinkedHashMap(java.util.LinkedHashMap) BindDimen(butterknife.BindDimen) BindInt(butterknife.BindInt) BindAnim(butterknife.BindAnim) BindFont(butterknife.BindFont) BindFloat(butterknife.BindFloat) BindDrawable(butterknife.BindDrawable) BindBitmap(butterknife.BindBitmap) BindString(butterknife.BindString) TypeElement(javax.lang.model.element.TypeElement) BindArray(butterknife.BindArray) MirroredTypeException(javax.lang.model.type.MirroredTypeException) IOException(java.io.IOException) BindViews(butterknife.BindViews) ArrayDeque(java.util.ArrayDeque) BindColor(butterknife.BindColor) BindBool(butterknife.BindBool) BindView(butterknife.BindView) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with BindFloat

use of butterknife.BindFloat in project AndroidLife by CaMnter.

the class ButterKnifeProcessor method findAndParseTargets.

/**
 * 1.扫描 R class,并解析
 * 2.SuperficialValidation.validateElement(...) google auto 验证 javax Element
 * 3.处理 @BindArray
 * 4.处理 @BindBitmap
 * 5.处理 @BindBool
 * 6.处理 @BindColor
 * 7.处理 @BindDimen
 * 8.处理 @BindDrawable
 * 9.处理 @BindFloat
 * 10.处理 @BindInt
 * 11.处理 @BindString
 * 12.处理 @BindView
 * 13.处理 @BindViews
 * 14.处理事件( @OnCheckedChanged, @OnClick, @OnEditorAction, @OnFocusChange, @OnItemClick 等)
 * 15.BindingSet.Builder Map 缓存生成 BindingSet Map 缓存
 *
 * @param env RoundEnvironment
 * @return Map
 */
private Map<TypeElement, BindingSet> findAndParseTargets(RoundEnvironment env) {
    Map<TypeElement, BindingSet.Builder> builderMap = new LinkedHashMap<>();
    Set<TypeElement> erasedTargetNames = new LinkedHashSet<>();
    // TODO Mark 1
    scanForRClasses(env);
    // 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 @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)) {
        // 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)) {
        // 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);
    }
    /*
         * TODO Mark 2
         *
         * 1.遍历 BindingSet.Builder Map 缓存
         * 2.拿到每个 .java 元素 和其对应的 BindingSet.Builder 缓存
         * 3.寻找 .java 元素的父类型元素
         * - 不存在的话,就直接构建
         * - 存在的话,查看父类型的 Builder 缓存。有的话,设置 Parent,开始构建。没有的话,放入 BindingSet.Builder 缓存 的末尾
         * 4.构建后的数据,会生成 BindingSet Map 缓存
         */
    // 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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) LinkedHashMap(java.util.LinkedHashMap) BindDimen(butterknife.BindDimen) BindInt(butterknife.BindInt) BindFloat(butterknife.BindFloat) BindDrawable(butterknife.BindDrawable) BindBitmap(butterknife.BindBitmap) BindString(butterknife.BindString) TypeElement(javax.lang.model.element.TypeElement) BindArray(butterknife.BindArray) MirroredTypeException(javax.lang.model.type.MirroredTypeException) IOException(java.io.IOException) BindViews(butterknife.BindViews) ArrayDeque(java.util.ArrayDeque) BindColor(butterknife.BindColor) BindBool(butterknife.BindBool) BindView(butterknife.BindView) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with BindFloat

use of butterknife.BindFloat in project AndroidLife by CaMnter.

the class ButterKnifeProcessor method parseResourceFloat.

/**
 * 解析 BindFloat
 *
 * 1.拿到 BindFloat 注解元素 的 .java 类元素
 * 2.校验元素的类型
 * 3.检查注解使用错误,或者注解所在的环境问题( 比如 private or static,所在的 .java 是 private 等 )
 * - 有错误就 return
 * 4.获取 id 构造一个 QualifiedId;获取该 .java 元素对应的 BindingSet.Builder,没有则创建
 * 5.然后 将 QualifiedId 包装成 FieldResourceBinding 添加到 BindingSet.Builder 中
 * 6.记录 .java 元素 为要删除的目录
 *
 * @param element BindFloat 注解元素
 * @param builderMap BindingSet.Builder 缓存 Map
 * @param erasedTargetNames 要删除元素的目录,存在的是 .java 的元素
 */
private void parseResourceFloat(Element element, Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) {
    boolean hasError = false;
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
    // Verify that the target type is float.
    if (element.asType().getKind() != TypeKind.FLOAT) {
        error(element, "@%s field type must be 'float'. (%s.%s)", BindFloat.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName());
        hasError = true;
    }
    // Verify common generated code restrictions.
    hasError |= isInaccessibleViaGeneratedCode(BindFloat.class, "fields", element);
    hasError |= isBindingInWrongPackage(BindFloat.class, element);
    if (hasError) {
        return;
    }
    // Assemble information on the field.
    String name = element.getSimpleName().toString();
    int id = element.getAnnotation(BindFloat.class).value();
    QualifiedId qualifiedId = elementToQualifiedId(element, id);
    BindingSet.Builder builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
    builder.addResource(new FieldResourceBinding(getId(qualifiedId), name, FieldResourceBinding.Type.FLOAT));
    erasedTargetNames.add(enclosingElement);
}
Also used : BindFloat(butterknife.BindFloat) TypeElement(javax.lang.model.element.TypeElement) BindString(butterknife.BindString)

Aggregations

BindFloat (butterknife.BindFloat)3 BindString (butterknife.BindString)3 TypeElement (javax.lang.model.element.TypeElement)3 BindArray (butterknife.BindArray)2 BindBitmap (butterknife.BindBitmap)2 BindBool (butterknife.BindBool)2 BindColor (butterknife.BindColor)2 BindDimen (butterknife.BindDimen)2 BindDrawable (butterknife.BindDrawable)2 BindInt (butterknife.BindInt)2 BindView (butterknife.BindView)2 BindViews (butterknife.BindViews)2 IOException (java.io.IOException)2 ArrayDeque (java.util.ArrayDeque)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 Element (javax.lang.model.element.Element)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 VariableElement (javax.lang.model.element.VariableElement)2