Search in sources :

Example 1 with GenericAptOutput

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;
}
Also used : Apt(io.sundr.adapter.apt.utils.Apt) PackageElement(javax.lang.model.element.PackageElement) TypeLookup(io.sundr.adapter.api.TypeLookup) PackageSelector(io.sundr.transform.annotations.PackageSelector) URL(java.net.URL) TemplateRenderers(io.sundr.codegen.template.TemplateRenderers) HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Function(java.util.function.Function) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Elements(javax.lang.model.util.Elements) AbstractCodeGeneratingProcessor(io.sundr.codegen.apt.processor.AbstractCodeGeneratingProcessor) ArrayList(java.util.ArrayList) TemplateRenderer(io.sundr.codegen.template.TemplateRenderer) ResourceSelector(io.sundr.transform.annotations.ResourceSelector) FileObject(javax.tools.FileObject) SupportedSourceVersion(javax.annotation.processing.SupportedSourceVersion) Matcher(java.util.regex.Matcher) GenericAptOutput(io.sundr.codegen.apt.GenericAptOutput) Map(java.util.Map) MirroredTypeException(javax.lang.model.type.MirroredTypeException) StandardLocation(javax.tools.StandardLocation) AptContext(io.sundr.adapter.apt.AptContext) Strings(io.sundr.utils.Strings) TypeDefAptOutput(io.sundr.codegen.apt.TypeDefAptOutput) DefinitionRepository(io.sundr.model.repo.DefinitionRepository) Adapters(io.sundr.adapter.api.Adapters) Set(java.util.Set) IOException(java.io.IOException) Element(javax.lang.model.element.Element) Types(javax.lang.model.util.Types) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) CodeGenerator(io.sundr.codegen.api.CodeGenerator) RoundEnvironment(javax.annotation.processing.RoundEnvironment) Filer(javax.annotation.processing.Filer) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) TypeDef(io.sundr.model.TypeDef) TemplateTransformation(io.sundr.transform.annotations.TemplateTransformation) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) TemplateTransformations(io.sundr.transform.annotations.TemplateTransformations) AnnotationSelector(io.sundr.transform.annotations.AnnotationSelector) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) HashMap(java.util.HashMap) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) Elements(javax.lang.model.util.Elements) TypeDefBuilder(io.sundr.model.TypeDefBuilder) URL(java.net.URL) TypeDef(io.sundr.model.TypeDef) Filer(javax.annotation.processing.Filer) TypeDefAptOutput(io.sundr.codegen.apt.TypeDefAptOutput) AptContext(io.sundr.adapter.apt.AptContext) TemplateTransformations(io.sundr.transform.annotations.TemplateTransformations) TypeElement(javax.lang.model.element.TypeElement) AnnotationSelector(io.sundr.transform.annotations.AnnotationSelector) ResourceSelector(io.sundr.transform.annotations.ResourceSelector) TemplateTransformation(io.sundr.transform.annotations.TemplateTransformation) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) PackageSelector(io.sundr.transform.annotations.PackageSelector)

Aggregations

Adapters (io.sundr.adapter.api.Adapters)1 TypeLookup (io.sundr.adapter.api.TypeLookup)1 AptContext (io.sundr.adapter.apt.AptContext)1 Apt (io.sundr.adapter.apt.utils.Apt)1 CodeGenerator (io.sundr.codegen.api.CodeGenerator)1 GenericAptOutput (io.sundr.codegen.apt.GenericAptOutput)1 TypeDefAptOutput (io.sundr.codegen.apt.TypeDefAptOutput)1 AbstractCodeGeneratingProcessor (io.sundr.codegen.apt.processor.AbstractCodeGeneratingProcessor)1 TemplateRenderer (io.sundr.codegen.template.TemplateRenderer)1 TemplateRenderers (io.sundr.codegen.template.TemplateRenderers)1 TypeDef (io.sundr.model.TypeDef)1 TypeDefBuilder (io.sundr.model.TypeDefBuilder)1 DefinitionRepository (io.sundr.model.repo.DefinitionRepository)1 AnnotationSelector (io.sundr.transform.annotations.AnnotationSelector)1 PackageSelector (io.sundr.transform.annotations.PackageSelector)1 ResourceSelector (io.sundr.transform.annotations.ResourceSelector)1 TemplateTransformation (io.sundr.transform.annotations.TemplateTransformation)1 TemplateTransformations (io.sundr.transform.annotations.TemplateTransformations)1 Strings (io.sundr.utils.Strings)1 BufferedReader (java.io.BufferedReader)1