Search in sources :

Example 41 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project robolectric by robolectric.

the class ImplementsValidator method visitType.

@Override
public Void visitType(TypeElement elem, Element parent) {
    // Don't import nested classes because some of them have the same name.
    AnnotationMirror am = getCurrentAnnotation();
    AnnotationValue av = RobolectricModel.getAnnotationValue(am, "value");
    AnnotationValue cv = RobolectricModel.getAnnotationValue(am, "className");
    AnnotationValue maxSdk = RobolectricModel.getAnnotationValue(am, "maxSdk");
    // This shadow doesn't apply to the current SDK. todo: check each SDK.
    if (maxSdk != null && RobolectricModel.intVisitor.visit(maxSdk) < MAX_SUPPORTED_ANDROID_SDK) {
        String sdkClassName;
        if (av == null) {
            sdkClassName = RobolectricModel.classNameVisitor.visit(cv).replace('$', '.');
        } else {
            sdkClassName = av.toString();
        }
        // there's no such type at the current SDK level, so just use strings...
        model.addExtraShadow(sdkClassName, elem.getQualifiedName().toString());
        return null;
    }
    TypeElement type = null;
    if (av == null) {
        if (cv == null) {
            error("@Implements: must specify <value> or <className>");
            return null;
        }
        type = getClassNameTypeElement(cv);
    } else {
        TypeMirror value = RobolectricModel.valueVisitor.visit(av);
        if (value == null) {
            return null;
        }
        boolean isAnything = model.ANYTHING_MIRROR != null && types.isSameType(value, model.ANYTHING_MIRROR);
        if (isAnything) {
            if (cv == null) {
                error("@Implements: Anything class specified but no <className> attribute");
                return null;
            }
            type = getClassNameTypeElement(cv);
        } else if (cv != null) {
            error("@Implements: cannot specify both <value> and <className> attributes");
        } else {
            type = RobolectricModel.typeVisitor.visit(types.asElement(value));
        }
    }
    if (type == null) {
        return null;
    }
    final List<? extends TypeParameterElement> typeTP = type.getTypeParameters();
    final List<? extends TypeParameterElement> elemTP = elem.getTypeParameters();
    if (!model.isSameParameterList(typeTP, elemTP)) {
        StringBuilder message = new StringBuilder();
        if (elemTP.isEmpty()) {
            message.append("Shadow type is missing type parameters, expected <");
            model.appendParameterList(message, type.getTypeParameters());
            message.append('>');
        } else if (typeTP.isEmpty()) {
            message.append("Shadow type has type parameters but real type does not");
        } else {
            message.append("Shadow type must have same type parameters as its real counterpart: expected <");
            model.appendParameterList(message, type.getTypeParameters());
            message.append(">, was <");
            model.appendParameterList(message, elem.getTypeParameters());
            message.append('>');
        }
        messager.printMessage(Kind.ERROR, message, elem);
        return null;
    }
    model.addShadowType(elem, type);
    return null;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue)

Aggregations

AnnotationValue (javax.lang.model.element.AnnotationValue)41 AnnotationMirror (javax.lang.model.element.AnnotationMirror)21 TypeElement (javax.lang.model.element.TypeElement)20 List (java.util.List)15 ArrayList (java.util.ArrayList)12 ExecutableElement (javax.lang.model.element.ExecutableElement)12 TypeMirror (javax.lang.model.type.TypeMirror)11 HashSet (java.util.HashSet)8 Map (java.util.Map)8 DeclaredType (javax.lang.model.type.DeclaredType)8 ImmutableMap (com.google.common.collect.ImmutableMap)3 Element (javax.lang.model.element.Element)3 ClassName (com.squareup.javapoet.ClassName)2 MethodSpec (com.squareup.javapoet.MethodSpec)2 TypeSpec (com.squareup.javapoet.TypeSpec)2 HashMap (java.util.HashMap)2 MoreElements.getAnnotationMirror (com.google.auto.common.MoreElements.getAnnotationMirror)1 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1