use of io.sundr.model.ClassRefBuilder in project sundrio by sundrio.
the class SimpleClassTest method testFluentImpl.
@Test
public void testFluentImpl() {
TypeDef fluentImpl = ClazzAs.FLUENT_IMPL.apply(simpleClassDef);
System.out.println(fluentImpl);
assertEquals(Kind.CLASS, fluentImpl.getKind());
assertEquals("SimpleClassFluentImpl", fluentImpl.getName());
assertEquals(1, fluentImpl.getExtendsList().size());
assertTrue(hasMethod(fluentImpl, "addToTags", new ClassRefBuilder().withFullyQualifiedName(String.class.getName()).withDimensions(1).build()));
assertTrue(hasMethod(fluentImpl, "removeFromTags", new ClassRefBuilder().withFullyQualifiedName(String.class.getName()).withDimensions(1).build()));
ClassRef superClass = fluentImpl.getExtendsList().iterator().next();
assertEquals("BaseFluent", superClass.getName());
assertEquals(1, superClass.getArguments().size());
assertEquals("A", superClass.getArguments().iterator().next().toString());
}
use of io.sundr.model.ClassRefBuilder in project sundrio by sundrio.
the class BindDefinition method mapClassRefArguments.
/**
* Map arguments, of {@link ClassRef} instances to known {@link TypeRef} based on the specified mappings.
* Example: Given a class reference to {@code Shape<T>} and a map containing {@code T -> Integer}
* the final reference will be: {@code Shape<Integer> type}.
*
* @param mappings A map that maps class arguments names to types.
* @return a visitors that performs the actual mapping.
*/
private static TypedVisitor<ClassRefBuilder> mapClassRefArguments(Map<String, TypeRef> mappings) {
return new TypedVisitor<ClassRefBuilder>() {
@Override
public void visit(ClassRefBuilder c) {
List<TypeRef> arguments = new ArrayList<>();
for (TypeRef arg : c.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);
}
c.withArguments(arguments);
}
};
}
use of io.sundr.model.ClassRefBuilder in project sundrio by sundrio.
the class ApplyTypeParamMappingToProperty method visit.
@Override
public void visit(PropertyFluent<?> property) {
TypeRef typeRef = property.buildTypeRef();
if (typeRef instanceof TypeParamRef) {
TypeParamRef typeParamRef = (TypeParamRef) typeRef;
String key = typeParamRef.getName();
TypeRef paramRef = mappings.get(key);
if (paramRef != null) {
property.withTypeRef(paramRef);
attributeKey.ifPresent(k -> property.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());
property.withTypeRef(new ClassRefBuilder(classRef).withArguments(mappedArguments).build());
attributeKey.ifPresent(k -> property.addToAttributes(k, classRef));
}
}
}
use of io.sundr.model.ClassRefBuilder in project sundrio by sundrio.
the class ReplacePackage method visitClassRefBuilder.
private void visitClassRefBuilder(ClassRefBuilder builder) {
builder.withFullyQualifiedName(builder.getFullyQualifiedName().replace(target, replacement)).accept(visitor);
List<TypeRef> updatedArguments = new ArrayList<TypeRef>();
for (TypeRef arg : builder.getArguments()) {
if (arg instanceof ClassRef && ((ClassRef) arg).getPackageName().equals(target)) {
updatedArguments.add(new ClassRefBuilder((ClassRef) arg).accept(visitor).build());
} else {
updatedArguments.add(arg);
}
}
builder.withArguments(updatedArguments);
}
use of io.sundr.model.ClassRefBuilder in project sundrio by sundrio.
the class ReplacePackage method visitMethodBuilder.
private void visitMethodBuilder(MethodBuilder builder) {
if (builder.getReturnType() instanceof ClassRef) {
ClassRefBuilder classRefBuilder = new ClassRefBuilder((ClassRef) builder.getReturnType());
builder.withReturnType(classRefBuilder.accept(visitor, this).build());
}
}
Aggregations