Search in sources :

Example 1 with GENERATE_MODEL

use of io.requery.processor.EntityProcessor.GENERATE_MODEL in project requery by requery.

the class EntityProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    // types to generate in this round
    Map<TypeElement, EntityElement> entities = new HashMap<>();
    SourceLanguage.map(processingEnv);
    Types types = processingEnv.getTypeUtils();
    Set<TypeElement> annotationElements = new LinkedHashSet<>();
    if (isEmptyKotlinAnnotationSet(annotations)) {
        annotationElements.addAll(SourceLanguage.getAnnotations());
    } else {
        annotationElements.addAll(annotations);
    }
    for (TypeElement annotation : annotationElements) {
        for (Element element : roundEnv.getElementsAnnotatedWith(annotation)) {
            typeElementOf(element).ifPresent(typeElement -> {
                EntityElement entity = null;
                if (isEntity(typeElement)) {
                    entity = computeType(entities, typeElement);
                    String model = entity.modelName();
                    graphs.computeIfAbsent(model, key -> new EntityGraph(types, embeddedTypes)).add(entity);
                } else if (isSuperclass(typeElement)) {
                    entity = computeType(superTypes, typeElement);
                } else if (isEmbeddable(typeElement)) {
                    entity = computeType(embeddedTypes, typeElement);
                }
                if (entity != null) {
                    entity.addAnnotationElement(annotation, element);
                }
            });
        }
    }
    // process
    boolean hasErrors = false;
    Set<ElementValidator> validators = new LinkedHashSet<>();
    Elements elements = processingEnv.getElementUtils();
    for (EntityElement entity : entities.values()) {
        // add the annotated elements from the super type (if any)
        if (entity.element().getKind() == ElementKind.INTERFACE) {
            List<? extends TypeMirror> interfaces = entity.element().getInterfaces();
            for (TypeMirror mirror : interfaces) {
                TypeElement superElement = elements.getTypeElement(mirror.toString());
                if (superElement != null) {
                    mergeSuperType(entity, superElement);
                }
            }
        }
        TypeMirror typeMirror = entity.element().getSuperclass();
        while (typeMirror.getKind() != TypeKind.NONE) {
            TypeElement superElement = elements.getTypeElement(typeMirror.toString());
            if (superElement != null) {
                mergeSuperType(entity, superElement);
                typeMirror = superElement.getSuperclass();
            } else {
                break;
            }
        }
        // process the entity
        Set<ElementValidator> results = entity.process(processingEnv);
        validators.addAll(results);
    }
    for (EntityElement entity : embeddedTypes.values()) {
        Set<ElementValidator> results = entity.process(processingEnv);
        validators.addAll(results);
    }
    for (EntityGraph graph : graphs.values()) {
        EntityGraphValidator validator = new EntityGraphValidator(processingEnv, graph);
        Set<ElementValidator> results = validator.validate();
        validators.addAll(results);
    }
    if (ElementValidator.hasErrors(validators)) {
        hasErrors = true;
        StringBuilder sb = new StringBuilder("Model has error(s) code generation may fail: ");
        validators.forEach(validator -> sb.append(validator.toString()));
        processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, sb);
    }
    // generate
    Set<SourceGenerator> generators = new LinkedHashSet<>();
    if (!hasErrors || getOption(GENERATE_ALWAYS, true)) {
        for (EntityDescriptor entity : entities.values()) {
            EntityGraph graph = graphs.get(entity.modelName());
            if (graph != null) {
                generators.add(new EntityGenerator(processingEnv, graph, entity, null));
            }
        }
    }
    if (getOption(GENERATE_MODEL, true)) {
        Map<String, Collection<EntityDescriptor>> packagesMap = new LinkedHashMap<>();
        Set<EntityDescriptor> allEntities = graphs.values().stream().flatMap(graph -> graph.entities().stream()).collect(Collectors.toSet());
        for (EntityDescriptor entity : allEntities) {
            EntityGraph graph = graphs.get(entity.modelName());
            String packageName = findModelPackageName(graph);
            packagesMap.computeIfAbsent(packageName, key -> new LinkedHashSet<>());
            packagesMap.get(packageName).addAll(graph.entities());
        }
        for (EntityDescriptor entity : entities.values()) {
            EntityGraph graph = graphs.get(entity.modelName());
            String packageName = findModelPackageName(graph);
            if (entity.generatesAdditionalTypes()) {
                packagesMap.remove(packageName);
            }
        }
        generators.addAll(packagesMap.entrySet().stream().filter(entry -> !entry.getValue().isEmpty()).filter(entry -> !generatedModelPackages.contains(entry.getKey())).map(entry -> {
            generatedModelPackages.add(entry.getKey());
            return entry;
        }).map(entry -> new ModelGenerator(processingEnv, entry.getKey(), entry.getValue())).collect(Collectors.toList()));
    }
    for (SourceGenerator generator : generators) {
        try {
            generator.generate();
        } catch (IOException e) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage());
        }
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AbstractProcessor(javax.annotation.processing.AbstractProcessor) SupportedOptions(javax.annotation.processing.SupportedOptions) TypeElement(javax.lang.model.element.TypeElement) HashMap(java.util.HashMap) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Elements(javax.lang.model.util.Elements) GENERATE_MODEL(io.requery.processor.EntityProcessor.GENERATE_MODEL) LinkedHashMap(java.util.LinkedHashMap) Embedded(io.requery.Embedded) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) GENERATE_JPA(io.requery.processor.EntityProcessor.GENERATE_JPA) LinkedHashSet(java.util.LinkedHashSet) Entity(io.requery.Entity) ElementKind(javax.lang.model.element.ElementKind) Collection(java.util.Collection) Set(java.util.Set) Element(javax.lang.model.element.Element) IOException(java.io.IOException) GENERATE_ALWAYS(io.requery.processor.EntityProcessor.GENERATE_ALWAYS) Superclass(io.requery.Superclass) Types(javax.lang.model.util.Types) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) RoundEnvironment(javax.annotation.processing.RoundEnvironment) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Optional(java.util.Optional) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) Elements(javax.lang.model.util.Elements) LinkedHashMap(java.util.LinkedHashMap) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) IOException(java.io.IOException) Collection(java.util.Collection)

Aggregations

Embedded (io.requery.Embedded)1 Entity (io.requery.Entity)1 Superclass (io.requery.Superclass)1 GENERATE_ALWAYS (io.requery.processor.EntityProcessor.GENERATE_ALWAYS)1 GENERATE_JPA (io.requery.processor.EntityProcessor.GENERATE_JPA)1 GENERATE_MODEL (io.requery.processor.EntityProcessor.GENERATE_MODEL)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 AbstractProcessor (javax.annotation.processing.AbstractProcessor)1 ProcessingEnvironment (javax.annotation.processing.ProcessingEnvironment)1 RoundEnvironment (javax.annotation.processing.RoundEnvironment)1 SupportedAnnotationTypes (javax.annotation.processing.SupportedAnnotationTypes)1