use of io.sundr.model.ClassRef 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;
}
use of io.sundr.model.ClassRef 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();
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class Combine method extractParameters.
private static final Set<TypeParamDef> extractParameters(ClassRef classRef) {
final Set<TypeParamDef> result = new LinkedHashSet<TypeParamDef>();
final Set<TypeParamRef> refs = new LinkedHashSet<TypeParamRef>();
ClassRef ignored = new ClassRefBuilder(classRef).accept(new TypeParamDefColletor(result)).accept(new TypeParamRefColletor(refs)).build();
for (TypeParamRef typeParamRef : refs) {
result.add(new TypeParamDefBuilder().withName(typeParamRef.getName()).withAttributes(typeParamRef.getAttributes()).build());
}
return result;
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class Combine method canBeExcluded.
private static boolean canBeExcluded(ClassRef candidate, Iterable<ClassRef> provided) {
Set<ClassRef> allOther = new LinkedHashSet<ClassRef>();
for (ClassRef c : provided) {
if (!c.equals(candidate)) {
allOther.add(c);
}
}
Set<ClassRef> allProvided = TypeDefUtils.extractInterfacesFromClassRefs(allOther);
for (ClassRef type : TypeDefUtils.extractInterfacesFromClassRef(candidate)) {
if (!allProvided.contains(type)) {
return false;
}
}
return true;
}
use of io.sundr.model.ClassRef in project sundrio by sundrio.
the class Combine method canBeExcluded.
private static boolean canBeExcluded(TypeDef candidate, Iterable<TypeDef> provided) {
Set<TypeDef> allOther = new LinkedHashSet<TypeDef>();
for (TypeDef c : provided) {
if (!c.equals(candidate)) {
allOther.add(c);
}
}
Set<ClassRef> allProvided = TypeDefUtils.extractInterfacesFromTypes(allOther);
for (ClassRef type : TypeDefUtils.extractInterfacesFromType(candidate)) {
if (!allProvided.contains(type)) {
return false;
}
}
return true;
}
Aggregations