Search in sources :

Example 46 with Element

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

the class EntityType method process.

@Override
public Set<ElementValidator> process(ProcessingEnvironment processingEnvironment) {
    // create attributes for fields that have no annotations
    if (element().getKind().isInterface() || isImmutable() || isUnimplementable()) {
        ElementFilter.methodsIn(element().getEnclosedElements()).stream().filter(this::isMethodProcessable).forEach(this::computeAttribute);
    } else {
        // private/static/final members fields are skipped
        Set<VariableElement> elements = ElementFilter.fieldsIn(element().getEnclosedElements()).stream().filter(element -> !element.getModifiers().contains(Modifier.PRIVATE) && !element.getModifiers().contains(Modifier.STATIC) && (!element.getModifiers().contains(Modifier.FINAL) || isImmutable())).collect(Collectors.toSet());
        if (elements.isEmpty()) {
            // if nothing to process try the getters instead
            ElementFilter.methodsIn(element().getEnclosedElements()).stream().filter(this::isMethodProcessable).forEach(this::computeAttribute);
        } else {
            elements.forEach(this::computeAttribute);
        }
    }
    // find listener annotated methods
    ElementFilter.methodsIn(element().getEnclosedElements()).forEach(element -> ListenerAnnotations.all().forEach(annotation -> {
        if (element.getAnnotation(annotation) != null) {
            ListenerMethod listener = listeners.computeIfAbsent(element, key -> new ListenerMethod(element));
            listener.annotations().put(annotation, element.getAnnotation(annotation));
        }
    }));
    Set<ProcessableElement<?>> elements = new LinkedHashSet<>();
    attributes().values().forEach(attribute -> elements.add((ProcessableElement<?>) attribute));
    elements.addAll(listeners.values());
    Set<ElementValidator> validations = new LinkedHashSet<>();
    elements.forEach(element -> validations.addAll(element.process(processingEnvironment)));
    ElementValidator validator = new ElementValidator(element(), processingEnvironment);
    Entity entity = annotationOf(Entity.class).orElse(null);
    if (entity != null && !Names.isEmpty(entity.name()) && !SourceVersion.isIdentifier(entity.name())) {
        validator.error("Invalid class identifier " + entity.name(), Entity.class);
    }
    if (element().getNestingKind() == NestingKind.ANONYMOUS) {
        validator.error("Entity annotation cannot be applied to anonymous class");
    }
    if (element().getKind() == ElementKind.ENUM) {
        validator.error("Entity annotation cannot be applied to an enum class");
    }
    if (attributes.values().isEmpty()) {
        validator.warning("Entity contains no attributes");
    }
    if (!isReadOnly() && !isEmbedded() && attributes.values().size() == 1 && attributes.values().iterator().next().isGenerated()) {
        validator.warning("Entity contains only a single generated attribute may fail to persist");
    }
    checkReserved(tableName(), validator);
    validations.add(validator);
    return validations;
}
Also used : Transient(io.requery.Transient) Table(io.requery.Table) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Embedded(io.requery.Embedded) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) MirroredTypeException(javax.lang.model.type.MirroredTypeException) PropertyNameStyle(io.requery.PropertyNameStyle) NestingKind(javax.lang.model.element.NestingKind) ElementFilter(javax.lang.model.util.ElementFilter) LinkedHashSet(java.util.LinkedHashSet) Name(javax.lang.model.element.Name) View(io.requery.View) Entity(io.requery.Entity) ElementKind(javax.lang.model.element.ElementKind) ExecutableElement(javax.lang.model.element.ExecutableElement) Cacheable(javax.persistence.Cacheable) Set(java.util.Set) ReadOnly(io.requery.ReadOnly) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Stream(java.util.stream.Stream) Index(javax.persistence.Index) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Annotation(java.lang.annotation.Annotation) Factory(io.requery.Factory) Optional(java.util.Optional) Embeddable(javax.persistence.Embeddable) LinkedHashSet(java.util.LinkedHashSet) Entity(io.requery.Entity) VariableElement(javax.lang.model.element.VariableElement)

Example 47 with Element

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

the class EntityType method factoryArguments.

@Override
public List<String> factoryArguments() {
    List<String> names = new ArrayList<>();
    ExecutableElement method = factoryMethod().orElseThrow(IllegalStateException::new);
    // TODO need more validation here
    // now match the builder fields to the parameters...
    Map<Element, AttributeDescriptor> map = new LinkedHashMap<>(attributes);
    for (VariableElement parameter : method.getParameters()) {
        // straight forward case type and name are the same
        Element matched = null;
        for (Map.Entry<Element, AttributeDescriptor> entry : map.entrySet()) {
            AttributeDescriptor attribute = entry.getValue();
            String fieldName = attribute.fieldName();
            if (fieldName.equalsIgnoreCase(parameter.getSimpleName().toString())) {
                names.add(fieldName);
                matched = entry.getKey();
            }
        }
        if (matched != null) {
            map.remove(matched);
        }
    }
    // didn't work likely because the parameter names are missing
    if (names.isEmpty()) {
        // for kotlin data classes add processable element field names in order
        if (isUnimplementable()) {
            ElementFilter.methodsIn(element().getEnclosedElements()).stream().filter(this::isMethodProcessable).forEach(getter -> names.addAll(map.entrySet().stream().filter(entry -> entry.getKey().equals(getter)).map(entry -> entry.getValue().fieldName()).collect(Collectors.toList())));
        } else {
            for (Map.Entry<Element, AttributeDescriptor> entry : map.entrySet()) {
                names.add(0, entry.getValue().fieldName());
            }
        }
    }
    return names;
}
Also used : Transient(io.requery.Transient) Table(io.requery.Table) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Embedded(io.requery.Embedded) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) MirroredTypeException(javax.lang.model.type.MirroredTypeException) PropertyNameStyle(io.requery.PropertyNameStyle) NestingKind(javax.lang.model.element.NestingKind) ElementFilter(javax.lang.model.util.ElementFilter) LinkedHashSet(java.util.LinkedHashSet) Name(javax.lang.model.element.Name) View(io.requery.View) Entity(io.requery.Entity) ElementKind(javax.lang.model.element.ElementKind) ExecutableElement(javax.lang.model.element.ExecutableElement) Cacheable(javax.persistence.Cacheable) Set(java.util.Set) ReadOnly(io.requery.ReadOnly) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Stream(java.util.stream.Stream) Index(javax.persistence.Index) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Annotation(java.lang.annotation.Annotation) Factory(io.requery.Factory) Optional(java.util.Optional) Embeddable(javax.persistence.Embeddable) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 48 with Element

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

the class EntityType method merge.

@Override
public void merge(EntityDescriptor from) {
    from.attributes().entrySet().forEach(entry -> {
        if (attributes.values().stream().noneMatch(attribute -> attribute.name().equals(entry.getValue().name()))) {
            Element element = entry.getValue().element();
            attributes.put(entry.getKey(), new AttributeMember(element, this));
        }
    });
    from.listeners().entrySet().stream().filter(entry -> entry.getValue() instanceof ListenerMethod).forEach(entry -> {
        ListenerMethod method = (ListenerMethod) entry.getValue();
        if (listeners.values().stream().noneMatch(listener -> listener.element().getSimpleName().equals(method.element().getSimpleName()))) {
            listeners.put(entry.getKey(), method);
        }
    });
}
Also used : Transient(io.requery.Transient) Table(io.requery.Table) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Embedded(io.requery.Embedded) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) MirroredTypeException(javax.lang.model.type.MirroredTypeException) PropertyNameStyle(io.requery.PropertyNameStyle) NestingKind(javax.lang.model.element.NestingKind) ElementFilter(javax.lang.model.util.ElementFilter) LinkedHashSet(java.util.LinkedHashSet) Name(javax.lang.model.element.Name) View(io.requery.View) Entity(io.requery.Entity) ElementKind(javax.lang.model.element.ElementKind) ExecutableElement(javax.lang.model.element.ExecutableElement) Cacheable(javax.persistence.Cacheable) Set(java.util.Set) ReadOnly(io.requery.ReadOnly) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Stream(java.util.stream.Stream) Index(javax.persistence.Index) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Annotation(java.lang.annotation.Annotation) Factory(io.requery.Factory) Optional(java.util.Optional) Embeddable(javax.persistence.Embeddable) 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 49 with Element

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

the class EntityType method typeName.

@Override
public QualifiedName typeName() {
    String entityName = Stream.of(Mirrors.findAnnotationMirror(element(), Entity.class), Mirrors.findAnnotationMirror(element(), javax.persistence.Entity.class)).filter(Optional::isPresent).map(Optional::get).map(mirror -> Mirrors.findAnnotationValue(mirror, "name")).filter(Optional::isPresent).map(Optional::get).map(value -> value.getValue().toString()).filter(name -> !Names.isEmpty(name)).findAny().orElse("");
    Elements elements = processingEnvironment.getElementUtils();
    String packageName = elements.getPackageOf(element()).getQualifiedName().toString();
    // if set in the annotation just use that
    if (!Names.isEmpty(entityName)) {
        return new QualifiedName(packageName, entityName);
    }
    String typeName = element().getSimpleName().toString();
    if (element().getKind().isInterface()) {
        // maybe I<Something> style
        if (typeName.startsWith("I") && Character.isUpperCase(typeName.charAt(1))) {
            entityName = typeName.substring(1);
        } else {
            entityName = typeName + "Entity";
        }
    } else {
        entityName = Names.removeClassPrefixes(typeName);
        if (entityName.equals(typeName)) {
            entityName = typeName + (isImmutable() || isUnimplementable() ? "Type" : "Entity");
        }
    }
    return new QualifiedName(packageName, entityName);
}
Also used : Transient(io.requery.Transient) Table(io.requery.Table) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Embedded(io.requery.Embedded) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) MirroredTypeException(javax.lang.model.type.MirroredTypeException) PropertyNameStyle(io.requery.PropertyNameStyle) NestingKind(javax.lang.model.element.NestingKind) ElementFilter(javax.lang.model.util.ElementFilter) LinkedHashSet(java.util.LinkedHashSet) Name(javax.lang.model.element.Name) View(io.requery.View) Entity(io.requery.Entity) ElementKind(javax.lang.model.element.ElementKind) ExecutableElement(javax.lang.model.element.ExecutableElement) Cacheable(javax.persistence.Cacheable) Set(java.util.Set) ReadOnly(io.requery.ReadOnly) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Stream(java.util.stream.Stream) Index(javax.persistence.Index) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Annotation(java.lang.annotation.Annotation) Factory(io.requery.Factory) Optional(java.util.Optional) Embeddable(javax.persistence.Embeddable) Optional(java.util.Optional) Elements(javax.lang.model.util.Elements)

Example 50 with Element

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

the class RobolectricProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    for (TypeElement annotation : annotations) {
        Validator validator = elementValidators.get(annotation);
        if (validator != null) {
            for (Element elem : roundEnv.getElementsAnnotatedWith(annotation)) {
                validator.visit(elem, elem.getEnclosingElement());
            }
        }
    }
    if (!generated && shadowPackage != null) {
        model.prepare();
        for (Generator generator : generators) {
            generator.generate(shadowPackage);
        }
        generated = true;
    }
    return true;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ImplementationValidator(org.robolectric.annotation.processing.validator.ImplementationValidator) ImplementsValidator(org.robolectric.annotation.processing.validator.ImplementsValidator) ResetterValidator(org.robolectric.annotation.processing.validator.ResetterValidator) Validator(org.robolectric.annotation.processing.validator.Validator) RealObjectValidator(org.robolectric.annotation.processing.validator.RealObjectValidator) Generator(org.robolectric.annotation.processing.generator.Generator) ShadowProviderGenerator(org.robolectric.annotation.processing.generator.ShadowProviderGenerator) ServiceLoaderGenerator(org.robolectric.annotation.processing.generator.ServiceLoaderGenerator)

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