Search in sources :

Example 6 with ClassRefBuilder

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

the class TypeToTypeRef method apply.

@Override
public TypeRef apply(Type type) {
    if (type instanceof VoidType) {
        return new VoidRef();
    } else if (type instanceof WildcardType) {
        return new WildcardRef();
    } else if (type instanceof ReferenceType) {
        ReferenceType referenceType = (ReferenceType) type;
        int dimensions = referenceType.getArrayCount();
        TypeRef typeRef = apply(referenceType.getType());
        if (dimensions == 0) {
            return typeRef;
        } else if (typeRef instanceof ClassRef) {
            return new ClassRefBuilder((ClassRef) typeRef).withDimensions(dimensions).build();
        } else if (typeRef instanceof PrimitiveRef) {
            return new PrimitiveRefBuilder((PrimitiveRef) typeRef).withDimensions(dimensions).build();
        } else if (typeRef instanceof TypeParamRef) {
            return new TypeParamRefBuilder((TypeParamRef) typeRef).withDimensions(dimensions).build();
        }
    } else if (type instanceof PrimitiveType) {
        PrimitiveType primitiveType = (PrimitiveType) type;
        return new PrimitiveRefBuilder().withName(primitiveType.getType().name()).build();
    } else if (type instanceof ClassOrInterfaceType) {
        return classOrInterfaceToTypeRef.apply((ClassOrInterfaceType) type);
    }
    throw new IllegalArgumentException("Can't handle type:[" + type + "].");
}
Also used : VoidType(com.github.javaparser.ast.type.VoidType) PrimitiveRefBuilder(io.sundr.model.PrimitiveRefBuilder) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) VoidRef(io.sundr.model.VoidRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) WildcardRef(io.sundr.model.WildcardRef) PrimitiveRef(io.sundr.model.PrimitiveRef) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) TypeParamRefBuilder(io.sundr.model.TypeParamRefBuilder) ReferenceType(com.github.javaparser.ast.type.ReferenceType) TypeParamRef(io.sundr.model.TypeParamRef) WildcardType(com.github.javaparser.ast.type.WildcardType) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType)

Example 7 with ClassRefBuilder

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

the class ApplyTypeParamMappingToMethod method visit.

@Override
public void visit(MethodFluent<?> 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);
            attributeKey.ifPresent(k -> method.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());
            method.withReturnType(new ClassRefBuilder(classRef).withArguments(mappedArguments).build());
            attributeKey.ifPresent(k -> method.addToAttributes(k, classRef));
        }
    }
}
Also used : TypeParamRef(io.sundr.model.TypeParamRef) List(java.util.List) ClassRefBuilder(io.sundr.model.ClassRefBuilder) TypedVisitor(io.sundr.builder.TypedVisitor) Map(java.util.Map) TypeParamRef(io.sundr.model.TypeParamRef) Optional(java.util.Optional) TypeRef(io.sundr.model.TypeRef) Collectors(java.util.stream.Collectors) MethodFluent(io.sundr.model.MethodFluent) 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 8 with ClassRefBuilder

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

the class ReplacePackage method visitPropertyBuilder.

private void visitPropertyBuilder(PropertyBuilder builder) {
    if (builder.getTypeRef() instanceof ClassRef) {
        ClassRefBuilder classRefBuilder = new ClassRefBuilder((ClassRef) builder.getTypeRef());
        builder.withTypeRef(classRefBuilder.accept(visitor, this).build());
    }
}
Also used : ClassRef(io.sundr.model.ClassRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder)

Example 9 with ClassRefBuilder

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

the class CollectionsTest method testCollections.

@Test
public void testCollections() throws Exception {
    DefinitionRepository.getRepository().register(LIST);
    DefinitionRepository.getRepository().register(ARRAY_LIST);
    DefinitionRepository.getRepository().register(LINKED_LIST);
    DefinitionRepository.getRepository().register(MAP);
    ClassRef list = new ClassRefBuilder().withFullyQualifiedName("java.util.List").build();
    ClassRef listOfString = new ClassRefBuilder().withFullyQualifiedName("java.util.List").addNewClassRefArgument().withFullyQualifiedName("java.lang.String").endClassRefArgument().build();
    ClassRef arrayList = new ClassRefBuilder().withFullyQualifiedName("java.util.ArrayList").build();
    ClassRef arrayListOfString = new ClassRefBuilder().withFullyQualifiedName("java.util.ArrayList").addNewClassRefArgument().withFullyQualifiedName("java.lang.String").endClassRefArgument().build();
    ClassRef linkedList = new ClassRefBuilder().withFullyQualifiedName("java.util.LinkedList").build();
    ClassRef linkedListOfString = new ClassRefBuilder().withFullyQualifiedName("java.util.LinkedList").addNewClassRefArgument().withFullyQualifiedName("java.lang.String").endClassRefArgument().build();
    ClassRef map = new ClassRefBuilder().withFullyQualifiedName("java.util.Map").build();
    assertTrue(Collections.IS_LIST.apply(list));
    assertTrue(Collections.IS_LIST.apply(listOfString));
    assertTrue(Collections.IS_LIST.apply(arrayList));
    assertTrue(Collections.IS_LIST.apply(arrayListOfString));
    assertTrue(Collections.IS_LIST.apply(linkedList));
    assertTrue(Collections.IS_LIST.apply(linkedListOfString));
    assertFalse(Collections.IS_LIST.apply(map));
}
Also used : ClassRef(io.sundr.model.ClassRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) Test(org.junit.Test)

Example 10 with ClassRefBuilder

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

the class TypeRefTypeVisitor method visitError.

public TypeRef visitError(ErrorType t, Integer dimension) {
    TypeElement element = (TypeElement) t.asElement();
    String fqcn = element.toString();
    return new ClassRefBuilder().withFullyQualifiedName(fqcn).build();
}
Also used : TypeElement(javax.lang.model.element.TypeElement) 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