Search in sources :

Example 16 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror 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 17 with AnnotationMirror

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

the class KeyCodeHelper method uniqueKeyCode.

public boolean uniqueKeyCode(Element element, String targetAnnotationClass) {
    int[] elementsKeyCodes = extractKeyCode(element);
    if (elementsKeyCodes.length == 0) {
        return false;
    }
    Set<Integer> uniqueKeyCodes = new HashSet<>(elementsKeyCodes.length);
    for (int keyCode : elementsKeyCodes) {
        uniqueKeyCodes.add(keyCode);
    }
    Element enclosingElement = element.getEnclosingElement();
    List<? extends Element> enclosedMethodElements = ElementFilter.methodsIn(enclosingElement.getEnclosedElements());
    for (Element oneEnclosedElement : enclosedMethodElements) {
        if (oneEnclosedElement != element) {
            List<? extends AnnotationMirror> annotationMirrors = oneEnclosedElement.getAnnotationMirrors();
            for (AnnotationMirror annotationMirror : annotationMirrors) {
                if (annotationMirror.getAnnotationType().asElement().toString().equals(targetAnnotationClass)) {
                    int[] keyCodes = extractKeyCode(oneEnclosedElement);
                    for (int keyCode : keyCodes) {
                        if (uniqueKeyCodes.contains(keyCode)) {
                            return false;
                        } else {
                            uniqueKeyCodes.add(keyCode);
                        }
                    }
                }
            }
        }
    }
    return true;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) HashSet(java.util.HashSet)

Example 18 with AnnotationMirror

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

the class RestSpringValidatorHelper method urlVariableNameExistsInEnclosingAnnotation.

public void urlVariableNameExistsInEnclosingAnnotation(Element element, ElementValidation validation) {
    Set<String> validRestMethodAnnotationNames = new HashSet<>();
    for (Class<? extends Annotation> validAnnotation : REST_ANNOTATION_CLASSES) {
        validRestMethodAnnotationNames.add(validAnnotation.getCanonicalName());
    }
    String url = null;
    for (AnnotationMirror annotationMirror : element.getEnclosingElement().getAnnotationMirrors()) {
        if (validRestMethodAnnotationNames.contains(annotationMirror.getAnnotationType().toString())) {
            url = restAnnotationHelper.extractAnnotationParameter(element.getEnclosingElement(), annotationMirror.getAnnotationType().toString(), "value");
            break;
        }
    }
    Set<String> urlVariableNames = restAnnotationHelper.extractUrlVariableNames(url);
    String expectedUrlVariableName = restAnnotationHelper.getUrlVariableCorrespondingTo((VariableElement) element);
    if (!urlVariableNames.contains(expectedUrlVariableName)) {
        validation.addError(element, "%s annotated parameter is has no corresponding url variable");
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) HashSet(java.util.HashSet)

Example 19 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror 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 20 with AnnotationMirror

use of javax.lang.model.element.AnnotationMirror in project dagger by square.

the class GeneratorKeys method get.

/** Returns the provider key for {@code variable}. */
public static String get(VariableElement variable) {
    StringBuilder result = new StringBuilder();
    AnnotationMirror qualifier = getQualifier(variable.getAnnotationMirrors());
    if (qualifier != null) {
        qualifierToString(qualifier, result);
    }
    typeToString(variable.asType(), result, '$');
    return result.toString();
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)88 TypeElement (javax.lang.model.element.TypeElement)46 AnnotationValue (javax.lang.model.element.AnnotationValue)21 Element (javax.lang.model.element.Element)19 DeclaredType (javax.lang.model.type.DeclaredType)19 ExecutableElement (javax.lang.model.element.ExecutableElement)18 VariableElement (javax.lang.model.element.VariableElement)16 TypeMirror (javax.lang.model.type.TypeMirror)16 ArrayList (java.util.ArrayList)15 HashSet (java.util.HashSet)13 List (java.util.List)12 Map (java.util.Map)10 PackageElement (javax.lang.model.element.PackageElement)7 HashMap (java.util.HashMap)5 Nullable (javax.annotation.Nullable)5 IOException (java.io.IOException)4 MoreElements.getAnnotationMirror (com.google.auto.common.MoreElements.getAnnotationMirror)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 ClassName (com.squareup.javapoet.ClassName)3