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