Search in sources :

Example 21 with TypeRef

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

the class SimpleClassTest method testBuilder.

@Test
public void testBuilder() {
    TypeDef builder = ClazzAs.BUILDER.apply(simpleClassDef);
    System.out.println(builder);
    assertEquals(Kind.CLASS, builder.getKind());
    assertEquals("SimpleClassBuilder", builder.getName());
    assertEquals(1, builder.getExtendsList().size());
    ClassRef superClass = builder.getImplementsList().iterator().next();
    assertEquals("VisitableBuilder", superClass.getName());
    assertEquals(2, superClass.getArguments().size());
    Iterator<TypeRef> argIterator = superClass.getArguments().iterator();
    TypeRef ref = argIterator.next();
    assertEquals("testpackage.SimpleClass", ref.toString());
    assertEquals("testpackage.SimpleClass", ref.render());
    ref = argIterator.next();
    assertEquals("testpackage.SimpleClassBuilder", ref.toString());
    assertEquals("testpackage.SimpleClassBuilder", ref.render());
}
Also used : TypeDef(io.sundr.model.TypeDef) RichTypeDef(io.sundr.model.RichTypeDef) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) Test(org.junit.Test)

Example 22 with TypeRef

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

the class ApplyTypeParamMappingToMethod method visit.

@Override
public void visit(MethodFluent<?> method) {
    TypeRef typeRef = method.buildReturnType();
    if (typeRef instanceof TypeParamRef) {
        TypeParamRef typeParamRef = (TypeParamRef) typeRef;
        String key = typeParamRef.getName();
        TypeRef paramRef = mappings.get(key);
        if (paramRef != null) {
            method.withReturnType(paramRef);
            attributeKey.ifPresent(k -> method.addToAttributes(k, typeParamRef));
        }
    } else if (typeRef instanceof ClassRef) {
        ClassRef classRef = (ClassRef) typeRef;
        if (classRef.getArguments().stream().anyMatch(a -> a instanceof TypeParamRef)) {
            List<TypeRef> mappedArguments = classRef.getArguments().stream().map(a -> a instanceof TypeParamRef ? mappings.getOrDefault(((TypeParamRef) a).getName(), a) : a).collect(Collectors.toList());
            method.withReturnType(new ClassRefBuilder(classRef).withArguments(mappedArguments).build());
            attributeKey.ifPresent(k -> method.addToAttributes(k, classRef));
        }
    }
}
Also used : TypeParamRef(io.sundr.model.TypeParamRef) List(java.util.List) ClassRefBuilder(io.sundr.model.ClassRefBuilder) TypedVisitor(io.sundr.builder.TypedVisitor) Map(java.util.Map) TypeParamRef(io.sundr.model.TypeParamRef) Optional(java.util.Optional) TypeRef(io.sundr.model.TypeRef) Collectors(java.util.stream.Collectors) MethodFluent(io.sundr.model.MethodFluent) AttributeKey(io.sundr.model.AttributeKey) ClassRef(io.sundr.model.ClassRef) Maps(io.sundr.utils.Maps) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) List(java.util.List)

Example 23 with TypeRef

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

the class ApplyTypeParamMappingToTypeArguments method visit.

@Override
public void visit(ClassRefFluent<?> classRef) {
    List<TypeRef> arguments = new ArrayList<>();
    for (TypeRef arg : classRef.buildArguments()) {
        TypeRef mappedRef = arg;
        if (arg instanceof TypeParamRef) {
            TypeParamRef typeParamRef = (TypeParamRef) arg;
            TypeRef mapping = mappings.get(typeParamRef.getName());
            if (mapping != null) {
                mappedRef = mapping;
            }
        }
        arguments.add(mappedRef);
    }
    classRef.withArguments(arguments);
}
Also used : TypeParamRef(io.sundr.model.TypeParamRef) TypeRef(io.sundr.model.TypeRef) ArrayList(java.util.ArrayList)

Example 24 with TypeRef

use of io.sundr.model.TypeRef 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 25 with TypeRef

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

the class SimpleClassWithInheritanceTest method testBuilder.

@Test
public void testBuilder() throws FileNotFoundException {
    TypeDef builder = ClazzAs.BUILDER.apply(simpleClassWithDateDef);
    System.out.println(builder);
    assertEquals(Kind.CLASS, builder.getKind());
    assertEquals("SimpleClassWithDateBuilder", builder.getName());
    assertEquals(1, builder.getExtendsList().size());
    ClassRef superClass = builder.getImplementsList().iterator().next();
    assertEquals(builderContext.getVisitableBuilderInterface().getName(), superClass.getName());
    assertEquals(2, superClass.getArguments().size());
    Iterator<TypeRef> argIterator = superClass.getArguments().iterator();
    TypeRef ref = argIterator.next();
    assertEquals("testpackage.SimpleClassWithDate", ref.render());
    assertEquals("testpackage.SimpleClassWithDate", ref.toString());
    ref = argIterator.next();
    assertEquals("testpackage.SimpleClassWithDateBuilder", ref.render());
    assertEquals("testpackage.SimpleClassWithDateBuilder", ref.toString());
}
Also used : TypeDef(io.sundr.model.TypeDef) RichTypeDef(io.sundr.model.RichTypeDef) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) Test(org.junit.Test)

Aggregations

TypeRef (io.sundr.model.TypeRef)45 ClassRef (io.sundr.model.ClassRef)33 TypeDef (io.sundr.model.TypeDef)23 ArrayList (java.util.ArrayList)16 ClassRefBuilder (io.sundr.model.ClassRefBuilder)14 Method (io.sundr.model.Method)13 RichTypeDef (io.sundr.model.RichTypeDef)13 TypeDefBuilder (io.sundr.model.TypeDefBuilder)12 Property (io.sundr.model.Property)10 MethodBuilder (io.sundr.model.MethodBuilder)9 PropertyBuilder (io.sundr.model.PropertyBuilder)9 TypeParamRef (io.sundr.model.TypeParamRef)9 TypedVisitor (io.sundr.builder.TypedVisitor)8 AnnotationRef (io.sundr.model.AnnotationRef)8 List (java.util.List)8 TypeParamDef (io.sundr.model.TypeParamDef)7 HashMap (java.util.HashMap)7 AttributeKey (io.sundr.model.AttributeKey)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6