use of io.sundr.codegen.apt.GenericAptOutput in project sundrio by sundrio.
the class TemplateTransformationProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
Elements elements = processingEnv.getElementUtils();
Types types = processingEnv.getTypeUtils();
Filer filer = processingEnv.getFiler();
AptContext aptContext = AptContext.create(elements, types, DefinitionRepository.getRepository());
Map<TemplateTransformation, Map<String, TypeDef>> annotatedTypes = new HashMap<>();
for (TypeElement typeElement : annotations) {
for (Element element : env.getElementsAnnotatedWith(typeElement)) {
TemplateTransformations transformations = element.getAnnotation(TemplateTransformations.class);
TemplateTransformation transformation = element.getAnnotation(TemplateTransformation.class);
List<TemplateTransformation> all = new ArrayList<>();
if (transformation != null) {
all.add(transformation);
}
if (transformations != null) {
for (TemplateTransformation t : transformations.value()) {
all.add(t);
}
}
TypeDef def = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement(element), aptContext)).build();
for (TemplateTransformation t : all) {
if (!annotatedTypes.containsKey(t)) {
annotatedTypes.put(t, new HashMap<>());
}
// If there are no selectors processes the annotated class
if (transformations == null) {
annotatedTypes.get(t).put(def.getFullyQualifiedName(), def);
} else if (transformations.annotations().length > 0) {
for (AnnotationSelector selector : transformations.annotations()) {
selectAnnotated(env, types, selector, annotatedTypes.get(t));
}
} else if (transformations.packages().length > 0) {
for (PackageSelector selector : transformations.packages()) {
selectPackages(elements, selector, annotatedTypes.get(t));
}
} else if (transformations.resources().length > 0) {
for (ResourceSelector selector : transformations.resources()) {
selectFromResource(elements, filer, selector, annotatedTypes.get(t));
}
} else {
annotatedTypes.get(t).put(def.getFullyQualifiedName(), def);
}
}
}
for (Map.Entry<TemplateTransformation, Map<String, TypeDef>> entry : annotatedTypes.entrySet()) {
TemplateTransformation transformation = entry.getKey();
Map<String, TypeDef> annotated = entry.getValue();
try {
if (transformation.gather()) {
URL templateUrl = readTemplateURL(filer, null, transformation.value());
TemplateRenderer<Map> renderer = TemplateRenderers.getTemplateRenderer(Map.class, templateUrl).orElseThrow(() -> new IllegalStateException("No template renderer found for:" + templateUrl));
CodeGenerator.newGenerator(Map.class).withRenderer(renderer).withOutput(new GenericAptOutput<Map>(filer, renderer, transformation.outputPath())).skipping(i -> false).generate(annotated);
} else {
for (TypeDef typeDef : annotated.values()) {
URL templateUrl = readTemplateURL(filer, typeDef.getPackageName(), transformation.value());
TemplateRenderer<TypeDef> renderer = TemplateRenderers.getTemplateRenderer(TypeDef.class, templateUrl).orElseThrow(() -> new IllegalStateException("No template renderer found for:" + templateUrl));
Function<TypeDef, String> identifier = t -> io.sundr.model.utils.Types.parseFullyQualifiedName(renderer.render(t));
CodeGenerator.newGenerator(TypeDef.class).withRenderer(renderer).withIdentifier(identifier).withOutput(new TypeDefAptOutput(filer, renderer)).skipping(t -> TypeLookup.lookup(identifier.apply(typeDef), AptContext.getContext()).isPresent()).generate(typeDef);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return false;
}
Aggregations