Search in sources :

Example 11 with Property

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

the class BuilderUtils method toEquals.

public static List<Statement> toEquals(TypeDef type, Collection<Property> properties) {
    List<Statement> statements = new ArrayList<>();
    String simpleName = type.getName();
    ClassRef superClass = type.getExtendsList().isEmpty() ? TypeDef.OBJECT_REF : type.getExtendsList().iterator().next();
    statements.add(new StringStatement("if (this == o) return true;"));
    statements.add(new StringStatement("if (o == null || getClass() != o.getClass()) return false;"));
    // If base fluent is the superclass just skip.
    final BuilderContext context = BuilderContextManager.getContext();
    final String superClassFQN = superClass.getFullyQualifiedName();
    if (!context.getBaseFluentClass().getFullyQualifiedName().equals(superClassFQN) && !OBJECT_FULLY_QUALIFIED_NAME.equals(superClassFQN)) {
        statements.add(new StringStatement("if (!super.equals(o)) return false;"));
    }
    statements.add(new StringStatement(new StringBuilder().append(simpleName).append(" that = (").append(simpleName).append(") o;").toString()));
    for (Property property : properties) {
        String name = property.getName();
        if (Types.isPrimitive(property.getTypeRef())) {
            statements.add(new StringStatement(new StringBuilder().append("if (").append(name).append(" != ").append("that.").append(name).append(") return false;").toString()));
        } else if (property.getTypeRef() instanceof ClassRef && Descendants.isDescendant(type, GetDefinition.of((ClassRef) property.getTypeRef()))) {
            statements.add(new StringStatement(new StringBuilder().append("if (").append(name).append(" != null &&").append(name).append(" != this ? !").append(name).append(".equals(that.").append(name).append(") :").append("that.").append(name).append(" != null &&").append(name).append(" != this ) return false;").append("\n").toString()));
        } else {
            statements.add(new StringStatement(new StringBuilder().append("if (").append(name).append(" != null ? !").append(name).append(".equals(that.").append(name).append(") :").append("that.").append(name).append(" != null) return false;").toString()));
        }
    }
    statements.add(new StringStatement("return true;"));
    return statements;
}
Also used : ClassRef(io.sundr.model.ClassRef) StringStatement(io.sundr.model.StringStatement) Statement(io.sundr.model.Statement) ArrayList(java.util.ArrayList) BuilderContext(io.sundr.builder.internal.BuilderContext) StringStatement(io.sundr.model.StringStatement) Property(io.sundr.model.Property)

Example 12 with Property

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

the class BuilderUtils method getInlineableConstructors.

public static Set<Method> getInlineableConstructors(Property property) {
    Set<Method> result = new HashSet<Method>();
    TypeRef typeRef = property.getTypeRef();
    TypeRef unwrapped = TypeAs.combine(TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(typeRef);
    if (unwrapped instanceof ClassRef) {
        ClassRef classRef = (ClassRef) unwrapped;
        // We need to handle `new String(String str)` as a special case of Inlineable constructor and deprecate Inlineables of it before we acutally remove it, so here goes...
        if (STRING_REF.equals(typeRef)) {
            result.add(new MethodBuilder().withName("String").addNewArgument().withName("s").withTypeRef(classRef).endArgument().build());
            return result;
        }
        // We only want to inline non java types
        String pkg = Nameable.getPackageName(((ClassRef) unwrapped).getFullyQualifiedName());
        if (!Stream.of(NON_INLINABLE_PACKAGES).filter(s -> pkg.startsWith(s)).findAny().isPresent()) {
            for (Method candidate : GetDefinition.of((ClassRef) unwrapped).getConstructors()) {
                if (isInlineable(candidate)) {
                    result.add(candidate);
                }
            }
        }
    }
    return result;
}
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) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) Method(io.sundr.model.Method) MethodBuilder(io.sundr.model.MethodBuilder) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 13 with Property

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

the class ToPojo method getDefaultValue.

private static String getDefaultValue(Attributeable attributeable) {
    Object value = attributeable.getAttribute(DEFAULT_VALUE);
    String stringVal = value != null ? String.valueOf(value) : null;
    TypeRef typeRef = null;
    if (attributeable instanceof Method) {
        typeRef = ((Method) attributeable).getReturnType();
    } else if (attributeable instanceof Property) {
        typeRef = ((Property) attributeable).getTypeRef();
    } else {
        throw new IllegalArgumentException("Method 'getDefaultValue' accepts Property or Method as argument!");
    }
    if (typeRef.getDimensions() > 0) {
        StringBuilder sb = new StringBuilder();
        if (typeRef instanceof PrimitiveRef) {
            sb.append(((PrimitiveRef) typeRef).getName());
        } else if (typeRef instanceof ClassRef) {
            sb.append("new ").append(((ClassRef) typeRef).getFullyQualifiedName());
        }
        for (int d = 0; d < typeRef.getDimensions(); d++) {
            sb.append("[0]");
        }
        return sb.toString();
    }
    if (Types.STRING_REF.equals(typeRef) && value != null && !String.valueOf(value).startsWith("\"")) {
        return "\"" + value + "\"";
    } else if (value instanceof Element) {
        Element element = (Element) value;
        if (element.getKind() == ElementKind.ENUM_CONSTANT) {
            return element.getEnclosingElement() + "." + element.getSimpleName();
        }
    } else if (stringVal != null && stringVal.startsWith("@")) {
        String annotationFQCN = stringVal.substring(1);
        TypeDef annotationDef = DefinitionRepository.getRepository().getDefinition(annotationFQCN);
        if (annotationDef != null) {
            TypeDef pojoDef = ClazzAs.POJO.apply(TypeArguments.apply(annotationDef));
            Optional<AnnotationRef> pojoAnnotation = getPojoAnnotation(pojoDef);
            Optional<Method> builderFromDefaults = getBuilderFromDefaults(pojoDef);
            if (pojoAnnotation.isPresent() && builderFromDefaults.isPresent()) {
                return pojoDef.getFullyQualifiedName() + "." + builderFromDefaults.get().getName() + "().build()";
            } else if (BuilderUtils.hasDefaultConstructor(pojoDef)) {
                return "new " + pojoDef.getFullyQualifiedName() + "()";
            }
        }
        return "null";
    } else if (stringVal == null && typeRef instanceof PrimitiveRef) {
        if (((PrimitiveRef) typeRef).getName().equals("boolean")) {
            return "false";
        } else {
            return "0";
        }
    }
    return stringVal;
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) Element(javax.lang.model.element.Element) Method(io.sundr.model.Method) PrimitiveRef(io.sundr.model.PrimitiveRef) RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) Property(io.sundr.model.Property) AnnotationRef(io.sundr.model.AnnotationRef)

Example 14 with Property

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

the class AbstractBuilderProcessor method inlineableOf.

static TypeDef inlineableOf(BuilderContext ctx, TypeDef type, Inline inline) {
    final String inlineableName = !inline.name().isEmpty() ? inline.name() : inline.prefix() + type.getName() + inline.suffix();
    List<Method> constructors = new ArrayList<Method>();
    final TypeDef builderType = TypeAs.BUILDER.apply(type);
    TypeDef inlineType = BuilderUtils.getInlineType(ctx, inline);
    TypeDef returnType = BuilderUtils.getInlineReturnType(ctx, inline, type);
    final ClassRef inlineTypeRef = inlineType.toReference(returnType.toReference());
    // Use the builder as the base of the inlineable. Just add interface and change name.
    final TypeDef shallowInlineType = new TypeDefBuilder(builderType).withName(inlineableName).withImplementsList(inlineTypeRef).withProperties().withMethods().withConstructors().build();
    TypeRef functionType = Constants.FUNCTION.toReference(type.toInternalReference(), returnType.toInternalReference());
    Property builderProperty = new PropertyBuilder().withTypeRef(TypeAs.BUILDER.apply(type).toInternalReference()).withName(BUILDER).withModifiers(Types.modifiersToInt(Modifier.PRIVATE, Modifier.FINAL)).build();
    Property functionProperty = new PropertyBuilder().withTypeRef(functionType).withName(FUNCTION).withModifiers(Types.modifiersToInt(Modifier.PRIVATE, Modifier.FINAL)).build();
    Method inlineMethod = new MethodBuilder().withReturnType(returnType.toInternalReference()).withName(inline.value()).withNewBlock().addNewStringStatementStatement(BUILD_AND_APPLY_FUNCTION).endBlock().withModifiers(Types.modifiersToInt(Modifier.PUBLIC)).build();
    constructors.add(new MethodBuilder().withReturnType(inlineTypeRef).withName(EMPTY).addNewArgument().withName(FUNCTION).withTypeRef(functionType).and().withModifiers(Types.modifiersToInt(Modifier.PUBLIC)).withNewBlock().addNewStringStatementStatement(String.format(NEW_BULDER_AND_SET_FUNCTION_FORMAT, builderType.getName())).endBlock().build());
    constructors.add(new MethodBuilder().withReturnType(inlineTypeRef).withName(EMPTY).addNewArgument().withName(ITEM).withTypeRef(type.toInternalReference()).and().addNewArgument().withName(FUNCTION).withTypeRef(functionType).and().withModifiers(Types.modifiersToInt(Modifier.PUBLIC)).withNewBlock().addNewStringStatementStatement(String.format(NEW_BULDER_WITH_ITEM_AND_SET_FUNCTION_FORMAT, builderType.getName())).endBlock().build());
    if (type.equals(returnType)) {
        constructors.add(new MethodBuilder().withReturnType(inlineTypeRef).withName(EMPTY).addNewArgument().withName(ITEM).withTypeRef(type.toInternalReference()).and().withModifiers(Types.modifiersToInt(Modifier.PUBLIC)).withNewBlock().addNewStringStatementStatement(String.format(NEW_BUILDER_AND_EMTPY_FUNCTION_FORMAT, builderType.getName(), String.format(EMPTY_FUNCTION_TEXT, type.toInternalReference(), returnType.toInternalReference(), returnType.toInternalReference(), type.toInternalReference()))).endBlock().build());
    }
    return new TypeDefBuilder(shallowInlineType).withAnnotations().withModifiers(Types.modifiersToInt(Modifier.PUBLIC)).withConstructors(constructors).addToProperties(builderProperty, functionProperty).addToMethods(inlineMethod).accept(new TypedVisitor<ClassRefBuilder>() {

        @Override
        public void visit(ClassRefBuilder builder) {
            List<TypeRef> updatedArguments = new ArrayList<TypeRef>();
            for (TypeRef arg : builder.getArguments()) {
                if (arg.equals(builderType.toInternalReference())) {
                    updatedArguments.add(shallowInlineType.toInternalReference());
                } else {
                    updatedArguments.add(arg);
                }
            }
            builder.withArguments(updatedArguments);
        }
    }).build();
}
Also used : TypedVisitor(io.sundr.builder.TypedVisitor) ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef) ClassRefBuilder(io.sundr.model.ClassRefBuilder) ArrayList(java.util.ArrayList) Method(io.sundr.model.Method) TypeDefBuilder(io.sundr.model.TypeDefBuilder) MethodBuilder(io.sundr.model.MethodBuilder) RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) Property(io.sundr.model.Property) PropertyBuilder(io.sundr.model.PropertyBuilder)

Example 15 with Property

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

Aggregations

Property (io.sundr.model.Property)25 TypeDef (io.sundr.model.TypeDef)18 ClassRef (io.sundr.model.ClassRef)17 ArrayList (java.util.ArrayList)16 Method (io.sundr.model.Method)15 TypeDefBuilder (io.sundr.model.TypeDefBuilder)13 AnnotationRef (io.sundr.model.AnnotationRef)11 TypeRef (io.sundr.model.TypeRef)10 MethodBuilder (io.sundr.model.MethodBuilder)9 HashMap (java.util.HashMap)9 TypeParamDef (io.sundr.model.TypeParamDef)8 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 AttributeKey (io.sundr.model.AttributeKey)7 PropertyBuilder (io.sundr.model.PropertyBuilder)7 HashSet (java.util.HashSet)7 Map (java.util.Map)7 Kind (io.sundr.model.Kind)6 RichTypeDef (io.sundr.model.RichTypeDef)6 Statement (io.sundr.model.Statement)6