Search in sources :

Example 11 with ClassRefBuilder

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

the class SimpleClassTest method testFluentImpl.

@Test
public void testFluentImpl() {
    TypeDef fluentImpl = ClazzAs.FLUENT_IMPL.apply(simpleClassDef);
    System.out.println(fluentImpl);
    assertEquals(Kind.CLASS, fluentImpl.getKind());
    assertEquals("SimpleClassFluentImpl", fluentImpl.getName());
    assertEquals(1, fluentImpl.getExtendsList().size());
    assertTrue(hasMethod(fluentImpl, "addToTags", new ClassRefBuilder().withFullyQualifiedName(String.class.getName()).withDimensions(1).build()));
    assertTrue(hasMethod(fluentImpl, "removeFromTags", new ClassRefBuilder().withFullyQualifiedName(String.class.getName()).withDimensions(1).build()));
    ClassRef superClass = fluentImpl.getExtendsList().iterator().next();
    assertEquals("BaseFluent", superClass.getName());
    assertEquals(1, superClass.getArguments().size());
    assertEquals("A", superClass.getArguments().iterator().next().toString());
}
Also used : TypeDef(io.sundr.model.TypeDef) RichTypeDef(io.sundr.model.RichTypeDef) ClassRef(io.sundr.model.ClassRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) Test(org.junit.Test)

Example 12 with ClassRefBuilder

use of io.sundr.model.ClassRefBuilder 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 13 with ClassRefBuilder

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

the class ApplyTypeParamMappingToProperty method visit.

@Override
public void visit(PropertyFluent<?> 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);
            attributeKey.ifPresent(k -> property.addToAttributes(k, typeParamRef));
        }
    } else if (typeRef instanceof ClassRef) {
        ClassRef classRef = (ClassRef) typeRef;
        if (classRef.getArguments().stream().anyMatch(a -> a instanceof TypeParamRef)) {
            List<TypeRef> mappedArguments = classRef.getArguments().stream().map(a -> a instanceof TypeParamRef ? mappings.getOrDefault(((TypeParamRef) a).getName(), a) : a).collect(Collectors.toList());
            property.withTypeRef(new ClassRefBuilder(classRef).withArguments(mappedArguments).build());
            attributeKey.ifPresent(k -> property.addToAttributes(k, classRef));
        }
    }
}
Also used : TypeParamRef(io.sundr.model.TypeParamRef) List(java.util.List) ClassRefBuilder(io.sundr.model.ClassRefBuilder) TypedVisitor(io.sundr.builder.TypedVisitor) PropertyFluent(io.sundr.model.PropertyFluent) Map(java.util.Map) TypeParamRef(io.sundr.model.TypeParamRef) Optional(java.util.Optional) TypeRef(io.sundr.model.TypeRef) Collectors(java.util.stream.Collectors) AttributeKey(io.sundr.model.AttributeKey) ClassRef(io.sundr.model.ClassRef) Maps(io.sundr.utils.Maps) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) List(java.util.List)

Example 14 with ClassRefBuilder

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

the class ReplacePackage method visitClassRefBuilder.

private void visitClassRefBuilder(ClassRefBuilder builder) {
    builder.withFullyQualifiedName(builder.getFullyQualifiedName().replace(target, replacement)).accept(visitor);
    List<TypeRef> updatedArguments = new ArrayList<TypeRef>();
    for (TypeRef arg : builder.getArguments()) {
        if (arg instanceof ClassRef && ((ClassRef) arg).getPackageName().equals(target)) {
            updatedArguments.add(new ClassRefBuilder((ClassRef) arg).accept(visitor).build());
        } else {
            updatedArguments.add(arg);
        }
    }
    builder.withArguments(updatedArguments);
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) ArrayList(java.util.ArrayList)

Example 15 with ClassRefBuilder

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

the class ReplacePackage method visitMethodBuilder.

private void visitMethodBuilder(MethodBuilder builder) {
    if (builder.getReturnType() instanceof ClassRef) {
        ClassRefBuilder classRefBuilder = new ClassRefBuilder((ClassRef) builder.getReturnType());
        builder.withReturnType(classRefBuilder.accept(visitor, this).build());
    }
}
Also used : ClassRef(io.sundr.model.ClassRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder)

Aggregations

ClassRefBuilder (io.sundr.model.ClassRefBuilder)22 ClassRef (io.sundr.model.ClassRef)18 TypeRef (io.sundr.model.TypeRef)13 ArrayList (java.util.ArrayList)8 TypedVisitor (io.sundr.builder.TypedVisitor)6 TypeDef (io.sundr.model.TypeDef)6 TypeParamRef (io.sundr.model.TypeParamRef)6 TypeDefBuilder (io.sundr.model.TypeDefBuilder)5 AttributeKey (io.sundr.model.AttributeKey)3 Property (io.sundr.model.Property)3 PropertyBuilder (io.sundr.model.PropertyBuilder)3 RichTypeDef (io.sundr.model.RichTypeDef)3 LinkedHashSet (java.util.LinkedHashSet)3 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 ReferenceType (com.github.javaparser.ast.type.ReferenceType)2 WildcardType (com.github.javaparser.ast.type.WildcardType)2 Construct (io.sundr.builder.internal.functions.Construct)2 PrimitiveRefBuilder (io.sundr.model.PrimitiveRefBuilder)2 PropertyFluent (io.sundr.model.PropertyFluent)2 TypeParamRefBuilder (io.sundr.model.TypeParamRefBuilder)2