示例#1
0
  private void parseBindText(
      Element element,
      Map<TypeElement, BindingClass> targetClassMap,
      Set<String> erasedTargetNames) {
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();

    TypeMirror elementType = element.asType();
    if (elementType.getKind() == TypeKind.TYPEVAR) {
      TypeVariable typeVariable = (TypeVariable) elementType;
      elementType = typeVariable.getUpperBound();
    }
    // Assemble information on the field.
    int[] ids = element.getAnnotation(BindText.class).value();
    BindingClass bindingClass =
        getOrCreateTargetClass(targetClassMap, enclosingElement, false, false);
    for (int id : ids) {
      if (bindingClass != null) {
        KBindings bindings = bindingClass.getKBindings(String.valueOf(id));
        if (bindings != null) {
          Iterator<FieldViewBinding> iterator = bindings.getFieldBindings().iterator();
          if (iterator.hasNext()) {
            FieldViewBinding existingBinding = iterator.next();
            error(
                element,
                "Attempt to use @%s for an already bound ID %s on '%s'. (%s.%s)",
                BindText.class.getSimpleName(),
                id,
                existingBinding.getName(),
                enclosingElement.getQualifiedName(),
                element.getSimpleName());
            return;
          }
        }
      } else {
        bindingClass = getOrCreateTargetClass(targetClassMap, enclosingElement, false, false);
      }
      String name = element.getSimpleName().toString();
      TypeName type = TypeName.get(elementType);
      boolean required = isRequiredBinding(element);

      FieldViewBinding binding = new FieldViewBinding(name, type, required);
      bindingClass.addField(String.valueOf(id), binding);
    }

    // Add the type-erased version to the valid binding targets set.
    erasedTargetNames.add(enclosingElement.toString());
  }
  private void parseBindOne(
      Element element,
      Map<TypeElement, BindingClass> targetClassMap,
      Set<String> erasedTargetNames) {
    boolean hasError = false;
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();

    // Verify that the target type extends from View.
    TypeMirror elementType = element.asType();
    if (elementType.getKind() == TypeKind.TYPEVAR) {
      TypeVariable typeVariable = (TypeVariable) elementType;
      elementType = typeVariable.getUpperBound();
    }
    if (!isSubtypeOfType(elementType, VIEW_TYPE) && !isInterface(elementType)) {
      error(
          element,
          "@%s fields must extend from View or be an interface. (%s.%s)",
          Bind.class.getSimpleName(),
          enclosingElement.getQualifiedName(),
          element.getSimpleName());
      hasError = true;
    }

    // Assemble information on the field.
    int[] ids = element.getAnnotation(Bind.class).value();
    if (ids.length != 1) {
      error(
          element,
          "@%s for a view must only specify one ID. Found: %s. (%s.%s)",
          Bind.class.getSimpleName(),
          Arrays.toString(ids),
          enclosingElement.getQualifiedName(),
          element.getSimpleName());
      hasError = true;
    }

    if (hasError) {
      return;
    }

    int id = ids[0];
    BindingClass bindingClass = targetClassMap.get(enclosingElement);
    if (bindingClass != null) {
      ViewBindings viewBindings = bindingClass.getViewBinding(id);
      if (viewBindings != null) {
        Iterator<FieldViewBinding> iterator = viewBindings.getFieldBindings().iterator();
        if (iterator.hasNext()) {
          FieldViewBinding existingBinding = iterator.next();
          error(
              element,
              "Attempt to use @%s for an already bound ID %d on '%s'. (%s.%s)",
              Bind.class.getSimpleName(),
              id,
              existingBinding.getName(),
              enclosingElement.getQualifiedName(),
              element.getSimpleName());
          return;
        }
      }
    } else {
      bindingClass = getOrCreateTargetClass(targetClassMap, enclosingElement);
    }

    String name = element.getSimpleName().toString();
    String type = elementType.toString();
    boolean required = isRequiredBinding(element);

    FieldViewBinding binding = new FieldViewBinding(name, type, required);
    bindingClass.addField(id, binding);

    // Add the type-erased version to the valid binding targets set.
    erasedTargetNames.add(enclosingElement.toString());
  }