Search in sources :

Example 66 with Element

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

the class ValidatorHelper method typeOrTargetValueHasAnnotation.

public void typeOrTargetValueHasAnnotation(Class<? extends Annotation> annotation, Element element, ElementValidation valid) {
    Element targetElement = findTargetElement(element, valid);
    if (targetElement == null) {
        return;
    }
    DeclaredType targetAnnotationClassValue = annotationHelper.extractAnnotationClassParameter(element);
    if (targetAnnotationClassValue != null) {
        typeHasAnnotation(annotation, targetAnnotationClassValue, valid);
        if (!annotationHelper.getTypeUtils().isAssignable(targetAnnotationClassValue, targetElement.asType())) {
            valid.addError("The value of %s must be assignable into the annotated field");
        }
    } else {
        typeHasAnnotation(annotation, targetElement, valid);
    }
}
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) DeclaredType(javax.lang.model.type.DeclaredType)

Example 67 with Element

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

the class ValidatorHelper method allowedType.

public void allowedType(Element element, List<String> allowedTypes, ElementValidation valid) {
    String qualifiedName;
    Element enclosingElement = element.getEnclosingElement();
    if (element instanceof VariableElement && enclosingElement instanceof ExecutableElement) {
        qualifiedName = element.asType().toString();
    } else if (element instanceof ExecutableElement) {
        element = ((ExecutableElement) element).getParameters().get(0);
        qualifiedName = element.asType().toString();
    } else {
        qualifiedName = element.asType().toString();
    }
    if (!allowedTypes.contains(qualifiedName)) {
        valid.addError("%s can only be used on a field which is a " + allowedTypes.toString() + ", not " + qualifiedName);
    }
}
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) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 68 with Element

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

the class BaseGeneratedClassHolder method setGeneratedClass.

protected void setGeneratedClass() throws Exception {
    String annotatedComponentQualifiedName = annotatedElement.getQualifiedName().toString();
    annotatedClass = getCodeModel().directClass(annotatedElement.asType().toString());
    if (annotatedElement.getNestingKind().isNested()) {
        Element enclosingElement = annotatedElement.getEnclosingElement();
        GeneratedClassHolder enclosingHolder = environment.getGeneratedClassHolder(enclosingElement);
        String generatedBeanSimpleName = annotatedElement.getSimpleName().toString() + classSuffix();
        int modifier = PUBLIC | STATIC;
        if (environment.getOptionBooleanValue(OPTION_GENERATE_FINAL_CLASSES)) {
            modifier |= FINAL;
        }
        generatedClass = enclosingHolder.getGeneratedClass()._class(modifier, generatedBeanSimpleName, EClassType.CLASS);
    } else {
        String generatedClassQualifiedName = annotatedComponentQualifiedName + classSuffix();
        int modifier = PUBLIC;
        if (environment.getOptionBooleanValue(OPTION_GENERATE_FINAL_CLASSES)) {
            modifier |= FINAL;
        }
        generatedClass = getCodeModel()._class(modifier, generatedClassQualifiedName, EClassType.CLASS);
    }
    codeModelHelper.generify(generatedClass, annotatedElement);
    setExtends();
    codeModelHelper.copyNonAAAnnotations(generatedClass, annotatedElement.getAnnotationMirrors());
}
Also used : Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement)

Example 69 with Element

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

the class RestServiceHandler method validate.

@Override
public void validate(Element element, ElementValidation validation) {
    injectHelper.validate(RestService.class, element, validation);
    if (!validation.isValid()) {
        return;
    }
    validatorHelper.isNotPrivate(element, validation);
    Element param = injectHelper.getParam(element);
    validatorHelper.typeHasAnnotation(Rest.class, param, validation);
}
Also used : Element(javax.lang.model.element.Element)

Example 70 with Element

use of javax.lang.model.element.Element in project antlr4 by antlr.

the class CommentHasStringValueProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    //		Messager messager = processingEnv.getMessager();
    //		messager.printMessage(Diagnostic.Kind.NOTE, "PROCESS--------------------");
    Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(CommentHasStringValue.class);
    for (Element annotatedElement : annotatedElements) {
        String docComment = utilities.getDocComment(annotatedElement);
        JCTree.JCLiteral literal = treeMaker.Literal(docComment != null ? docComment : "");
        JCTree elementTree = utilities.getTree(annotatedElement);
        if (elementTree instanceof JCTree.JCVariableDecl) {
            ((JCTree.JCVariableDecl) elementTree).init = literal;
        } else if (elementTree instanceof JCTree.JCMethodDecl) {
            JCTree.JCStatement[] statements = new JCTree.JCStatement[1];
            statements[0] = treeMaker.Return(literal);
            JCTree.JCBlock body = treeMaker.Block(0, List.from(statements));
            ((JCTree.JCMethodDecl) elementTree).body = body;
        }
    }
    return true;
}
Also used : Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) JCTree(com.sun.tools.javac.tree.JCTree)

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