Search in sources :

Example 26 with TypeRef

use of io.sundr.model.TypeRef in project sundrio by sundrio.

the class SimpleClassWithParameterTest method testBuilder.

@Test
public void testBuilder() {
    TypeDef builder = ClazzAs.BUILDER.apply(simpleClassWithParameterDef);
    System.out.println(builder);
    assertEquals(Kind.CLASS, builder.getKind());
    assertEquals("SimpleClassWithParameterBuilder", 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.SimpleClassWithParameter<N>", ref.render());
    assertEquals("testpackage.SimpleClassWithParameter<N>", ref.toString());
    ref = argIterator.next();
    assertEquals("testpackage.SimpleClassWithParameterBuilder<N>", ref.render());
    assertEquals("testpackage.SimpleClassWithParameterBuilder<N>", ref.toString());
}
Also used : TypeDef(io.sundr.model.TypeDef) RichTypeDef(io.sundr.model.RichTypeDef) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) Test(org.junit.Test)

Example 27 with TypeRef

use of io.sundr.model.TypeRef in project sundrio by sundrio.

the class BindDefinition method mapGenericProperties.

/**
 * Map generic properties to known {@link TypeRef} based on the specified mappings.
 * Example: Given a property {@code T size} and a map containing {@code T -> Integer} the final
 * property will be: {@code Integer type}.
 *
 * @param mappings A map that maps class arguments names to types.
 * @return a visitors that performs the actual mapping.
 */
private static TypedVisitor<PropertyBuilder> mapGenericProperties(Map<String, TypeRef> mappings) {
    return new TypedVisitor<PropertyBuilder>() {

        @Override
        public void visit(PropertyBuilder property) {
            TypeRef typeRef = property.buildTypeRef();
            if (typeRef instanceof TypeParamRef) {
                TypeParamRef typeParamRef = (TypeParamRef) typeRef;
                String key = typeParamRef.getName();
                TypeRef paramRef = mappings.get(key);
                if (paramRef != null) {
                    property.withTypeRef(paramRef);
                }
            }
        }
    };
}
Also used : TypedVisitor(io.sundr.builder.TypedVisitor) TypeParamRef(io.sundr.model.TypeParamRef) TypeRef(io.sundr.model.TypeRef) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 28 with TypeRef

use of io.sundr.model.TypeRef in project sundrio by sundrio.

the class BindDefinition method mapClassRefArguments.

/**
 * Map arguments, of {@link ClassRef} instances to known {@link TypeRef} based on the specified mappings.
 * Example: Given a class reference to {@code Shape<T>} and a map containing {@code T -> Integer}
 * the final reference will be: {@code Shape<Integer> type}.
 *
 * @param mappings A map that maps class arguments names to types.
 * @return a visitors that performs the actual mapping.
 */
private static TypedVisitor<ClassRefBuilder> mapClassRefArguments(Map<String, TypeRef> mappings) {
    return new TypedVisitor<ClassRefBuilder>() {

        @Override
        public void visit(ClassRefBuilder c) {
            List<TypeRef> arguments = new ArrayList<>();
            for (TypeRef arg : c.buildArguments()) {
                TypeRef mappedRef = arg;
                if (arg instanceof TypeParamRef) {
                    TypeParamRef typeParamRef = (TypeParamRef) arg;
                    TypeRef mapping = mappings.get(typeParamRef.getName());
                    if (mapping != null) {
                        mappedRef = mapping;
                    }
                }
                arguments.add(mappedRef);
            }
            c.withArguments(arguments);
        }
    };
}
Also used : TypedVisitor(io.sundr.builder.TypedVisitor) TypeParamRef(io.sundr.model.TypeParamRef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) ArrayList(java.util.ArrayList)

Example 29 with TypeRef

use of io.sundr.model.TypeRef 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)

Example 30 with TypeRef

use of io.sundr.model.TypeRef in project sundrio by sundrio.

the class BindDefinition method mapGenericReturnTypes.

/**
 * Map generic properties to known {@link TypeRef} based on the specified mappings.
 * Example: Given a property {@code T size} and a map containing {@code T -> Integer} the final
 * property will be: {@code Integer type}.
 *
 * @param mappings A map that maps class arguments names to types.
 * @return a visitors that performs the actual mapping.
 */
private static TypedVisitor<MethodBuilder> mapGenericReturnTypes(Map<String, TypeRef> mappings) {
    return new TypedVisitor<MethodBuilder>() {

        @Override
        public void visit(MethodBuilder method) {
            TypeRef typeRef = method.buildReturnType();
            if (typeRef instanceof TypeParamRef) {
                TypeParamRef typeParamRef = (TypeParamRef) typeRef;
                String key = typeParamRef.getName();
                TypeRef paramRef = mappings.get(key);
                if (paramRef != null) {
                    method.withReturnType(paramRef);
                }
            }
        }
    };
}
Also used : TypedVisitor(io.sundr.builder.TypedVisitor) TypeParamRef(io.sundr.model.TypeParamRef) TypeRef(io.sundr.model.TypeRef) MethodBuilder(io.sundr.model.MethodBuilder)

Aggregations

TypeRef (io.sundr.model.TypeRef)45 ClassRef (io.sundr.model.ClassRef)33 TypeDef (io.sundr.model.TypeDef)23 ArrayList (java.util.ArrayList)16 ClassRefBuilder (io.sundr.model.ClassRefBuilder)14 Method (io.sundr.model.Method)13 RichTypeDef (io.sundr.model.RichTypeDef)13 TypeDefBuilder (io.sundr.model.TypeDefBuilder)12 Property (io.sundr.model.Property)10 MethodBuilder (io.sundr.model.MethodBuilder)9 PropertyBuilder (io.sundr.model.PropertyBuilder)9 TypeParamRef (io.sundr.model.TypeParamRef)9 TypedVisitor (io.sundr.builder.TypedVisitor)8 AnnotationRef (io.sundr.model.AnnotationRef)8 List (java.util.List)8 TypeParamDef (io.sundr.model.TypeParamDef)7 HashMap (java.util.HashMap)7 AttributeKey (io.sundr.model.AttributeKey)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6