Search in sources :

Example 16 with TypeDef

use of io.sundr.model.TypeDef 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 17 with TypeDef

use of io.sundr.model.TypeDef 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)

Example 18 with TypeDef

use of io.sundr.model.TypeDef in project sundrio by sundrio.

the class ClassDirective method render.

@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException {
    String block = "";
    TypeDef clazz = null;
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        if (node.jjtGetChild(i) != null) {
            if (!(node.jjtGetChild(i) instanceof ASTBlock)) {
                // reading and casting inline parameters
                if (i == 0) {
                    clazz = (TypeDef) node.jjtGetChild(i).value(context);
                } else {
                    break;
                }
            } else {
                // reading block content and rendering it
                try {
                    StringWriter blockContent = new StringWriter();
                    node.jjtGetChild(i).render(context, blockContent);
                    block = blockContent.toString();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
    writeClazz(writer, clazz, block);
    return true;
}
Also used : TypeDef(io.sundr.model.TypeDef) StringWriter(java.io.StringWriter) ASTBlock(org.apache.velocity.runtime.parser.node.ASTBlock) IOException(java.io.IOException)

Example 19 with TypeDef

use of io.sundr.model.TypeDef in project sundrio by sundrio.

the class StringTemplateRendererTest method testStringTemplateRenderer.

@Test
public void testStringTemplateRenderer() throws Exception {
    StringTemplateRenderer<TypeDef> renderer = new StringTemplateRenderer<>("$model.fullyQualifiedName$");
    TypeDef string = TypeDef.forName(String.class.getName());
    String result = renderer.render(string);
    assertEquals(String.class.getName(), result);
}
Also used : TypeDef(io.sundr.model.TypeDef) Test(org.junit.Test)

Example 20 with TypeDef

use of io.sundr.model.TypeDef in project sundrio by sundrio.

the class VelocityRendererTest method testVelocityRendererInline.

@Test
public void testVelocityRendererInline() throws Exception {
    VelocityRenderer<TypeDef> renderer = new VelocityRenderer<>("${model.getFullyQualifiedName()}");
    TypeDef string = TypeDef.forName(String.class.getName());
    String result = renderer.render(string);
    assertEquals(String.class.getName(), result);
}
Also used : TypeDef(io.sundr.model.TypeDef) Test(org.junit.Test)

Aggregations

TypeDef (io.sundr.model.TypeDef)99 ClassRef (io.sundr.model.ClassRef)43 Test (org.junit.Test)40 RichTypeDef (io.sundr.model.RichTypeDef)38 TypeDefBuilder (io.sundr.model.TypeDefBuilder)35 TypeRef (io.sundr.model.TypeRef)26 Method (io.sundr.model.Method)22 Property (io.sundr.model.Property)22 ArrayList (java.util.ArrayList)20 List (java.util.List)15 TypeElement (javax.lang.model.element.TypeElement)15 Collectors (java.util.stream.Collectors)14 Element (javax.lang.model.element.Element)14 Set (java.util.Set)12 Test (org.junit.jupiter.api.Test)12 AnnotationRef (io.sundr.model.AnnotationRef)11 DefinitionRepository (io.sundr.model.repo.DefinitionRepository)11 HashMap (java.util.HashMap)11 AptContext (io.sundr.adapter.apt.AptContext)10 ClassRefBuilder (io.sundr.model.ClassRefBuilder)10