Search in sources :

Example 21 with ArrayType

use of javax.lang.model.type.ArrayType in project auto by google.

the class AutoAnnotationProcessor method compatibleTypes.

/**
   * Returns true if {@code parameterType} can be used to provide the value of an annotation member
   * of type {@code memberType}. They must either be the same type, or the member type must be an
   * array and the parameter type must be a collection of a compatible type.
   */
private boolean compatibleTypes(TypeMirror parameterType, TypeMirror memberType) {
    if (typeUtils.isAssignable(parameterType, memberType)) {
        // is a subtype of that annotation interface (why would you do that?).
        return true;
    }
    // to Collection<Integer> (in this example).
    if (memberType.getKind() != TypeKind.ARRAY) {
        return false;
    }
    TypeMirror arrayElementType = ((ArrayType) memberType).getComponentType();
    TypeMirror wrappedArrayElementType = arrayElementType.getKind().isPrimitive() ? typeUtils.boxedClass((PrimitiveType) arrayElementType).asType() : arrayElementType;
    TypeElement javaUtilCollection = processingEnv.getElementUtils().getTypeElement(Collection.class.getCanonicalName());
    DeclaredType collectionOfElement = typeUtils.getDeclaredType(javaUtilCollection, wrappedArrayElementType);
    return typeUtils.isAssignable(parameterType, collectionOfElement);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Collection(java.util.Collection) DeclaredType(javax.lang.model.type.DeclaredType)

Example 22 with ArrayType

use of javax.lang.model.type.ArrayType 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 23 with ArrayType

use of javax.lang.model.type.ArrayType in project butterknife by JakeWharton.

the class ButterKnifeProcessor method getArrayResourceMethodName.

/**
   * Returns a method name from the {@link android.content.res.Resources} class for array resource
   * binding, null if the element type is not supported.
   */
private static FieldResourceBinding.Type getArrayResourceMethodName(Element element) {
    TypeMirror typeMirror = element.asType();
    if (TYPED_ARRAY_TYPE.equals(typeMirror.toString())) {
        return FieldResourceBinding.Type.TYPED_ARRAY;
    }
    if (TypeKind.ARRAY.equals(typeMirror.getKind())) {
        ArrayType arrayType = (ArrayType) typeMirror;
        String componentType = arrayType.getComponentType().toString();
        if (STRING_TYPE.equals(componentType)) {
            return FieldResourceBinding.Type.STRING_ARRAY;
        } else if ("int".equals(componentType)) {
            return FieldResourceBinding.Type.INT_ARRAY;
        } else if ("java.lang.CharSequence".equals(componentType)) {
            return FieldResourceBinding.Type.TEXT_ARRAY;
        }
    }
    return null;
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) BindString(butterknife.BindString)

Example 24 with ArrayType

use of javax.lang.model.type.ArrayType in project RoboBinding by RoboBinding.

the class TypeMirrorWrapperTest method supportedTypeMirrors.

@DataPoints("supportedTypeMirrors")
public static TypeMirrorToWrapped[] supportedTypeMirrors() {
    Types types = compilation.getTypes();
    NoType voidType = types.getNoType(TypeKind.VOID);
    PrimitiveType primitiveType = types.getPrimitiveType(TypeKind.INT);
    Elements elements = compilation.getElements();
    DeclaredType declaredType = (DeclaredType) elements.getTypeElement(Object.class.getName()).asType();
    ArrayType arrayType = types.getArrayType(declaredType);
    return new TypeMirrorToWrapped[] { a(voidType).itsWrapped(WrappedVoidType.class), a(primitiveType).itsWrapped(WrappedPrimitiveType.class), a(declaredType).itsWrapped(WrappedDeclaredType.class), a(arrayType).itsWrapped(WrappedArrayType.class) };
}
Also used : ArrayType(javax.lang.model.type.ArrayType) Types(javax.lang.model.util.Types) NoType(javax.lang.model.type.NoType) PrimitiveType(javax.lang.model.type.PrimitiveType) Elements(javax.lang.model.util.Elements) DeclaredType(javax.lang.model.type.DeclaredType) FromDataPoints(org.junit.experimental.theories.FromDataPoints) DataPoints(org.junit.experimental.theories.DataPoints)

Example 25 with ArrayType

use of javax.lang.model.type.ArrayType in project vertx-docgen by vert-x3.

the class Helper method toString.

/**
 * Compute the string representation of a type mirror.
 *
 * @param mirror the type mirror
 * @param buffer the buffer appended with the string representation
 */
static void toString(TypeMirror mirror, StringBuilder buffer) {
    if (mirror instanceof DeclaredType) {
        DeclaredType dt = (DeclaredType) mirror;
        TypeElement elt = (TypeElement) dt.asElement();
        buffer.append(elt.getQualifiedName().toString());
        List<? extends TypeMirror> args = dt.getTypeArguments();
        if (args.size() > 0) {
            buffer.append("<");
            for (int i = 0; i < args.size(); i++) {
                if (i > 0) {
                    buffer.append(",");
                }
                toString(args.get(i), buffer);
            }
            buffer.append(">");
        }
    } else if (mirror instanceof PrimitiveType) {
        PrimitiveType pm = (PrimitiveType) mirror;
        buffer.append(pm.getKind().name().toLowerCase());
    } else if (mirror instanceof javax.lang.model.type.WildcardType) {
        javax.lang.model.type.WildcardType wt = (javax.lang.model.type.WildcardType) mirror;
        buffer.append("?");
        if (wt.getSuperBound() != null) {
            buffer.append(" super ");
            toString(wt.getSuperBound(), buffer);
        } else if (wt.getExtendsBound() != null) {
            buffer.append(" extends ");
            toString(wt.getExtendsBound(), buffer);
        }
    } else if (mirror instanceof javax.lang.model.type.TypeVariable) {
        javax.lang.model.type.TypeVariable tv = (TypeVariable) mirror;
        TypeParameterElement elt = (TypeParameterElement) tv.asElement();
        buffer.append(elt.getSimpleName().toString());
        if (tv.getUpperBound() != null && !tv.getUpperBound().toString().equals("java.lang.Object")) {
            buffer.append(" extends ");
            toString(tv.getUpperBound(), buffer);
        } else if (tv.getLowerBound() != null && tv.getLowerBound().getKind() != TypeKind.NULL) {
            buffer.append(" super ");
            toString(tv.getUpperBound(), buffer);
        }
    } else if (mirror instanceof javax.lang.model.type.ArrayType) {
        javax.lang.model.type.ArrayType at = (ArrayType) mirror;
        toString(at.getComponentType(), buffer);
        buffer.append("[]");
    } else {
        throw new UnsupportedOperationException("todo " + mirror + " " + mirror.getKind());
    }
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeElement(javax.lang.model.element.TypeElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) ArrayType(javax.lang.model.type.ArrayType) TypeVariable(javax.lang.model.type.TypeVariable) TypeVariable(javax.lang.model.type.TypeVariable) PrimitiveType(javax.lang.model.type.PrimitiveType) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

ArrayType (javax.lang.model.type.ArrayType)25 TypeMirror (javax.lang.model.type.TypeMirror)18 DeclaredType (javax.lang.model.type.DeclaredType)12 TypeElement (javax.lang.model.element.TypeElement)10 Test (org.junit.Test)7 Expression (com.google.devtools.j2objc.ast.Expression)4 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)3 VariableElement (javax.lang.model.element.VariableElement)3 PrimitiveType (javax.lang.model.type.PrimitiveType)3 TypeVariable (javax.lang.model.type.TypeVariable)3 BindString (butterknife.BindString)2 ArrayInitializer (com.google.devtools.j2objc.ast.ArrayInitializer)2 FieldAccess (com.google.devtools.j2objc.ast.FieldAccess)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)2 SimpleName (com.google.devtools.j2objc.ast.SimpleName)2 Statement (com.google.devtools.j2objc.ast.Statement)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)2 ArrayList (java.util.ArrayList)2