Search in sources :

Example 6 with AnnotationValue

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

the class RobolectricModel method getImplementedClass.

public TypeMirror getImplementedClass(AnnotationMirror am) {
    if (am == null) {
        return null;
    }
    // RobolectricWiringTest prefers className (if provided) to value, so we do the same here.
    TypeMirror impType = getImplementedClassName(am);
    if (impType != null) {
        return impType;
    }
    AnnotationValue av = getAnnotationValue(am, "value");
    if (av == null) {
        return null;
    }
    TypeMirror type = valueVisitor.visit(av);
    if (type == null) {
        return null;
    }
    // If the class is Robolectric.Anything, treat as if it wasn't specified at all.
    if (ANYTHING_MIRROR != null && types.isSameType(type, ANYTHING_MIRROR)) {
        return null;
    }
    return type;
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 7 with AnnotationValue

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

the class RobolectricModel method getImplementedClassName.

private TypeMirror getImplementedClassName(AnnotationMirror am) {
    AnnotationValue className = getAnnotationValue(am, "className");
    if (className == null) {
        return null;
    }
    String classNameString = classNameVisitor.visit(className);
    if (classNameString == null) {
        return null;
    }
    TypeElement impElement = elements.getTypeElement(classNameString.replace('$', '.'));
    if (impElement == null) {
        return null;
    }
    return impElement.asType();
}
Also used : TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue)

Example 8 with AnnotationValue

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

the class AnnotationHelper method extractAnnotationClassParameter.

public DeclaredType extractAnnotationClassParameter(Element element, String annotationName, String methodName) {
    AnnotationMirror annotationMirror = findAnnotationMirror(element, annotationName);
    Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues();
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) {
        /*
			 * "methodName" is unset when the default value is used
			 */
        if (methodName.equals(entry.getKey().getSimpleName().toString())) {
            AnnotationValue annotationValue = entry.getValue();
            return (DeclaredType) annotationValue.getValue();
        }
    }
    return null;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotationValue(javax.lang.model.element.AnnotationValue) Map(java.util.Map) DeclaredType(javax.lang.model.type.DeclaredType)

Example 9 with AnnotationValue

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

the class RoboGuiceHandler method extractListenerTypeMirrors.

private List<TypeMirror> extractListenerTypeMirrors(Element activityElement) {
    List<? extends AnnotationMirror> annotationMirrors = activityElement.getAnnotationMirrors();
    String annotationName = RoboGuice.class.getName();
    for (AnnotationMirror annotationMirror : annotationMirrors) {
        if (annotationName.equals(annotationMirror.getAnnotationType().toString())) {
            for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()) {
                if ("value".equals(entry.getKey().getSimpleName().toString())) {
                    AnnotationValue action = entry.getValue();
                    @SuppressWarnings("unchecked") List<AnnotationValue> elements = (List<AnnotationValue>) action.getValue();
                    List<TypeMirror> listenerTypeMirrors = new ArrayList<>(elements.size());
                    for (AnnotationValue annotationValue : elements) {
                        listenerTypeMirrors.add((TypeMirror) annotationValue.getValue());
                    }
                    return listenerTypeMirrors;
                }
            }
        }
    }
    return Collections.emptyList();
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) AnnotationValue(javax.lang.model.element.AnnotationValue) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 10 with AnnotationValue

use of javax.lang.model.element.AnnotationValue in project javapoet by square.

the class AnnotationSpec method get.

public static AnnotationSpec get(AnnotationMirror annotation) {
    TypeElement element = (TypeElement) annotation.getAnnotationType().asElement();
    AnnotationSpec.Builder builder = AnnotationSpec.builder(ClassName.get(element));
    Visitor visitor = new Visitor(builder);
    for (ExecutableElement executableElement : annotation.getElementValues().keySet()) {
        String name = executableElement.getSimpleName().toString();
        AnnotationValue value = annotation.getElementValues().get(executableElement);
        value.accept(visitor, name);
    }
    return builder.build();
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) 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