Search in sources :

Example 1 with TypeParamRef

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

the class Combine method extractParameters.

private static final Set<TypeParamDef> extractParameters(ClassRef classRef) {
    final Set<TypeParamDef> result = new LinkedHashSet<TypeParamDef>();
    final Set<TypeParamRef> refs = new LinkedHashSet<TypeParamRef>();
    ClassRef ignored = new ClassRefBuilder(classRef).accept(new TypeParamDefColletor(result)).accept(new TypeParamRefColletor(refs)).build();
    for (TypeParamRef typeParamRef : refs) {
        result.add(new TypeParamDefBuilder().withName(typeParamRef.getName()).withAttributes(typeParamRef.getAttributes()).build());
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeParamRef(io.sundr.model.TypeParamRef) TypeParamDef(io.sundr.model.TypeParamDef) TypeParamRefColletor(io.sundr.dsl.internal.visitors.TypeParamRefColletor) ClassRef(io.sundr.model.ClassRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) TypeParamDefColletor(io.sundr.dsl.internal.visitors.TypeParamDefColletor)

Example 2 with TypeParamRef

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

the class ClazzAs method toInstanceConstructorBody.

private static List<Statement> toInstanceConstructorBody(TypeDef clazz, TypeDef instance, String fluent) {
    Method constructor = findBuildableConstructor(clazz);
    List<Statement> statements = new ArrayList<Statement>();
    final String ref = fluent != null && !fluent.isEmpty() ? fluent : "this";
    // We may use a reference to fluent or we may use directly "this". So we need to check.
    if (fluent != null && !fluent.isEmpty()) {
        statements.add(new StringStatement("this.fluent = " + fluent + "; "));
    }
    for (Property property : constructor.getArguments()) {
        Optional<Method> getter = Getter.findOptional(instance, property);
        getter.ifPresent(g -> {
            String cast = property.getTypeRef() instanceof TypeParamRef ? "(" + property.getTypeRef().toString() + ")" : "";
            statements.add(new StringStatement(new StringBuilder().append(ref).append(".with").append(property.getNameCapitalized()).append("(").append(cast).append("instance.").append(g.getName()).append("()); ").toString()));
        });
    // } else {
    // throw new IllegalStateException("Could not find getter for property:" + property + " in class:" + clazz);
    // }
    }
    TypeDef target = clazz;
    // Iterate parent objects and check for properties with setters but not ctor arguments.
    while (target != null && !Types.OBJECT.equals(target) && BuilderUtils.isBuildable(target)) {
        for (Property property : target.getProperties()) {
            if (!hasBuildableConstructorWithArgument(target, property) && Setter.has(target, property)) {
                Getter.findOptional(instance, property).map(Method::getName).ifPresent(getterName -> {
                    String withName = "with" + property.getNameCapitalized();
                    statements.add(new StringStatement(new StringBuilder().append(ref).append(".").append(withName).append("(instance.").append(getterName).append("());\n").toString()));
                });
            }
        }
        if (!target.getExtendsList().isEmpty()) {
            target = BuilderContextManager.getContext().getBuildableRepository().getBuildable(target.getExtendsList().iterator().next());
        } else {
            return statements;
        }
    }
    return statements;
}
Also used : TypeParamRef(io.sundr.model.TypeParamRef) RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) StringStatement(io.sundr.model.StringStatement) Statement(io.sundr.model.Statement) ArrayList(java.util.ArrayList) Method(io.sundr.model.Method) StringStatement(io.sundr.model.StringStatement) Property(io.sundr.model.Property)

Example 3 with TypeParamRef

use of io.sundr.model.TypeParamRef 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 4 with TypeParamRef

use of io.sundr.model.TypeParamRef 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 5 with TypeParamRef

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

the class ApplyTypeParamMappingToTypeArguments method visit.

@Override
public void visit(ClassRefFluent<?> classRef) {
    List<TypeRef> arguments = new ArrayList<>();
    for (TypeRef arg : classRef.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);
    }
    classRef.withArguments(arguments);
}
Also used : TypeParamRef(io.sundr.model.TypeParamRef) TypeRef(io.sundr.model.TypeRef) ArrayList(java.util.ArrayList)

Aggregations

TypeParamRef (io.sundr.model.TypeParamRef)11 TypeRef (io.sundr.model.TypeRef)8 ClassRefBuilder (io.sundr.model.ClassRefBuilder)6 TypedVisitor (io.sundr.builder.TypedVisitor)5 ClassRef (io.sundr.model.ClassRef)5 ArrayList (java.util.ArrayList)4 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)2 ReferenceType (com.github.javaparser.ast.type.ReferenceType)2 WildcardType (com.github.javaparser.ast.type.WildcardType)2 TypeParamDefColletor (io.sundr.dsl.internal.visitors.TypeParamDefColletor)2 TypeParamRefColletor (io.sundr.dsl.internal.visitors.TypeParamRefColletor)2 AttributeKey (io.sundr.model.AttributeKey)2 TypeDef (io.sundr.model.TypeDef)2 TypeParamDef (io.sundr.model.TypeParamDef)2 TypeParamDefBuilder (io.sundr.model.TypeParamDefBuilder)2 TypeParamRefBuilder (io.sundr.model.TypeParamRefBuilder)2 WildcardRef (io.sundr.model.WildcardRef)2 Maps (io.sundr.utils.Maps)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2