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;
}
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);
});
}
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());
}
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());
}
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());
}
Aggregations