Search in sources :

Example 1 with ResourceSelector

use of io.sundr.transform.annotations.ResourceSelector in project sundrio by sundrio.

the class TemplateTransformationProcessor method selectFromResource.

public void selectFromResource(Elements elements, Filer filer, ResourceSelector selector, Map<String, TypeDef> definitions) {
    try {
        FileObject fileObject = filer.getResource(StandardLocation.CLASS_PATH, "", selector.value());
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(fileObject.openInputStream()))) {
            List<String> lines = reader.lines().map(String::trim).filter(l -> !Strings.isNullOrEmpty(l)).collect(Collectors.toList());
            AptContext aptContext = AptContext.getContext();
            Map<String, TypeDef> map = lines.stream().map(l -> elements.getTypeElement(l)).filter(e -> e instanceof TypeElement).map(e -> Adapters.adaptType(e, aptContext)).collect(Collectors.toMap(e -> e.getFullyQualifiedName(), e -> e));
            definitions.putAll(map);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
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) InputStreamReader(java.io.InputStreamReader) AptContext(io.sundr.adapter.apt.AptContext) TypeDef(io.sundr.model.TypeDef) TypeElement(javax.lang.model.element.TypeElement) BufferedReader(java.io.BufferedReader) FileObject(javax.tools.FileObject) IOException(java.io.IOException)

Example 2 with ResourceSelector

use of io.sundr.transform.annotations.ResourceSelector 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)2 TypeLookup (io.sundr.adapter.api.TypeLookup)2 AptContext (io.sundr.adapter.apt.AptContext)2 Apt (io.sundr.adapter.apt.utils.Apt)2 CodeGenerator (io.sundr.codegen.api.CodeGenerator)2 GenericAptOutput (io.sundr.codegen.apt.GenericAptOutput)2 TypeDefAptOutput (io.sundr.codegen.apt.TypeDefAptOutput)2 AbstractCodeGeneratingProcessor (io.sundr.codegen.apt.processor.AbstractCodeGeneratingProcessor)2 TemplateRenderer (io.sundr.codegen.template.TemplateRenderer)2 TemplateRenderers (io.sundr.codegen.template.TemplateRenderers)2 TypeDef (io.sundr.model.TypeDef)2 TypeDefBuilder (io.sundr.model.TypeDefBuilder)2 DefinitionRepository (io.sundr.model.repo.DefinitionRepository)2 AnnotationSelector (io.sundr.transform.annotations.AnnotationSelector)2 PackageSelector (io.sundr.transform.annotations.PackageSelector)2 ResourceSelector (io.sundr.transform.annotations.ResourceSelector)2 TemplateTransformation (io.sundr.transform.annotations.TemplateTransformation)2 TemplateTransformations (io.sundr.transform.annotations.TemplateTransformations)2 Strings (io.sundr.utils.Strings)2 BufferedReader (java.io.BufferedReader)2