use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class AbstractBuilderProcessor method generatePojos.
/**
* Returns true if pojos where generated.
*
* @param builderContext The builder context.
* @param buildables The set of buildables.
*/
public void generatePojos(BuilderContext builderContext, Set<TypeDef> buildables) {
Set<TypeDef> additonalBuildables = new HashSet<>();
Set<TypeDef> additionalTypes = new HashSet<>();
for (TypeDef typeDef : buildables) {
RichTypeDef richTypeDef = TypeArguments.apply(typeDef);
if (typeDef.isInterface() || typeDef.isAnnotation()) {
typeDef = ClazzAs.POJO.apply(richTypeDef);
builderContext.getDefinitionRepository().register(typeDef);
builderContext.getBuildableRepository().register(typeDef);
generate(typeDef);
additonalBuildables.add(typeDef);
if (typeDef.hasAttribute(ADDITIONAL_BUILDABLES)) {
for (TypeDef also : typeDef.getAttribute(ADDITIONAL_BUILDABLES)) {
builderContext.getDefinitionRepository().register(also);
builderContext.getBuildableRepository().register(also);
generate(also);
additonalBuildables.add(also);
}
}
if (typeDef.hasAttribute(ADDITIONAL_TYPES)) {
for (TypeDef also : typeDef.getAttribute(ADDITIONAL_TYPES)) {
builderContext.getDefinitionRepository().register(also);
generate(also);
additionalTypes.add(also);
}
}
}
}
generateBuildables(builderContext, additonalBuildables);
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class BuilderUtils method isBuildable.
/**
* Checks if {@link ClassRef} is buildable.
*
* @param ref The reference.
* @return True if buildable repository contains the ref or builder for the reference is present.
*/
public static boolean isBuildable(ClassRef ref) {
if (BuilderContextManager.getContext().getBuildableRepository().isBuildable(ref)) {
return true;
}
String builderFQCN = ref.getFullyQualifiedName() + "Builder";
TypeDef builder = BuilderContextManager.getContext().getDefinitionRepository().getDefinition(builderFQCN);
if (builder == null) {
return false;
}
return builder.getMethods().stream().filter(m -> "build".equals(m.getName())).filter(m -> m.getReturnType() instanceof ClassRef).map(m -> (ClassRef) m.getReturnType()).filter(r -> Assignable.isAssignable(r).from(ref)).count() > 0;
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class BuilderUtils method findBuildableReferences.
/**
* Returns all references of a {@link ClassRef} that are considered buildable.
*
* @param ref The reference.
* @return The list with all detected references.
*/
public static List<ClassRef> findBuildableReferences(ClassRef ref) {
List<ClassRef> result = new ArrayList<>();
TypeDef def = new TypeDefBuilder(GetDefinition.of(ref)).accept(new TypedVisitor<ClassRefBuilder>() {
@Override
public void visit(ClassRefBuilder builder) {
ClassRef candidate = builder.build();
if (isBuildable(candidate)) {
result.add(candidate);
}
}
}).build();
return result;
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class BuilderUtils method buildableField.
public static Property buildableField(Property property) {
TypeRef typeRef = property.getTypeRef();
ClassRef targetType = (ClassRef) TypeAs.combine(UNWRAP_COLLECTION_OF, UNWRAP_ARRAY_OF, UNWRAP_OPTIONAL_OF).apply(typeRef);
boolean isArray = Types.isArray(typeRef);
boolean isSet = Types.isSet(typeRef);
boolean isAbstractSet = isSet && Types.isAbstract(typeRef);
boolean isList = Types.isList(typeRef);
boolean isAbstractList = isList && Types.isAbstract(typeRef);
boolean isMap = Types.isMap(typeRef);
boolean isOptional = Types.isOptional(typeRef);
boolean isOptionalDouble = Types.isOptionalDouble(typeRef);
boolean isOptionalLong = Types.isOptionalLong(typeRef);
// For fields that are concrete we can possibly create an instance of a VisitableBuilder.
// For everything else we can have a builder e.g. Builder<Foo> = () -> fooInstance but it won't be visitable
ClassRef builderType = Types.isConcrete(targetType) ? TypeAs.BUILDER.apply(GetDefinition.of(targetType)).toInternalReference() : TypeAs.VISITABLE_BUILDER.apply(targetType);
if (isArray || isList) {
ClassRef listRef = isArray || isAbstractList ? Collections.ARRAY_LIST.toReference(builderType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(builderType).withDimensions(0).build();
TypeDef listDef = new TypeDefBuilder(TypeDef.forName(listRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.LIST.toReference(builderType)).withName("l").endArgument().endConstructor().build();
return new PropertyBuilder(property).withTypeRef(listRef).addToAttributes(LAZY_INIT, " new " + listRef + "()").addToAttributes(INIT_FUNCTION, new Construct(listDef, targetType)).addToAttributes(ALSO_IMPORT, alsoImport(property, listRef, builderType)).build();
}
if (isSet) {
ClassRef setRef = isAbstractSet ? Collections.LINKED_HASH_SET.toReference(builderType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(builderType).build();
TypeDef setDef = new TypeDefBuilder(TypeDef.forName(setRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.SET.toReference(builderType)).withName("s").endArgument().endConstructor().build();
return new PropertyBuilder(property).withTypeRef(setRef).addToAttributes(LAZY_INIT, " new " + setRef + "()").addToAttributes(INIT_FUNCTION, new Construct(setDef, targetType)).addToAttributes(ALSO_IMPORT, alsoImport(property, setRef, builderType)).build();
}
if (isOptionalLong) {
ClassRef optionalRef = Optionals.OPTIONAL_LONG.toReference(builderType);
return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " OptionalLong.empty()").build();
}
if (isOptionalDouble) {
ClassRef optionalRef = Optionals.OPTIONAL_DOUBLE.toReference(builderType);
return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " OptionalDouble.empty()").build();
}
if (Types.isOptionalInt(targetType)) {
ClassRef optionalRef = Optionals.OPTIONAL_INT.toReference(builderType);
return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " OptionalInt.empty()").build();
}
if (isOptional) {
ClassRef optionalRef = Optionals.OPTIONAL.toReference(builderType);
return new PropertyBuilder(property).withTypeRef(optionalRef).addToAttributes(INIT, " Optional.empty()").addToAttributes(ALSO_IMPORT, alsoImport(property, optionalRef, builderType)).build();
}
if (Types.isConcrete(builderType) && BuilderUtils.hasDefaultConstructor(builderType) && property.hasAttribute(DEFAULT_VALUE)) {
return new PropertyBuilder(property).withTypeRef(builderType).addToAttributes(ALSO_IMPORT, alsoImport(property, builderType)).addToAttributes(INIT, "new " + builderType + "()").build();
}
return new PropertyBuilder(property).withTypeRef(builderType).addToAttributes(ALSO_IMPORT, alsoImport(property, builderType)).removeFromAttributes(INIT).build();
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class InitEnricher method visit.
@Override
public void visit(PropertyBuilder builder) {
TypeRef typeRef = builder.buildTypeRef();
TypeRef unwrapped = TypeAs.combine(TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(typeRef);
boolean isBuildable = isBuildable(unwrapped);
boolean hasDescendants = false;
if (!(typeRef instanceof ClassRef)) {
return;
}
Property parent = (Property) builder.getAttributes().get(DESCENDANT_OF);
if (parent != null) {
typeRef = parent.getTypeRef();
unwrapped = TypeAs.combine(TypeAs.UNWRAP_ARRAY_OF, TypeAs.UNWRAP_COLLECTION_OF, TypeAs.UNWRAP_OPTIONAL_OF).apply(typeRef);
} else if (builder.getAttributes().containsKey(DESCENDANTS) && !((Collection) builder.getAttributes().get(DESCENDANTS)).isEmpty()) {
hasDescendants = true;
}
List<TypeRef> arguments = ((ClassRef) typeRef).getArguments();
TypeRef targetType = unwrapped;
if (isBuildable || hasDescendants) {
ClassRef unwarppedClassRef = (unwrapped instanceof ClassRef) ? (ClassRef) unwrapped : null;
targetType = isAbstract(unwarppedClassRef) || GetDefinition.of(unwarppedClassRef).getKind() == Kind.INTERFACE ? TypeAs.VISITABLE_BUILDER.apply(unwarppedClassRef) : TypeAs.BUILDER.apply(GetDefinition.of(unwarppedClassRef)).toInternalReference();
}
boolean isArray = Types.isArray(typeRef);
boolean isSet = Types.isSet(typeRef);
boolean isAbstractSet = isSet && Types.isAbstract(typeRef);
boolean isList = Types.isList(typeRef);
boolean isAbstractList = isList && Types.isAbstract(typeRef);
boolean isMap = Types.isMap(typeRef);
boolean isAbstractMap = isMap && Types.isAbstract(typeRef);
boolean isOptional = Types.isOptional(typeRef);
boolean isOptionalInt = Types.isOptionalInt(typeRef);
boolean isOptionalDouble = Types.isOptionalDouble(typeRef);
boolean isOptionalLong = Types.isOptionalLong(typeRef);
if (isArray || isList) {
ClassRef listRef = isArray || isAbstractList ? Collections.ARRAY_LIST.toReference(targetType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(targetType).withDimensions(0).build();
TypeDef listDef = new TypeDefBuilder(TypeDef.forName(listRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.LIST.toReference(targetType)).withName("l").endArgument().endConstructor().build();
builder.addToAttributes(LAZY_INIT, "new " + listRef + "()").addToAttributes(INIT, builder.getAttributes().containsKey(LAZY_COLLECTIONS_INIT_ENABLED) && (Boolean) builder.getAttributes().get(LAZY_COLLECTIONS_INIT_ENABLED) ? null : builder.getAttributes().get(LAZY_INIT)).addToAttributes(INIT_FUNCTION, new Construct(listDef, targetType)).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, listRef));
} else if (isSet) {
ClassRef setRef = isAbstractSet ? Collections.LINKED_HASH_SET.toReference(targetType) : new ClassRefBuilder((ClassRef) typeRef).withArguments(targetType).build();
TypeDef setDef = new TypeDefBuilder(TypeDef.forName(setRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.SET.toReference(targetType)).withName("s").endArgument().endConstructor().build();
builder.addToAttributes(LAZY_INIT, "new " + setRef + "()").addToAttributes(INIT, builder.getAttributes().containsKey(LAZY_COLLECTIONS_INIT_ENABLED) && (Boolean) builder.getAttributes().get(LAZY_COLLECTIONS_INIT_ENABLED) ? null : builder.getAttributes().get(LAZY_INIT)).addToAttributes(INIT_FUNCTION, new Construct(setDef, unwrapped)).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, setRef));
} else if (isMap) {
ClassRef mapRef = isAbstractMap ? Collections.LINKED_HASH_MAP.toReference(arguments) : new ClassRefBuilder((ClassRef) typeRef).withArguments(arguments).build();
TypeDef mapDef = new TypeDefBuilder(TypeDef.forName(mapRef.getFullyQualifiedName())).addNewConstructor().endConstructor().addNewConstructor().addNewArgument().withTypeRef(Collections.MAP.toReference(arguments)).withName("m").endArgument().endConstructor().build();
builder.addToAttributes(LAZY_INIT, "new " + mapRef + "()").addToAttributes(INIT, builder.getAttributes().containsKey(LAZY_MAP_INIT_ENABLED) && (Boolean) builder.getAttributes().get(LAZY_MAP_INIT_ENABLED) ? null : builder.getAttributes().get(LAZY_INIT)).addToAttributes(INIT_FUNCTION, new Construct(mapDef, arguments)).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, mapRef));
} else if (isOptional) {
final ClassRef ref = new ClassRefBuilder(Optionals.OPTIONAL.toReference()).withArguments(java.util.Collections.EMPTY_LIST).build();
builder.addToAttributes(INIT, "Optional.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
} else if (isOptionalDouble) {
final ClassRef ref = Optionals.OPTIONAL_DOUBLE.toReference();
builder.addToAttributes(INIT, "OptionalDouble.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL_DOUBLE, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
} else if (isOptionalInt) {
final ClassRef ref = Optionals.OPTIONAL_INT.toReference();
builder.addToAttributes(INIT, "OptionalInt.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL_INT, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
} else if (isOptionalLong) {
final ClassRef ref = Optionals.OPTIONAL_LONG.toReference();
builder.addToAttributes(INIT, "OptionalLong.empty()").addToAttributes(INIT_FUNCTION, new Construct(Optionals.OPTIONAL_LONG, targetType, "of")).addToAttributes(ALSO_IMPORT, Arrays.asList(targetType, ref));
}
}
Aggregations