Search in sources :

Example 16 with TypeDefBuilder

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

the class DslProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
    Elements elements = processingEnv.getElementUtils();
    Types types = processingEnv.getTypeUtils();
    DslContext context = DslContextManager.create(elements, types);
    for (TypeElement annotation : annotations) {
        for (Element element : env.getElementsAnnotatedWith(annotation)) {
            if (element instanceof TypeElement) {
                Generics.clear();
                TypeElement typeElement = (TypeElement) element;
                InterfaceName interfaceName = element.getAnnotation(InterfaceName.class);
                String targetInterface = interfaceName.value();
                Set<TypeDef> interfacesToGenerate = new LinkedHashSet<TypeDef>();
                Collection<ExecutableElement> sorted = ElementFilter.methodsIn(typeElement.getEnclosedElements());
                // 1st step generate generic interface for all types.
                Set<TypeDef> genericInterfaces = executablesToInterfaces(context, sorted);
                Set<TypeDef> genericAndScopeInterfaces = Nodes.TO_SCOPE.apply(genericInterfaces);
                for (TypeDef clazz : genericAndScopeInterfaces) {
                    if (!TypeDefUtils.isEntryPoint(clazz)) {
                        interfacesToGenerate.add(clazz);
                    }
                }
                // 2nd step create dependency graph.
                List<Method> methods = new ArrayList<Method>();
                Set<Node<TypeDef>> graph = Nodes.TO_GRAPH.apply(genericAndScopeInterfaces);
                for (Node<TypeDef> root : graph) {
                    Node<TypeDef> uncyclic = Nodes.TO_UNCYCLIC.apply(root);
                    Node<TypeDef> unwrapped = Nodes.TO_UNWRAPPED.apply(NodeContext.builder().withItem(uncyclic.getItem()).build());
                    TypeDef current = unwrapped.getItem();
                    // Just add the method with the direct return type.
                    if (unwrapped.getTransitions().isEmpty()) {
                        for (Method m : current.getMethods()) {
                            TypeRef returnType = m.getReturnType();
                            if (returnType instanceof ClassRef) {
                                TypeDef toUnwrap = GetDefinition.of((ClassRef) returnType);
                                methods.add(new MethodBuilder(m).withReturnType(Generics.UNWRAP.apply(toUnwrap).toInternalReference()).build());
                            } else if (returnType.getAttributes().containsKey(ORIGINAL_REF)) {
                                methods.add(new MethodBuilder(m).withReturnType((TypeRef) returnType.getAttributes().get(ORIGINAL_REF)).build());
                            } else {
                                methods.add(new MethodBuilder(m).withReturnType(returnType).build());
                            }
                        }
                    } else {
                        for (Method m : current.getMethods()) {
                            methods.add(new MethodBuilder(m).withReturnType(current.toUnboundedReference()).build());
                        }
                        interfacesToGenerate.add(Nodes.TO_ROOT.apply(unwrapped));
                    }
                }
                // Do generate the DSL interface
                interfacesToGenerate.add(new TypeDefBuilder().withComments("Generated").withPackageName(Apt.getPackageElement(element).toString()).withName(targetInterface).withKind(Kind.INTERFACE).withModifiers(modifiersToInt(Modifier.PUBLIC)).withMethods(methods).build());
                interfacesToGenerate.addAll(context.getDefinitionRepository().getDefinitions(IS_GENERATED));
                for (TypeDef clazz : interfacesToGenerate) {
                    generate(clazz);
                }
            }
        }
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Types(javax.lang.model.util.Types) ClassRef(io.sundr.model.ClassRef) TypeElement(javax.lang.model.element.TypeElement) TypeRef(io.sundr.model.TypeRef) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) Node(io.sundr.dsl.internal.graph.Node) ArrayList(java.util.ArrayList) Method(io.sundr.model.Method) Elements(javax.lang.model.util.Elements) TypeDefBuilder(io.sundr.model.TypeDefBuilder) MethodBuilder(io.sundr.model.MethodBuilder) TypeDef(io.sundr.model.TypeDef) InterfaceName(io.sundr.dsl.annotations.InterfaceName)

Example 17 with TypeDefBuilder

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

the class Combine method extractParameters.

private static final Set<TypeParamDef> extractParameters(TypeDef typeDef) {
    final Set<TypeParamDef> result = new LinkedHashSet<TypeParamDef>();
    final Set<TypeParamRef> refs = new LinkedHashSet<TypeParamRef>();
    TypeDef ignored = new TypeDefBuilder(typeDef).accept(new TypeParamDefColletor(result)).accept(new TypeParamRefColletor(refs)).build();
    for (TypeParamRef typeParamRef : refs) {
        result.add(new TypeParamDefBuilder().withName(typeParamRef.getName()).withAttributes(typeParamRef.getAttributes()).build());
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeParamRef(io.sundr.model.TypeParamRef) TypeParamDef(io.sundr.model.TypeParamDef) TypeParamRefColletor(io.sundr.dsl.internal.visitors.TypeParamRefColletor) TypeDef(io.sundr.model.TypeDef) TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) TypeParamDefColletor(io.sundr.dsl.internal.visitors.TypeParamDefColletor) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Example 18 with TypeDefBuilder

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

the class TypeDefElementVisitor method visit.

public TypeDefBuilder visit(Element e) {
    if (e instanceof TypeElement) {
        return new TypeDefBuilder(context.getDefinitionRepository().register(new TypeDefBuilder(typeAdapterFunction.apply((TypeElement) e)).build()));
    }
    String name = e.getSimpleName().toString();
    builder.withName(name);
    if (e.getKind() == ElementKind.INTERFACE) {
        builder.withKind(Kind.INTERFACE);
    } else if (e.getKind() == ElementKind.ENUM) {
        builder.withKind(Kind.ENUM);
    } else if (e.getKind() == ElementKind.ANNOTATION_TYPE) {
        builder.withKind(Kind.ANNOTATION);
    } else {
        builder.withKind(Kind.CLASS);
    }
    if (e.getEnclosingElement() instanceof PackageElement) {
        String packageName = e.getEnclosingElement().toString();
        builder.withPackageName(packageName);
    }
    return builder;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Example 19 with TypeDefBuilder

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

the class TemplateTransformationProcessor method selectAnnotated.

public void selectAnnotated(RoundEnvironment env, Types types, AnnotationSelector selector, Map<String, TypeDef> definitions) {
    for (Object o : env.getElementsAnnotatedWith((TypeElement) types.asElement(annotationMirror(selector)))) {
        if (o instanceof TypeElement) {
            TypeDef typeDef = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement((Element) o), AptContext.getContext())).build();
            definitions.put(typeDef.getFullyQualifiedName(), typeDef);
        }
    }
}
Also used : TypeDef(io.sundr.model.TypeDef) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) FileObject(javax.tools.FileObject) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Example 20 with TypeDefBuilder

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

the class TemplateTransformationProcessor method selectPackages.

public void selectPackages(Elements elements, PackageSelector selector, Map<String, TypeDef> definitions) {
    Pattern pattern = Pattern.compile(selector.pattern());
    PackageElement packageElement = elements.getPackageElement(selector.value());
    List<TypeElement> typeElements = new ArrayList<>();
    if (packageElement != null) {
        for (Element e : packageElement.getEnclosedElements()) {
            if (e instanceof TypeElement) {
                typeElements.add((TypeElement) e);
            }
        }
    } else {
        TypeElement e = elements.getTypeElement(selector.value());
        if (e != null) {
            typeElements.add(e);
        }
    }
    for (TypeElement typeElement : typeElements) {
        TypeDef typeDef = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement(typeElement), AptContext.getContext())).build();
        Matcher m = pattern.matcher(typeDef.getName());
        if (m.matches()) {
            definitions.put(typeDef.getFullyQualifiedName(), typeDef);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) TypeDef(io.sundr.model.TypeDef) Matcher(java.util.regex.Matcher) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) PackageElement(javax.lang.model.element.PackageElement) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Aggregations

TypeDefBuilder (io.sundr.model.TypeDefBuilder)30 TypeDef (io.sundr.model.TypeDef)25 ClassRef (io.sundr.model.ClassRef)14 ArrayList (java.util.ArrayList)14 TypeRef (io.sundr.model.TypeRef)11 Property (io.sundr.model.Property)10 Method (io.sundr.model.Method)9 HashMap (java.util.HashMap)8 Collectors (java.util.stream.Collectors)8 Element (javax.lang.model.element.Element)8 TypeElement (javax.lang.model.element.TypeElement)8 MethodBuilder (io.sundr.model.MethodBuilder)7 PropertyBuilder (io.sundr.model.PropertyBuilder)7 TypeParamDef (io.sundr.model.TypeParamDef)7 List (java.util.List)7 AnnotationRef (io.sundr.model.AnnotationRef)6 HashSet (java.util.HashSet)6 TypedVisitor (io.sundr.builder.TypedVisitor)5 Map (java.util.Map)5 Set (java.util.Set)5