Search in sources :

Example 46 with TypeDef

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);
}
Also used : JavacTypes(com.sun.tools.javac.model.JavacTypes) CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) JavacElements(com.sun.tools.javac.model.JavacElements) Generics(io.sundr.dsl.internal.type.functions.Generics) TypeElement(javax.lang.model.element.TypeElement) IS_TERMINAL(io.sundr.dsl.internal.Constants.IS_TERMINAL) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Elements(javax.lang.model.util.Elements) DslContext(io.sundr.dsl.internal.processor.DslContext) TypeDefUtils.isVoid(io.sundr.dsl.internal.utils.TypeDefUtils.isVoid) Combine(io.sundr.dsl.internal.type.functions.Combine) Assume(org.junit.Assume) ElementFilter(javax.lang.model.util.ElementFilter) TRANSPARENT_REF(io.sundr.dsl.internal.Constants.TRANSPARENT_REF) ExecutableElement(javax.lang.model.element.ExecutableElement) Test(org.junit.Test) Types(javax.lang.model.util.Types) TypeDefUtils.executableToInterface(io.sundr.dsl.internal.utils.TypeDefUtils.executableToInterface) List(java.util.List) TypeDef(io.sundr.model.TypeDef) Assert(org.junit.Assert) Context(com.sun.tools.javac.util.Context) Assert.assertEquals(org.junit.Assert.assertEquals) ORIGINAL_RETURN_TYPE(io.sundr.dsl.internal.Constants.ORIGINAL_RETURN_TYPE) TypeDefUtils(io.sundr.dsl.internal.utils.TypeDefUtils) TypeDef(io.sundr.model.TypeDef) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Test(org.junit.Test)

Example 47 with TypeDef

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());
    });
}
Also used : TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) Types.modifiersToInt(io.sundr.model.utils.Types.modifiersToInt) Modifier(javax.lang.model.element.Modifier) Assert.assertNotNull(org.junit.Assert.assertNotNull) DefinitionRepository(io.sundr.model.repo.DefinitionRepository) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Method(io.sundr.model.Method) Kind(io.sundr.model.Kind) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Property(io.sundr.model.Property) ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) Optional(java.util.Optional) TypeParamDef(io.sundr.model.TypeParamDef) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) TypeDef(io.sundr.model.TypeDef) Property(io.sundr.model.Property) Test(org.junit.Test)

Example 48 with TypeDef

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());
    });
}
Also used : TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) Types.modifiersToInt(io.sundr.model.utils.Types.modifiersToInt) Modifier(javax.lang.model.element.Modifier) Assert.assertNotNull(org.junit.Assert.assertNotNull) DefinitionRepository(io.sundr.model.repo.DefinitionRepository) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Method(io.sundr.model.Method) Kind(io.sundr.model.Kind) TypeDefBuilder(io.sundr.model.TypeDefBuilder) Property(io.sundr.model.Property) ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) Optional(java.util.Optional) TypeParamDef(io.sundr.model.TypeParamDef) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) Method(io.sundr.model.Method) Test(org.junit.Test)

Example 49 with TypeDef

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());
}
Also used : TypeDef(io.sundr.model.TypeDef) Method(io.sundr.model.Method) SundrException(io.sundr.SundrException)

Example 50 with TypeDef

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;
}
Also used : TypeDef(io.sundr.model.TypeDef) Method(io.sundr.model.Method)

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