Search in sources :

Example 36 with TypeDef

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

the class ClazzAs method toInstanceConstructorBody.

private static List<Statement> toInstanceConstructorBody(TypeDef clazz, TypeDef instance, String fluent) {
    Method constructor = findBuildableConstructor(clazz);
    List<Statement> statements = new ArrayList<Statement>();
    final String ref = fluent != null && !fluent.isEmpty() ? fluent : "this";
    // We may use a reference to fluent or we may use directly "this". So we need to check.
    if (fluent != null && !fluent.isEmpty()) {
        statements.add(new StringStatement("this.fluent = " + fluent + "; "));
    }
    for (Property property : constructor.getArguments()) {
        Optional<Method> getter = Getter.findOptional(instance, property);
        getter.ifPresent(g -> {
            String cast = property.getTypeRef() instanceof TypeParamRef ? "(" + property.getTypeRef().toString() + ")" : "";
            statements.add(new StringStatement(new StringBuilder().append(ref).append(".with").append(property.getNameCapitalized()).append("(").append(cast).append("instance.").append(g.getName()).append("()); ").toString()));
        });
    // } else {
    // throw new IllegalStateException("Could not find getter for property:" + property + " in class:" + clazz);
    // }
    }
    TypeDef target = clazz;
    // Iterate parent objects and check for properties with setters but not ctor arguments.
    while (target != null && !Types.OBJECT.equals(target) && BuilderUtils.isBuildable(target)) {
        for (Property property : target.getProperties()) {
            if (!hasBuildableConstructorWithArgument(target, property) && Setter.has(target, property)) {
                Getter.findOptional(instance, property).map(Method::getName).ifPresent(getterName -> {
                    String withName = "with" + property.getNameCapitalized();
                    statements.add(new StringStatement(new StringBuilder().append(ref).append(".").append(withName).append("(instance.").append(getterName).append("());\n").toString()));
                });
            }
        }
        if (!target.getExtendsList().isEmpty()) {
            target = BuilderContextManager.getContext().getBuildableRepository().getBuildable(target.getExtendsList().iterator().next());
        } else {
            return statements;
        }
    }
    return statements;
}
Also used : TypeParamRef(io.sundr.model.TypeParamRef) RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) StringStatement(io.sundr.model.StringStatement) Statement(io.sundr.model.Statement) ArrayList(java.util.ArrayList) Method(io.sundr.model.Method) StringStatement(io.sundr.model.StringStatement) Property(io.sundr.model.Property)

Example 37 with TypeDef

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

the class SourceAdapterFactoryTest method shouldAdaptList.

@Test
public void shouldAdaptList() throws Exception {
    Optional<Adapter<TypeDeclaration, ClassOrInterfaceType, FieldDeclaration, MethodDeclaration>> adapter = createAdapter();
    assertTrue(adapter.isPresent());
    adapter.ifPresent(a -> {
        TypeDeclaration typeDeclaration = Sources.readTypeFromResource("java/util/List.java");
        assertNotNull(typeDeclaration);
        TypeDef typeDef = a.adaptType(typeDeclaration);
        assertNotNull(typeDef);
    });
}
Also used : TypeDef(io.sundr.model.TypeDef) Adapter(io.sundr.adapter.api.Adapter) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) Test(org.junit.Test)

Example 38 with TypeDef

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

the class SimpleClassTest method testInline.

@Test
public void testInline() {
    TypeDef inlineable = BuildableProcessor.inlineableOf(builderContext, simpleClassDef, inline);
    System.out.println(inlineable);
    assertEquals(Kind.CLASS, inlineable.getKind());
    assertEquals("CallableSimpleClass", inlineable.getName());
}
Also used : TypeDef(io.sundr.model.TypeDef) RichTypeDef(io.sundr.model.RichTypeDef) Test(org.junit.Test)

Example 39 with TypeDef

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

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

the class SimpleClassTest method testFluent.

@Test
public void testFluent() {
    TypeDef fluent = ClazzAs.FLUENT_INTERFACE.apply(simpleClassDef);
    System.out.println(fluent);
    assertEquals(Kind.INTERFACE, fluent.getKind());
    assertEquals("SimpleClassFluent", fluent.getName());
    assertEquals(1, fluent.getExtendsList().size());
    ClassRef superClass = fluent.getExtendsList().iterator().next();
    assertEquals("Fluent", superClass.getName());
    assertEquals(1, superClass.getArguments().size());
    assertEquals("A", superClass.getArguments().iterator().next().toString());
}
Also used : TypeDef(io.sundr.model.TypeDef) RichTypeDef(io.sundr.model.RichTypeDef) ClassRef(io.sundr.model.ClassRef) 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