Search in sources :

Example 11 with TypeRef

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

the class BuilderUtils method fullyQualifiedNameDiff.

public static String fullyQualifiedNameDiff(TypeRef typeRef, TypeDef originType) {
    Map<String, String> map = DefinitionRepository.getRepository().getReferenceMap();
    String currentPackage = originType != null ? originType.getPackageName() : null;
    if (typeRef instanceof ClassRef) {
        TypeRef unwrapped = TypeAs.combine(UNWRAP_COLLECTION_OF, UNWRAP_ARRAY_OF, UNWRAP_OPTIONAL_OF, UNWRAP_MAP_VALUE_OF).apply(typeRef);
        if (unwrapped instanceof ClassRef) {
            ClassRef classRef = (ClassRef) unwrapped;
            String candidateFqn = classRef.getFullyQualifiedName().replace(classRef.getPackageName(), currentPackage);
            // If classRef is inside the current package.
            if (candidateFqn.equals(classRef.getFullyQualifiedName())) {
                return "";
            }
            // If candidate is imported and different that the actual name, do a diff
            if (originType.getImports().contains(candidateFqn) && !classRef.getFullyQualifiedName().equals(candidateFqn)) {
                return capitalizeFirst(Types.fullyQualifiedNameDiff(candidateFqn, classRef.getFullyQualifiedName()));
            }
            // If not then we compare against what has been found in the map.
            String fqcn = map.get(classRef.getName());
            TypeDef mainDef = fqcn != null ? DefinitionRepository.getRepository().getDefinition(fqcn) : null;
            boolean mainBuildable = mainDef != null ? isBuildable(mainDef) : false;
            if (fqcn == null) {
                System.out.println("Warning: Expected to find class with name:" + classRef.getName());
            } else if (!classRef.getFullyQualifiedName().equals(fqcn) && mainBuildable) {
                return capitalizeFirst(Types.fullyQualifiedNameDiff(fqcn, classRef.getFullyQualifiedName()));
            }
        }
    }
    return "";
}
Also used : ClassRef(io.sundr.model.ClassRef) TypeDef(io.sundr.model.TypeDef) TypeRef(io.sundr.model.TypeRef)

Example 12 with TypeRef

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

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

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

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

TypeRef (io.sundr.model.TypeRef)45 ClassRef (io.sundr.model.ClassRef)33 TypeDef (io.sundr.model.TypeDef)23 ArrayList (java.util.ArrayList)16 ClassRefBuilder (io.sundr.model.ClassRefBuilder)14 Method (io.sundr.model.Method)13 RichTypeDef (io.sundr.model.RichTypeDef)13 TypeDefBuilder (io.sundr.model.TypeDefBuilder)12 Property (io.sundr.model.Property)10 MethodBuilder (io.sundr.model.MethodBuilder)9 PropertyBuilder (io.sundr.model.PropertyBuilder)9 TypeParamRef (io.sundr.model.TypeParamRef)9 TypedVisitor (io.sundr.builder.TypedVisitor)8 AnnotationRef (io.sundr.model.AnnotationRef)8 List (java.util.List)8 TypeParamDef (io.sundr.model.TypeParamDef)7 HashMap (java.util.HashMap)7 AttributeKey (io.sundr.model.AttributeKey)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6