use of io.sundr.model.TypeRef in project sundrio by sundrio.
the class SimpleClassWithParameterTest method testBuilder.
@Test
public void testBuilder() {
TypeDef builder = ClazzAs.BUILDER.apply(simpleClassWithParameterDef);
System.out.println(builder);
assertEquals(Kind.CLASS, builder.getKind());
assertEquals("SimpleClassWithParameterBuilder", builder.getName());
assertEquals(1, builder.getExtendsList().size());
ClassRef superClass = builder.getImplementsList().iterator().next();
assertEquals("VisitableBuilder", superClass.getName());
assertEquals(2, superClass.getArguments().size());
Iterator<TypeRef> argIterator = superClass.getArguments().iterator();
TypeRef ref = argIterator.next();
assertEquals("testpackage.SimpleClassWithParameter<N>", ref.render());
assertEquals("testpackage.SimpleClassWithParameter<N>", ref.toString());
ref = argIterator.next();
assertEquals("testpackage.SimpleClassWithParameterBuilder<N>", ref.render());
assertEquals("testpackage.SimpleClassWithParameterBuilder<N>", ref.toString());
}
use of io.sundr.model.TypeRef in project sundrio by sundrio.
the class BindDefinition method mapGenericProperties.
/**
* Map generic properties to known {@link TypeRef} based on the specified mappings.
* Example: Given a property {@code T size} and a map containing {@code T -> Integer} the final
* property will be: {@code Integer type}.
*
* @param mappings A map that maps class arguments names to types.
* @return a visitors that performs the actual mapping.
*/
private static TypedVisitor<PropertyBuilder> mapGenericProperties(Map<String, TypeRef> mappings) {
return new TypedVisitor<PropertyBuilder>() {
@Override
public void visit(PropertyBuilder 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);
}
}
}
};
}
use of io.sundr.model.TypeRef 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.TypeRef in project sundrio by sundrio.
the class BindDefinition method apply.
@Override
public TypeDef apply(ClassRef t) {
List<TypeRef> arguments = t.getArguments();
TypeDef definition = GetDefinition.of(t);
if (arguments.isEmpty()) {
return definition;
}
List<TypeParamDef> parameters = definition.getParameters();
List<TypeParamDef> newParameters = definition.getParameters();
Map<String, TypeRef> mappings = new HashMap<>();
for (int i = 0; i < arguments.size(); i++) {
String name = parameters.get(i).getName();
TypeRef typeRef = arguments.get(i);
mappings.put(name, typeRef);
newParameters.remove(i);
}
return new TypeDefBuilder(definition).withParameters(newParameters).accept(mapClassRefArguments(mappings), mapGenericProperties(mappings), mapGenericReturnTypes(mappings)).build();
}
use of io.sundr.model.TypeRef in project sundrio by sundrio.
the class BindDefinition method mapGenericReturnTypes.
/**
* Map generic properties to known {@link TypeRef} based on the specified mappings.
* Example: Given a property {@code T size} and a map containing {@code T -> Integer} the final
* property will be: {@code Integer type}.
*
* @param mappings A map that maps class arguments names to types.
* @return a visitors that performs the actual mapping.
*/
private static TypedVisitor<MethodBuilder> mapGenericReturnTypes(Map<String, TypeRef> mappings) {
return new TypedVisitor<MethodBuilder>() {
@Override
public void visit(MethodBuilder 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);
}
}
}
};
}
Aggregations