Search in sources :

Example 11 with ClassRef

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

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

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

the class ToPojo method readObjectArrayValue.

/**
 * Returns the string representation of the code that reads an object array property.
 *
 * @param ref The reference.
 * @param source The type of the reference.
 * @param property The property to read.
 * @return The code.
 */
private static String readObjectArrayValue(String ref, TypeDef source, Property property) {
    StringBuilder sb = new StringBuilder();
    Method getter = getterOf(source, property);
    TypeRef getterTypeRef = getter.getReturnType();
    TypeRef propertyTypeRef = property.getTypeRef();
    if (propertyTypeRef instanceof ClassRef && getterTypeRef instanceof ClassRef) {
        String nextRef = variables.pop();
        try {
            TypeDef propertyType = GetDefinition.of((ClassRef) propertyTypeRef);
            TypeDef getterType = GetDefinition.of((ClassRef) getterTypeRef);
            if (Types.STRING.equals(getterType)) {
                sb.append(ref + " instanceof Map ? toStringArray(((Map)" + ref + ").get(\"" + getter.getName() + "\")) : toStringArray(" + ref + ")");
            } else {
                sb.append(indent(ref)).append("Arrays.stream(");
                sb.append("(Map[])(" + ref + " instanceof Map ? ((Map)" + ref + ").getOrDefault(\"" + getter.getName() + "\" , new Map[0]) : new Map[0]))");
                sb.append(".map(").append(nextRef).append(" ->").append(convertMap(nextRef, getterType, propertyType)).append(")");
                sb.append(".toArray(size-> new " + propertyType.getFullyQualifiedName() + "[size])");
            }
        } finally {
            variables.push(nextRef);
        }
        return sb.toString();
    }
    throw new IllegalArgumentException("Expected an object property and a matching object getter!!");
}
Also used : ClassRef(io.sundr.model.ClassRef) RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef) Method(io.sundr.model.Method)

Example 14 with ClassRef

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

the class ToPojo method readObjectArrayProperty.

/**
 * Returns the string representation of the code that reads an object array property.
 *
 * @param ref The reference.
 * @param source The type of the reference.
 * @param property The property to read.
 * @return The code.
 */
private static String readObjectArrayProperty(String ref, TypeDef source, Property property) {
    StringBuilder sb = new StringBuilder();
    Method getter = getterOf(source, property);
    TypeRef getterTypeRef = getter.getReturnType();
    TypeRef propertyTypeRef = property.getTypeRef();
    if (propertyTypeRef instanceof ClassRef && getterTypeRef instanceof ClassRef) {
        String nextRef = variables.pop();
        try {
            TypeDef propertyType = GetDefinition.of((ClassRef) propertyTypeRef);
            TypeDef getterType = GetDefinition.of((ClassRef) getterTypeRef);
            sb.append("Arrays.asList(").append(ref).append(".").append(getter.getName()).append("())").append(".stream().map(").append(nextRef).append(" ->").append(convertReference(nextRef, getterType, propertyType)).append(")").append(".collect(Collectors.toList()).toArray(new " + propertyType.getFullyQualifiedName() + "[0])");
        } finally {
            variables.push(nextRef);
        }
        return sb.toString();
    }
    throw new IllegalArgumentException("Expected an object property and a matching object getter!!");
}
Also used : ClassRef(io.sundr.model.ClassRef) RichTypeDef(io.sundr.model.RichTypeDef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef) Method(io.sundr.model.Method)

Example 15 with ClassRef

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

the class ToPojo method readPrimitiveValue.

/**
 * Returns the string representation of the code that reads an enum property from a reference using a getter.
 *
 * @param ref The reference.
 * @param source The type of the reference.
 * @param property The property to read.
 * @return The code.
 */
private static String readPrimitiveValue(String ref, TypeDef source, Property property) {
    String dv = getDefaultValue(property);
    String key = getterOf(source, property).getName();
    String type = property.getTypeRef().toString();
    TypeRef propertyRef = property.getTypeRef();
    ClassRef boxed = (ClassRef) TypeAs.BOXED_OF.apply(propertyRef);
    String parse = TypeAs.PARSER_OF.apply(propertyRef);
    String boxedName = boxed.getFullyQualifiedName();
    if (parse != null) {
        return indent(ref) + boxedName + "." + parse + "(String.valueOf(" + ref + " instanceof Map ? ((Map)" + ref + ").getOrDefault(\"" + key + "\",\"" + dv + "\") : \"" + dv + "\"))";
    } else {
        return indent(ref) + "(" + type + ")(" + ref + " instanceof Map ? ((Map)" + ref + ").getOrDefault(\"" + key + "\", " + dv + ") : " + dv + ")";
    }
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeRef(io.sundr.model.TypeRef)

Aggregations

ClassRef (io.sundr.model.ClassRef)68 TypeDef (io.sundr.model.TypeDef)41 TypeRef (io.sundr.model.TypeRef)35 RichTypeDef (io.sundr.model.RichTypeDef)22 ClassRefBuilder (io.sundr.model.ClassRefBuilder)21 ArrayList (java.util.ArrayList)21 Property (io.sundr.model.Property)19 Method (io.sundr.model.Method)18 TypeDefBuilder (io.sundr.model.TypeDefBuilder)18 Test (org.junit.Test)17 List (java.util.List)12 AnnotationRef (io.sundr.model.AnnotationRef)11 MethodBuilder (io.sundr.model.MethodBuilder)11 Collectors (java.util.stream.Collectors)11 TypedVisitor (io.sundr.builder.TypedVisitor)10 TypeParamDef (io.sundr.model.TypeParamDef)10 LinkedHashSet (java.util.LinkedHashSet)10 PropertyBuilder (io.sundr.model.PropertyBuilder)9 Map (java.util.Map)9 AttributeKey (io.sundr.model.AttributeKey)8