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;
}
Aggregations