use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class TypeDefUtilsTest method testCombineTwoNonTerminal.
@Test
public void testCombineTwoNonTerminal() throws Exception {
TypeElement typeElement = elements.getTypeElement(TwoNonTerminal.class.getCanonicalName());
List<ExecutableElement> methods = ElementFilter.methodsIn(typeElement.getEnclosedElements());
ExecutableElement left = isVoid(methods.get(0)) ? methods.get(0) : methods.get(1);
ExecutableElement right = !isVoid(methods.get(0)) ? methods.get(0) : methods.get(1);
TypeDef leftClazz = executableToInterface(dslContext, left);
TypeDef rightClazz = executableToInterface(dslContext, right);
TypeDef combined = Combine.TYPEDEFS.apply(Arrays.asList(leftClazz, rightClazz));
Assert.assertNotNull(combined);
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());
assertEquals(combined.getAttributes().get(ORIGINAL_RETURN_TYPE), TRANSPARENT_REF);
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class BindDefinitionTest method shouldTranslateProperty.
@Test
public void shouldTranslateProperty() {
TypeDef bound = BindDefinition.of(target.toReference(string));
assertNotNull(bound);
assertTrue(bound.getParameters().isEmpty());
Optional<Property> type = bound.getProperties().stream().filter(p -> "type".equals(p.getName())).findFirst();
assertTrue(type.isPresent());
type.ifPresent(p -> {
assertEquals(string, p.getTypeRef());
});
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class BindDefinitionTest method shouldTranslateMethod.
@Test
public void shouldTranslateMethod() {
ClassRef string = ClassRef.forName(String.class.getName());
TypeDef bound = BindDefinition.of(target.toReference(string));
assertNotNull(bound);
assertTrue(bound.getParameters().isEmpty());
Optional<Method> getType = bound.getMethods().stream().filter(p -> "getType".equals(p.getName())).findFirst();
assertTrue(getType.isPresent());
getType.ifPresent(p -> {
assertEquals(string, p.getReturnType());
});
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class Setter method find.
/**
* Find the setter of the specified property in the type.
*
* @param clazz The class.
* @param property The property.
* @return The setter method if found. Throws exception if no setter is matched.
*/
public static Method find(TypeDef clazz, Property property) {
TypeDef current = clazz;
while (current != null && !current.equals(TypeDef.OBJECT)) {
// 1st pass strict
for (Method method : current.getMethods()) {
if (isApplicable(method, property, true)) {
return method;
}
}
// 2nd pass relaxed
for (Method method : current.getMethods()) {
if (isApplicable(method, property, false)) {
return method;
}
}
if (!current.getExtendsList().iterator().hasNext()) {
break;
}
String fqn = current.getExtendsList().iterator().next().getFullyQualifiedName();
current = DefinitionRepository.getRepository().getDefinition(fqn);
}
throw new SundrException("No setter found for property: " + property.getName() + " on class: " + clazz.getFullyQualifiedName());
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class Setter method hasOrInherits.
public static boolean hasOrInherits(TypeDef clazz, Property property) {
TypeDef current = clazz;
// Iterate parent objects and check for properties with setters but not ctor arguments.
while (current != null && !current.equals(TypeDef.OBJECT)) {
for (Method method : current.getMethods()) {
if (isApplicable(method, property)) {
return true;
}
}
if (!current.getExtendsList().isEmpty()) {
String fqn = current.getExtendsList().iterator().next().getFullyQualifiedName();
current = DefinitionRepository.getRepository().getDefinition(fqn);
} else {
current = null;
}
}
return false;
}
Aggregations