use of io.sundr.model.TypeParamRef 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.TypeParamRef in project sundrio by sundrio.
the class ClazzAs method toInstanceConstructorBody.
private static List<Statement> toInstanceConstructorBody(TypeDef clazz, TypeDef instance, String fluent) {
Method constructor = findBuildableConstructor(clazz);
List<Statement> statements = new ArrayList<Statement>();
final String ref = fluent != null && !fluent.isEmpty() ? fluent : "this";
// We may use a reference to fluent or we may use directly "this". So we need to check.
if (fluent != null && !fluent.isEmpty()) {
statements.add(new StringStatement("this.fluent = " + fluent + "; "));
}
for (Property property : constructor.getArguments()) {
Optional<Method> getter = Getter.findOptional(instance, property);
getter.ifPresent(g -> {
String cast = property.getTypeRef() instanceof TypeParamRef ? "(" + property.getTypeRef().toString() + ")" : "";
statements.add(new StringStatement(new StringBuilder().append(ref).append(".with").append(property.getNameCapitalized()).append("(").append(cast).append("instance.").append(g.getName()).append("()); ").toString()));
});
// } else {
// throw new IllegalStateException("Could not find getter for property:" + property + " in class:" + clazz);
// }
}
TypeDef target = clazz;
// Iterate parent objects and check for properties with setters but not ctor arguments.
while (target != null && !Types.OBJECT.equals(target) && BuilderUtils.isBuildable(target)) {
for (Property property : target.getProperties()) {
if (!hasBuildableConstructorWithArgument(target, property) && Setter.has(target, property)) {
Getter.findOptional(instance, property).map(Method::getName).ifPresent(getterName -> {
String withName = "with" + property.getNameCapitalized();
statements.add(new StringStatement(new StringBuilder().append(ref).append(".").append(withName).append("(instance.").append(getterName).append("());\n").toString()));
});
}
}
if (!target.getExtendsList().isEmpty()) {
target = BuilderContextManager.getContext().getBuildableRepository().getBuildable(target.getExtendsList().iterator().next());
} else {
return statements;
}
}
return statements;
}
use of io.sundr.model.TypeParamRef in project sundrio by sundrio.
the class TypeToTypeRef method apply.
@Override
public TypeRef apply(Type type) {
if (type instanceof VoidType) {
return new VoidRef();
} else if (type instanceof WildcardType) {
return new WildcardRef();
} else if (type instanceof ReferenceType) {
ReferenceType referenceType = (ReferenceType) type;
int dimensions = referenceType.getArrayCount();
TypeRef typeRef = apply(referenceType.getType());
if (dimensions == 0) {
return typeRef;
} else if (typeRef instanceof ClassRef) {
return new ClassRefBuilder((ClassRef) typeRef).withDimensions(dimensions).build();
} else if (typeRef instanceof PrimitiveRef) {
return new PrimitiveRefBuilder((PrimitiveRef) typeRef).withDimensions(dimensions).build();
} else if (typeRef instanceof TypeParamRef) {
return new TypeParamRefBuilder((TypeParamRef) typeRef).withDimensions(dimensions).build();
}
} else if (type instanceof PrimitiveType) {
PrimitiveType primitiveType = (PrimitiveType) type;
return new PrimitiveRefBuilder().withName(primitiveType.getType().name()).build();
} else if (type instanceof ClassOrInterfaceType) {
return classOrInterfaceToTypeRef.apply((ClassOrInterfaceType) type);
}
throw new IllegalArgumentException("Can't handle type:[" + type + "].");
}
use of io.sundr.model.TypeParamRef in project sundrio by sundrio.
the class ApplyTypeParamMappingToMethod method visit.
@Override
public void visit(MethodFluent<?> method) {
TypeRef typeRef = method.buildReturnType();
if (typeRef instanceof TypeParamRef) {
TypeParamRef typeParamRef = (TypeParamRef) typeRef;
String key = typeParamRef.getName();
TypeRef paramRef = mappings.get(key);
if (paramRef != null) {
method.withReturnType(paramRef);
attributeKey.ifPresent(k -> method.addToAttributes(k, typeParamRef));
}
} else if (typeRef instanceof ClassRef) {
ClassRef classRef = (ClassRef) typeRef;
if (classRef.getArguments().stream().anyMatch(a -> a instanceof TypeParamRef)) {
List<TypeRef> mappedArguments = classRef.getArguments().stream().map(a -> a instanceof TypeParamRef ? mappings.getOrDefault(((TypeParamRef) a).getName(), a) : a).collect(Collectors.toList());
method.withReturnType(new ClassRefBuilder(classRef).withArguments(mappedArguments).build());
attributeKey.ifPresent(k -> method.addToAttributes(k, classRef));
}
}
}
use of io.sundr.model.TypeParamRef in project sundrio by sundrio.
the class ApplyTypeParamMappingToTypeArguments method visit.
@Override
public void visit(ClassRefFluent<?> classRef) {
List<TypeRef> arguments = new ArrayList<>();
for (TypeRef arg : classRef.buildArguments()) {
TypeRef mappedRef = arg;
if (arg instanceof TypeParamRef) {
TypeParamRef typeParamRef = (TypeParamRef) arg;
TypeRef mapping = mappings.get(typeParamRef.getName());
if (mapping != null) {
mappedRef = mapping;
}
}
arguments.add(mappedRef);
}
classRef.withArguments(arguments);
}
Aggregations