Search in sources :

Example 76 with TypeDef

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;
}
Also used : StringStatement(io.sundr.model.StringStatement) Statement(io.sundr.model.Statement) ArrayList(java.util.ArrayList) Method(io.sundr.model.Method) StringStatement(io.sundr.model.StringStatement) Function(java.util.function.Function) RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) BuilderContext(io.sundr.builder.internal.BuilderContext) Property(io.sundr.model.Property) SundrException(io.sundr.SundrException)

Example 77 with TypeDef

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());
}
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 78 with TypeDef

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

Example 79 with TypeDef

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;
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 80 with TypeDef

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();
}
Also used : TypeParamDef(io.sundr.model.TypeParamDef) TypeDef(io.sundr.model.TypeDef) HashMap(java.util.HashMap) TypeRef(io.sundr.model.TypeRef) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

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