use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class ClazzAs method toBuild.
private static List<Statement> toBuild(final TypeDef clazz, final TypeDef instanceType) {
Method constructor = findBuildableConstructor(clazz);
List<Statement> statements = new ArrayList<Statement>();
statements.add(new StringStatement(new StringBuilder().append(instanceType.getName()).append(" buildable = new ").append(instanceType.getName()).append("(").append(Strings.join(constructor.getArguments(), new Function<Property, String>() {
public String apply(Property item) {
return "fluent." + Getter.name(item) + "()";
}
}, ",")).append(");").toString()));
TypeDef target = clazz;
List<TypeDef> parents = new ArrayList<TypeDef>();
Types.visitParents(target, parents);
for (TypeDef c : parents) {
if (!isBuildable(c)) {
continue;
}
for (Property property : c.getProperties()) {
Method setter;
try {
setter = Setter.find(clazz, property);
} catch (SundrException e) {
// no setter found nothing to set
continue;
}
if (!hasBuildableConstructorWithArgument(c, property)) {
String getterName = Getter.name(property);
statements.add(new StringStatement(new StringBuilder().append("buildable.").append(setter.getName()).append("(fluent.").append(getterName).append("());").toString()));
}
}
}
BuilderContext context = BuilderContextManager.getContext();
if (context.isExternalvalidatorSupported()) {
statements.add(new StringStatement("if (validationEnabled) {" + context.getBuilderPackage() + ".ValidationUtils.validate(buildable, validator);}"));
} else if (context.isValidationEnabled()) {
statements.add(new StringStatement("if (validationEnabled) {" + context.getBuilderPackage() + ".ValidationUtils.validate(buildable);}"));
}
statements.add(new StringStatement("return buildable;"));
return statements;
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class TypeDefUtilsTest method testCombineTwoWithTerminal.
@Test
public void testCombineTwoWithTerminal() throws Exception {
TypeElement typeElement = elements.getTypeElement(TwoWithTerminal.class.getCanonicalName());
List<ExecutableElement> methods = ElementFilter.methodsIn(typeElement.getEnclosedElements());
ExecutableElement simple = isVoid(methods.get(0)) ? methods.get(0) : methods.get(1);
ExecutableElement terminal = !isVoid(methods.get(0)) ? methods.get(0) : methods.get(1);
TypeDef simpleClazz = TypeDefUtils.executableToInterface(dslContext, simple);
TypeDef teminalClazz = TypeDefUtils.executableToInterface(dslContext, terminal);
TypeDef combined = Combine.TYPEDEFS.apply(Arrays.asList(simpleClazz, teminalClazz));
Assert.assertNotNull(combined);
String T = Generics.MAP.apply(TRANSPARENT_REF).getName();
Assert.assertThat(combined.getName(), CoreMatchers.equalTo("MethodABInterface"));
Assert.assertThat(combined.getPackageName(), CoreMatchers.equalTo(getClass().getPackage().getName()));
// Assert.assertThat(combined.getParameters().size(), CoreMatchers.is(1));
// Assert.assertThat(combined.getParameters().get(0).getName(), CoreMatchers.equalTo(T));
Assert.assertThat(combined.getExtendsList().size(), CoreMatchers.is(2));
Assert.assertTrue(combined.getExtendsList().stream().map(Object::toString).filter(s -> s.equals("utils.MethodAInterface<" + T + ">")).findAny().isPresent());
Assert.assertTrue(combined.getExtendsList().stream().map(Object::toString).filter(s -> s.equals("utils.MethodBInterface<" + T + ">")).findAny().isPresent());
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class VelocityRendererTest method testVelocityRendererFromResource.
@Test
public void testVelocityRendererFromResource() throws Exception {
VelocityRenderer.fromTemplateResource("/template.vm", TypeDef.class).ifPresent(r -> {
TypeDef string = TypeDef.forName(String.class.getName());
String result = r.render(string);
System.out.println(result);
assertEquals(String.class.getName(), result);
});
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class DefinitionRepository method getReferenceMapInternal.
private Map<String, String> getReferenceMapInternal() {
Map<String, String> mapping = new HashMap<String, String>();
List<ClassRef> refs = new ArrayList<ClassRef>();
for (TypeDef typeDef : getDefinitions()) {
refs.add(typeDef.toInternalReference());
}
// It's best to have predictable order, so that we can generate uniform code.
Collections.sort(refs, new Comparator<ClassRef>() {
@Override
public int compare(ClassRef o1, ClassRef o2) {
return o1.getFullyQualifiedName().compareTo(o2.getFullyQualifiedName());
}
});
for (ClassRef classRef : refs) {
String key = classRef.getName();
if (!mapping.containsKey(key)) {
mapping.put(key, classRef.getFullyQualifiedName());
}
}
return mapping;
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class BindDefinition method apply.
@Override
public TypeDef apply(ClassRef t) {
List<TypeRef> arguments = t.getArguments();
TypeDef definition = GetDefinition.of(t);
if (arguments.isEmpty()) {
return definition;
}
List<TypeParamDef> parameters = definition.getParameters();
List<TypeParamDef> newParameters = definition.getParameters();
Map<String, TypeRef> mappings = new HashMap<>();
for (int i = 0; i < arguments.size(); i++) {
String name = parameters.get(i).getName();
TypeRef typeRef = arguments.get(i);
mappings.put(name, typeRef);
newParameters.remove(i);
}
return new TypeDefBuilder(definition).withParameters(newParameters).accept(mapClassRefArguments(mappings), mapGenericProperties(mappings), mapGenericReturnTypes(mappings)).build();
}
Aggregations