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 "";
}
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;
}
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!!");
}
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!!");
}
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 + ")";
}
}
Aggregations