Search in sources :

Example 61 with Element

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

the class IdValidatorHelper method uniqueResourceId.

public void uniqueResourceId(Element element, Res resourceType, ElementValidation valid) {
    if (valid.isValid()) {
        List<String> annotationQualifiedIds = idAnnotationHelper.extractAnnotationResources(element, resourceType, true);
        Element elementEnclosingElement = element.getEnclosingElement();
        Set<? extends Element> annotatedElements = validatedModel().getRootAnnotatedElements(annotationHelper.getTarget());
        for (Element uniqueCheckElement : annotatedElements) {
            Element uniqueCheckEnclosingElement = uniqueCheckElement.getEnclosingElement();
            if (elementEnclosingElement.equals(uniqueCheckEnclosingElement)) {
                List<String> checkQualifiedIds = idAnnotationHelper.extractAnnotationResources(uniqueCheckElement, resourceType, true);
                for (String checkQualifiedId : checkQualifiedIds) {
                    for (String annotationQualifiedId : annotationQualifiedIds) {
                        if (annotationQualifiedId.equals(checkQualifiedId)) {
                            String annotationSimpleId = annotationQualifiedId.substring(annotationQualifiedId.lastIndexOf('.') + 1);
                            valid.addError("The resource id " + annotationSimpleId + " is already used on the following " + annotationHelper.annotationName() + " method: " + uniqueCheckElement);
                            return;
                        }
                    }
                }
            }
        }
    }
}
Also used : Element(javax.lang.model.element.Element)

Example 62 with Element

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

the class InjectHelper method validate.

public void validate(Class<? extends Annotation> expectedAnnotation, Element element, ElementValidation valid) {
    Element enclosingElement = element.getEnclosingElement();
    if (element instanceof VariableElement && enclosingElement instanceof ExecutableElement) {
        validatorHelper.param.annotatedWith(expectedAnnotation).multiple().validate((ExecutableElement) enclosingElement, valid);
        validatorHelper.doesNotHaveAnyOfSupportedAnnotations(enclosingElement, valid);
        handler.validateEnclosingElement(enclosingElement, valid);
    } else if (element instanceof ExecutableElement) {
        handler.validateEnclosingElement(element, valid);
        validatorHelper.param.anyType().validate((ExecutableElement) element, valid);
        List<? extends VariableElement> parameters = ((ExecutableElement) element).getParameters();
        for (VariableElement param : parameters) {
            validatorHelper.doesNotHaveAnyOfSupportedAnnotations(param, valid);
        }
    } else {
        handler.validateEnclosingElement(element, valid);
    }
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) ArrayList(java.util.ArrayList) List(java.util.List) VariableElement(javax.lang.model.element.VariableElement)

Example 63 with Element

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

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

the class ValidatorHelper method enclosingElementHasAnnotation.

public void enclosingElementHasAnnotation(Class<? extends Annotation> annotation, Element element, ElementValidation valid, String error) {
    Element enclosingElement = element.getEnclosingElement();
    elementHasAnnotation(annotation, enclosingElement, valid, error);
}
Also used : VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element)

Example 65 with Element

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

the class ValidatorHelper method extendsMenuItem.

public void extendsMenuItem(Element element, ElementValidation valid) {
    Element enclosingElement = element.getEnclosingElement();
    String enclosingQualifiedName = enclosingElement.asType().toString();
    TypeElement enclosingTypeElement = annotationHelper.typeElementFromQualifiedName(enclosingQualifiedName);
    if (enclosingTypeElement != null) {
        extendsType(element, CanonicalNameConstants.MENU_ITEM, valid);
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element)

Aggregations

Element (javax.lang.model.element.Element)286 TypeElement (javax.lang.model.element.TypeElement)227 ExecutableElement (javax.lang.model.element.ExecutableElement)148 VariableElement (javax.lang.model.element.VariableElement)96 TypeMirror (javax.lang.model.type.TypeMirror)68 PackageElement (javax.lang.model.element.PackageElement)48 ArrayList (java.util.ArrayList)39 DeclaredType (javax.lang.model.type.DeclaredType)30 IOException (java.io.IOException)29 Map (java.util.Map)26 HashSet (java.util.HashSet)23 LinkedHashSet (java.util.LinkedHashSet)22 List (java.util.List)22 Set (java.util.Set)22 Test (org.junit.Test)21 ElementKind (javax.lang.model.element.ElementKind)20 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Elements (javax.lang.model.util.Elements)18 HashMap (java.util.HashMap)16 LinkedHashMap (java.util.LinkedHashMap)15