Search in sources :

Example 61 with TypeName

use of com.squareup.javapoet.TypeName in project butterknife by JakeWharton.

the class ButterKnifeProcessor method parseBindViews.

private void parseBindViews(Element element, Map<TypeElement, BindingSet.Builder> builderMap, Set<TypeElement> erasedTargetNames) {
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
    // Start by verifying common generated code restrictions.
    boolean hasError = isInaccessibleViaGeneratedCode(BindViews.class, "fields", element) || isBindingInWrongPackage(BindViews.class, element);
    // Verify that the type is a List or an array.
    TypeMirror elementType = element.asType();
    String erasedType = doubleErasure(elementType);
    TypeMirror viewType = null;
    FieldCollectionViewBinding.Kind kind = null;
    if (elementType.getKind() == TypeKind.ARRAY) {
        ArrayType arrayType = (ArrayType) elementType;
        viewType = arrayType.getComponentType();
        kind = FieldCollectionViewBinding.Kind.ARRAY;
    } else if (LIST_TYPE.equals(erasedType)) {
        DeclaredType declaredType = (DeclaredType) elementType;
        List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
        if (typeArguments.size() != 1) {
            error(element, "@%s List must have a generic component. (%s.%s)", BindViews.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName());
            hasError = true;
        } else {
            viewType = typeArguments.get(0);
        }
        kind = FieldCollectionViewBinding.Kind.LIST;
    } else {
        error(element, "@%s must be a List or array. (%s.%s)", BindViews.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName());
        hasError = true;
    }
    if (viewType != null && viewType.getKind() == TypeKind.TYPEVAR) {
        TypeVariable typeVariable = (TypeVariable) viewType;
        viewType = typeVariable.getUpperBound();
    }
    // Verify that the target type extends from View.
    if (viewType != null && !isSubtypeOfType(viewType, VIEW_TYPE) && !isInterface(viewType)) {
        if (viewType.getKind() == TypeKind.ERROR) {
            note(element, "@%s List or array with unresolved type (%s) " + "must elsewhere be generated as a View or interface. (%s.%s)", BindViews.class.getSimpleName(), viewType, enclosingElement.getQualifiedName(), element.getSimpleName());
        } else {
            error(element, "@%s List or array type must extend from View or be an interface. (%s.%s)", BindViews.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName());
            hasError = true;
        }
    }
    // Assemble information on the field.
    String name = element.getSimpleName().toString();
    int[] ids = element.getAnnotation(BindViews.class).value();
    if (ids.length == 0) {
        error(element, "@%s must specify at least one ID. (%s.%s)", BindViews.class.getSimpleName(), enclosingElement.getQualifiedName(), element.getSimpleName());
        hasError = true;
    }
    Integer duplicateId = findDuplicate(ids);
    if (duplicateId != null) {
        error(element, "@%s annotation contains duplicate ID %d. (%s.%s)", BindViews.class.getSimpleName(), duplicateId, enclosingElement.getQualifiedName(), element.getSimpleName());
        hasError = true;
    }
    if (hasError) {
        return;
    }
    // Always false as hasError would have been true.
    assert viewType != null;
    TypeName type = TypeName.get(viewType);
    boolean required = isFieldRequired(element);
    List<Id> idVars = new ArrayList<>();
    for (int id : ids) {
        QualifiedId qualifiedId = elementToQualifiedId(element, id);
        idVars.add(getId(qualifiedId));
    }
    BindingSet.Builder builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
    builder.addFieldCollection(new FieldCollectionViewBinding(name, type, kind, idVars, required));
    erasedTargetNames.add(enclosingElement);
}
Also used : TypeName(com.squareup.javapoet.TypeName) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) BindString(butterknife.BindString) BindViews(butterknife.BindViews) ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) TypeVariable(javax.lang.model.type.TypeVariable) List(java.util.List) ArrayList(java.util.ArrayList) DeclaredType(javax.lang.model.type.DeclaredType)

Example 62 with TypeName

use of com.squareup.javapoet.TypeName in project butterknife by JakeWharton.

the class FieldCollectionViewBinding method render.

CodeBlock render() {
    CodeBlock.Builder builder = CodeBlock.builder().add("target.$L = $T.$L(", name, UTILS, kind.factoryName);
    for (int i = 0; i < ids.size(); i++) {
        if (i > 0) {
            builder.add(", ");
        }
        builder.add("\n");
        boolean requiresCast = requiresCast(type);
        if (!requiresCast && !required) {
            builder.add("source.findViewById($L)", ids.get(i).code);
        } else {
            builder.add("$T.find", UTILS);
            builder.add(required ? "RequiredView" : "OptionalView");
            if (requiresCast) {
                builder.add("AsType");
            }
            builder.add("(source, $L, \"field '$L'\"", ids.get(i).code, name);
            if (requiresCast) {
                TypeName rawType = type;
                if (rawType instanceof ParameterizedTypeName) {
                    rawType = ((ParameterizedTypeName) rawType).rawType;
                }
                builder.add(", $T.class", rawType);
            }
            builder.add(")");
        }
    }
    return builder.add(")").build();
}
Also used : TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) CodeBlock(com.squareup.javapoet.CodeBlock) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 63 with TypeName

use of com.squareup.javapoet.TypeName in project butterknife by JakeWharton.

the class BindingSet method newBuilder.

static Builder newBuilder(TypeElement enclosingElement) {
    TypeMirror typeMirror = enclosingElement.asType();
    boolean isView = isSubtypeOfType(typeMirror, VIEW_TYPE);
    boolean isActivity = isSubtypeOfType(typeMirror, ACTIVITY_TYPE);
    boolean isDialog = isSubtypeOfType(typeMirror, DIALOG_TYPE);
    TypeName targetType = TypeName.get(typeMirror);
    if (targetType instanceof ParameterizedTypeName) {
        targetType = ((ParameterizedTypeName) targetType).rawType;
    }
    String packageName = getPackage(enclosingElement).getQualifiedName().toString();
    String className = enclosingElement.getQualifiedName().toString().substring(packageName.length() + 1).replace('.', '$');
    ClassName bindingClassName = ClassName.get(packageName, className + "_ViewBinding");
    boolean isFinal = enclosingElement.getModifiers().contains(Modifier.FINAL);
    return new Builder(targetType, bindingClassName, isFinal, isView, isActivity, isDialog);
}
Also used : WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) TypeMirror(javax.lang.model.type.TypeMirror) ClassName(com.squareup.javapoet.ClassName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Aggregations

TypeName (com.squareup.javapoet.TypeName)63 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)54 ClassName (com.squareup.javapoet.ClassName)30 MethodSpec (com.squareup.javapoet.MethodSpec)27 TypeSpec (com.squareup.javapoet.TypeSpec)22 TypeMirror (javax.lang.model.type.TypeMirror)17 TypeElement (javax.lang.model.element.TypeElement)16 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)14 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)10 Map (java.util.Map)10 CodeBlock (com.squareup.javapoet.CodeBlock)9 FieldSpec (com.squareup.javapoet.FieldSpec)9 WireField (com.squareup.wire.WireField)9 Field (com.squareup.wire.schema.Field)9 ArrayList (java.util.ArrayList)9 HashSet (java.util.HashSet)7 List (java.util.List)7 Set (java.util.Set)7 AnnotationSpec (com.squareup.javapoet.AnnotationSpec)6 LinkedHashMap (java.util.LinkedHashMap)6