Search in sources :

Example 86 with TypeDef

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

the class AbstractBuilderProcessor method generatePojos.

/**
 * Returns true if pojos where generated.
 *
 * @param builderContext The builder context.
 * @param buildables The set of buildables.
 */
public void generatePojos(BuilderContext builderContext, Set<TypeDef> buildables) {
    Set<TypeDef> additonalBuildables = new HashSet<>();
    Set<TypeDef> additionalTypes = new HashSet<>();
    for (TypeDef typeDef : buildables) {
        RichTypeDef richTypeDef = TypeArguments.apply(typeDef);
        if (typeDef.isInterface() || typeDef.isAnnotation()) {
            typeDef = ClazzAs.POJO.apply(richTypeDef);
            builderContext.getDefinitionRepository().register(typeDef);
            builderContext.getBuildableRepository().register(typeDef);
            generate(typeDef);
            additonalBuildables.add(typeDef);
            if (typeDef.hasAttribute(ADDITIONAL_BUILDABLES)) {
                for (TypeDef also : typeDef.getAttribute(ADDITIONAL_BUILDABLES)) {
                    builderContext.getDefinitionRepository().register(also);
                    builderContext.getBuildableRepository().register(also);
                    generate(also);
                    additonalBuildables.add(also);
                }
            }
            if (typeDef.hasAttribute(ADDITIONAL_TYPES)) {
                for (TypeDef also : typeDef.getAttribute(ADDITIONAL_TYPES)) {
                    builderContext.getDefinitionRepository().register(also);
                    generate(also);
                    additionalTypes.add(also);
                }
            }
        }
    }
    generateBuildables(builderContext, additonalBuildables);
}
Also used : RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) HashSet(java.util.HashSet) RichTypeDef(io.sundr.model.RichTypeDef)

Example 87 with TypeDef

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

the class BuilderUtils method isBuildable.

/**
 * Checks if {@link ClassRef} is buildable.
 *
 * @param ref The reference.
 * @return True if buildable repository contains the ref or builder for the reference is present.
 */
public static boolean isBuildable(ClassRef ref) {
    if (BuilderContextManager.getContext().getBuildableRepository().isBuildable(ref)) {
        return true;
    }
    String builderFQCN = ref.getFullyQualifiedName() + "Builder";
    TypeDef builder = BuilderContextManager.getContext().getDefinitionRepository().getDefinition(builderFQCN);
    if (builder == null) {
        return false;
    }
    return builder.getMethods().stream().filter(m -> "build".equals(m.getName())).filter(m -> m.getReturnType() instanceof ClassRef).map(m -> (ClassRef) m.getReturnType()).filter(r -> Assignable.isAssignable(r).from(ref)).count() > 0;
}
Also used : INIT(io.sundr.model.Attributeable.INIT) Arrays(java.util.Arrays) TypeParamDefBuilder(io.sundr.model.TypeParamDefBuilder) Descendants(io.sundr.builder.internal.functions.Descendants) Optionals(io.sundr.model.utils.Optionals) TypeElement(javax.lang.model.element.TypeElement) Attributeable(io.sundr.model.Attributeable) Nameable(io.sundr.model.Nameable) ClassRef(io.sundr.model.ClassRef) Getter(io.sundr.model.utils.Getter) STRING_REF(io.sundr.model.utils.Types.STRING_REF) Map(java.util.Map) MirroredTypeException(javax.lang.model.type.MirroredTypeException) BuildableRepository(io.sundr.builder.internal.BuildableRepository) Path(java.nio.file.Path) Collections(io.sundr.model.utils.Collections) BuilderContext(io.sundr.builder.internal.BuilderContext) DefinitionRepository(io.sundr.model.repo.DefinitionRepository) Collection(java.util.Collection) Set(java.util.Set) Element(javax.lang.model.element.Element) Method(io.sundr.model.Method) Construct(io.sundr.builder.internal.functions.Construct) Collectors(java.util.stream.Collectors) io.sundr.builder.annotations(io.sundr.builder.annotations) ALSO_IMPORT(io.sundr.model.Attributeable.ALSO_IMPORT) List(java.util.List) ClassRefBuilder(io.sundr.model.ClassRefBuilder) Stream(java.util.stream.Stream) MethodBuilder(io.sundr.model.MethodBuilder) BuilderContextManager(io.sundr.builder.internal.BuilderContextManager) TypeParamDef(io.sundr.model.TypeParamDef) Strings.capitalizeFirst(io.sundr.utils.Strings.capitalizeFirst) StringStatement(io.sundr.model.StringStatement) TypeParamRef(io.sundr.model.TypeParamRef) GetDefinition(io.sundr.model.functions.GetDefinition) TypeDefBuilder(io.sundr.model.TypeDefBuilder) AdapterContext(io.sundr.adapter.api.AdapterContext) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) INIT_FUNCTION(io.sundr.model.Attributeable.INIT_FUNCTION) Assignable(io.sundr.model.functions.Assignable) Types(io.sundr.model.utils.Types) LinkedHashSet(java.util.LinkedHashSet) Constants(io.sundr.builder.Constants) AptContext(io.sundr.adapter.apt.AptContext) TypedVisitor(io.sundr.builder.TypedVisitor) Adapters(io.sundr.adapter.api.Adapters) LAZY_INIT(io.sundr.model.Attributeable.LAZY_INIT) TypeRef(io.sundr.model.TypeRef) Types.isAbstract(io.sundr.model.utils.Types.isAbstract) AnnotationRef(io.sundr.model.AnnotationRef) Kind(io.sundr.model.Kind) Property(io.sundr.model.Property) File(java.io.File) LAZY_COLLECTIONS_INIT_ENABLED(io.sundr.builder.Constants.LAZY_COLLECTIONS_INIT_ENABLED) Statement(io.sundr.model.Statement) TypeDef(io.sundr.model.TypeDef) DEFAULT_VALUE(io.sundr.model.Attributeable.DEFAULT_VALUE) PropertyBuilder(io.sundr.model.PropertyBuilder) TypeAs(io.sundr.builder.internal.functions.TypeAs) DESCENDANTS(io.sundr.builder.Constants.DESCENDANTS) TypeDef(io.sundr.model.TypeDef) ClassRef(io.sundr.model.ClassRef)

Example 88 with TypeDef

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

the class BuilderUtils method findBuildableReferences.

/**
 * Returns all references of a {@link ClassRef} that are considered buildable.
 *
 * @param ref The reference.
 * @return The list with all detected references.
 */
public static List<ClassRef> findBuildableReferences(ClassRef ref) {
    List<ClassRef> result = new ArrayList<>();
    TypeDef def = new TypeDefBuilder(GetDefinition.of(ref)).accept(new TypedVisitor<ClassRefBuilder>() {

        @Override
        public void visit(ClassRefBuilder builder) {
            ClassRef candidate = builder.build();
            if (isBuildable(candidate)) {
                result.add(candidate);
            }
        }
    }).build();
    return result;
}
Also used : TypedVisitor(io.sundr.builder.TypedVisitor) ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) ArrayList(java.util.ArrayList) TypeDefBuilder(io.sundr.model.TypeDefBuilder)

Example 89 with TypeDef

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

the class BuilderUtils method buildableField.

public static Property buildableField(Property property) {
    TypeRef typeRef = property.getTypeRef();
    ClassRef targetType = (ClassRef) TypeAs.combine(UNWRAP_COLLECTION_OF, UNWRAP_ARRAY_OF, UNWRAP_OPTIONAL_OF).apply(typeRef);
    boolean isArray = Types.isArray(typeRef);
    boolean isSet = Types.isSet(typeRef);
    boolean isAbstractSet = isSet && Types.isAbstract(typeRef);
    boolean isList = Types.isList(typeRef);
    boolean isAbstractList = isList && Types.isAbstract(typeRef);
    boolean isMap = Types.isMap(typeRef);
    boolean isOptional = Types.isOptional(typeRef);
    boolean isOptionalDouble = Types.isOptionalDouble(typeRef);
    boolean isOptionalLong = Types.isOptionalLong(typeRef);
    // For fields that are concrete we can possibly create an instance of a VisitableBuilder.
    // For everything else we can have a builder e.g. Builder<Foo> = () -> fooInstance but it won't be visitable
    ClassRef builderType = Types.isConcrete(targetType) ? TypeAs.BUILDER.apply(GetDefinition.of(targetType)).toInternalReference() : TypeAs.VISITABLE_BUILDER.apply(targetType);
    if (isArray || isList) {
        ClassRef listRef = isArray || isAbstractList ? Collections.ARRAY_LIST.toReference(builderType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(builderType).withDimensions(0).build();
        TypeDef listDef = new TypeDefBuilder(TypeDef.forName(listRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.LIST.toReference(builderType)).withName("l").endArgument().endConstructor().build();
        return new PropertyBuilder(property).withTypeRef(listRef).addToAttributes(LAZY_INIT, " new " + listRef + "()").addToAttributes(INIT_FUNCTION, new Construct(listDef, targetType)).addToAttributes(ALSO_IMPORT, alsoImport(property, listRef, builderType)).build();
    }
    if (isSet) {
        ClassRef setRef = isAbstractSet ? Collections.LINKED_HASH_SET.toReference(builderType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(builderType).build();
        TypeDef setDef = new TypeDefBuilder(TypeDef.forName(setRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.SET.toReference(builderType)).withName("s").endArgument().endConstructor().build();
        return new PropertyBuilder(property).withTypeRef(setRef).addToAttributes(LAZY_INIT, " new " + setRef + "()").addToAttributes(INIT_FUNCTION, new Construct(setDef, targetType)).addToAttributes(ALSO_IMPORT, alsoImport(property, setRef, builderType)).build();
    }
    if (isOptionalLong) {
        ClassRef optionalRef = Optionals.OPTIONAL_LONG.toReference(builderType);
        return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " OptionalLong.empty()").build();
    }
    if (isOptionalDouble) {
        ClassRef optionalRef = Optionals.OPTIONAL_DOUBLE.toReference(builderType);
        return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " OptionalDouble.empty()").build();
    }
    if (Types.isOptionalInt(targetType)) {
        ClassRef optionalRef = Optionals.OPTIONAL_INT.toReference(builderType);
        return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " OptionalInt.empty()").build();
    }
    if (isOptional) {
        ClassRef optionalRef = Optionals.OPTIONAL.toReference(builderType);
        return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " Optional.empty()").addToAttributes(ALSO_IMPORT, alsoImport(property, optionalRef, builderType)).build();
    }
    if (Types.isConcrete(builderType) && BuilderUtils.hasDefaultConstructor(builderType) && property.hasAttribute(DEFAULT_VALUE)) {
        return new PropertyBuilder(property).withTypeRef(builderType).addToAttributes(ALSO_IMPORT, alsoImport(property, builderType)).addToAttributes(INIT, "new " + builderType + "()").build();
    }
    return new PropertyBuilder(property).withTypeRef(builderType).addToAttributes(ALSO_IMPORT, alsoImport(property, builderType)).removeFromAttributes(INIT).build();
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) Construct(io.sundr.builder.internal.functions.Construct) TypeDefBuilder(io.sundr.model.TypeDefBuilder) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 90 with TypeDef

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

the class InitEnricher method visit.

@Override
public void visit(PropertyBuilder builder) {
    TypeRef typeRef = builder.buildTypeRef();
    TypeRef unwrapped = TypeAs.combine(TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(typeRef);
    boolean isBuildable = isBuildable(unwrapped);
    boolean hasDescendants = false;
    if (!(typeRef instanceof ClassRef)) {
        return;
    }
    Property parent = (Property) builder.getAttributes().get(DESCENDANT_OF);
    if (parent != null) {
        typeRef = parent.getTypeRef();
        unwrapped = TypeAs.combine(TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(typeRef);
    } else if (builder.getAttributes().containsKey(DESCENDANTS) && !((Collection) builder.getAttributes().get(DESCENDANTS)).isEmpty()) {
        hasDescendants = true;
    }
    List<TypeRef> arguments = ((ClassRef) typeRef).getArguments();
    TypeRef targetType = unwrapped;
    if (isBuildable || hasDescendants) {
        ClassRef unwarppedClassRef = (unwrapped instanceof ClassRef) ? (ClassRef) unwrapped : null;
        targetType = isAbstract(unwarppedClassRef) || GetDefinition.of(unwarppedClassRef).getKind() == Kind.INTERFACE ? TypeAs.VISITABLE_BUILDER.apply(unwarppedClassRef) : TypeAs.BUILDER.apply(GetDefinition.of(unwarppedClassRef)).toInternalReference();
    }
    boolean isArray = Types.isArray(typeRef);
    boolean isSet = Types.isSet(typeRef);
    boolean isAbstractSet = isSet && Types.isAbstract(typeRef);
    boolean isList = Types.isList(typeRef);
    boolean isAbstractList = isList && Types.isAbstract(typeRef);
    boolean isMap = Types.isMap(typeRef);
    boolean isAbstractMap = isMap && Types.isAbstract(typeRef);
    boolean isOptional = Types.isOptional(typeRef);
    boolean isOptionalInt = Types.isOptionalInt(typeRef);
    boolean isOptionalDouble = Types.isOptionalDouble(typeRef);
    boolean isOptionalLong = Types.isOptionalLong(typeRef);
    if (isArray || isList) {
        ClassRef listRef = isArray || isAbstractList ? Collections.ARRAY_LIST.toReference(targetType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(targetType).withDimensions(0).build();
        TypeDef listDef = new TypeDefBuilder(TypeDef.forName(listRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.LIST.toReference(targetType)).withName("l").endArgument().endConstructor().build();
        builder.addToAttributes(LAZY_INIT, "new " + listRef + "()").addToAttributes(INIT, builder.getAttributes().containsKey(LAZY_COLLECTIONS_INIT_ENABLED) && (Boolean) builder.getAttributes().get(LAZY_COLLECTIONS_INIT_ENABLED) ? null : builder.getAttributes().get(LAZY_INIT)).addToAttributes(INIT_FUNCTION, new Construct(listDef, targetType)).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, listRef));
    } else if (isSet) {
        ClassRef setRef = isAbstractSet ? Collections.LINKED_HASH_SET.toReference(targetType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(targetType).build();
        TypeDef setDef = new TypeDefBuilder(TypeDef.forName(setRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.SET.toReference(targetType)).withName("s").endArgument().endConstructor().build();
        builder.addToAttributes(LAZY_INIT, "new " + setRef + "()").addToAttributes(INIT, builder.getAttributes().containsKey(LAZY_COLLECTIONS_INIT_ENABLED) && (Boolean) builder.getAttributes().get(LAZY_COLLECTIONS_INIT_ENABLED) ? null : builder.getAttributes().get(LAZY_INIT)).addToAttributes(INIT_FUNCTION, new Construct(setDef, unwrapped)).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, setRef));
    } else if (isMap) {
        ClassRef mapRef = isAbstractMap ? Collections.LINKED_HASH_MAP.toReference(arguments) : new ClassRefBuilder((ClassRef) typeRef).withArguments(arguments).build();
        TypeDef mapDef = new TypeDefBuilder(TypeDef.forName(mapRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.MAP.toReference(arguments)).withName("m").endArgument().endConstructor().build();
        builder.addToAttributes(LAZY_INIT, "new " + mapRef + "()").addToAttributes(INIT, builder.getAttributes().containsKey(LAZY_MAP_INIT_ENABLED) && (Boolean) builder.getAttributes().get(LAZY_MAP_INIT_ENABLED) ? null : builder.getAttributes().get(LAZY_INIT)).addToAttributes(INIT_FUNCTION, new Construct(mapDef, arguments)).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, mapRef));
    } else if (isOptional) {
        final ClassRef ref = new ClassRefBuilder(Optionals.OPTIONAL.toReference()).withArguments(java.util.Collections.EMPTY_LIST).build();
        builder.addToAttributes(INIT, "Optional.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
    } else if (isOptionalDouble) {
        final ClassRef ref = Optionals.OPTIONAL_DOUBLE.toReference();
        builder.addToAttributes(INIT, "OptionalDouble.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL_DOUBLE, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
    } else if (isOptionalInt) {
        final ClassRef ref = Optionals.OPTIONAL_INT.toReference();
        builder.addToAttributes(INIT, "OptionalInt.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL_INT, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
    } else if (isOptionalLong) {
        final ClassRef ref = Optionals.OPTIONAL_LONG.toReference();
        builder.addToAttributes(INIT, "OptionalLong.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL_LONG, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
    }
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) Construct(io.sundr.builder.internal.functions.Construct) Property(io.sundr.model.Property) 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