Search in sources :

Example 1 with BindFont

use of butterknife.BindFont in project butterknife by JakeWharton.

the class ButterKnifeProcessor method parseResourceFont.

private void parseResourceFont(Element element, Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) {
    boolean hasError = false;
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
    // Verify that the target type is a Typeface.
    if (!TYPEFACE_TYPE.equals(element.asType().toString())) {
        error(element, "@%s field type must be 'Typeface'. (%s.%s)", BindFont.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName());
        hasError = true;
    }
    // Verify common generated code restrictions.
    hasError |= isInaccessibleViaGeneratedCode(BindFont.class, "fields", element);
    hasError |= isBindingInWrongPackage(BindFont.class, element);
    // Assemble information on the field.
    String name = element.getSimpleName().toString();
    BindFont bindFont = element.getAnnotation(BindFont.class);
    int styleValue = bindFont.style();
    TypefaceStyles style = TypefaceStyles.fromValue(styleValue);
    if (style == null) {
        error(element, "@%s style must be NORMAL, BOLD, ITALIC, or BOLD_ITALIC. (%s.%s)", BindFont.class.getSimpleName(), enclosingElement.getQualifiedName(), name);
        hasError = true;
    }
    if (hasError) {
        return;
    }
    BindingSet.Builder builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
    Id resourceId = elementToId(element, BindFont.class, bindFont.value());
    builder.addResource(new FieldTypefaceBinding(resourceId, name, style));
    erasedTargetNames.add(enclosingElement);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) TypefaceStyles(butterknife.compiler.FieldTypefaceBinding.TypefaceStyles) BindString(butterknife.BindString) BindFont(butterknife.BindFont)

Example 2 with BindFont

use of butterknife.BindFont 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<>();
    // 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);
    }
    Map<TypeElement, ClasspathBindingSet> classpathBindings = findAllSupertypeBindings(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, 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);
            }
        }
    }
    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) 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) IOException(java.io.IOException) BindViews(butterknife.BindViews) ArrayDeque(java.util.ArrayDeque) BindColor(butterknife.BindColor) BindBool(butterknife.BindBool) BindView(butterknife.BindView) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

BindFont (butterknife.BindFont)2 BindString (butterknife.BindString)2 TypeElement (javax.lang.model.element.TypeElement)2 BindAnim (butterknife.BindAnim)1 BindArray (butterknife.BindArray)1 BindBitmap (butterknife.BindBitmap)1 BindBool (butterknife.BindBool)1 BindColor (butterknife.BindColor)1 BindDimen (butterknife.BindDimen)1 BindDrawable (butterknife.BindDrawable)1 BindFloat (butterknife.BindFloat)1 BindInt (butterknife.BindInt)1 BindView (butterknife.BindView)1 BindViews (butterknife.BindViews)1 TypefaceStyles (butterknife.compiler.FieldTypefaceBinding.TypefaceStyles)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 IOException (java.io.IOException)1 ArrayDeque (java.util.ArrayDeque)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1