use of io.sundr.model.TypeRef in project sundrio by sundrio.
the class SimpleClassTest method testBuilder.
@Test
public void testBuilder() {
TypeDef builder = ClazzAs.BUILDER.apply(simpleClassDef);
System.out.println(builder);
assertEquals(Kind.CLASS, builder.getKind());
assertEquals("SimpleClassBuilder", 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.SimpleClass", ref.toString());
assertEquals("testpackage.SimpleClass", ref.render());
ref = argIterator.next();
assertEquals("testpackage.SimpleClassBuilder", ref.toString());
assertEquals("testpackage.SimpleClassBuilder", ref.render());
}
use of io.sundr.model.TypeRef 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.TypeRef 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);
}
use of io.sundr.model.TypeRef in project sundrio by sundrio.
the class DslProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
Elements elements = processingEnv.getElementUtils();
Types types = processingEnv.getTypeUtils();
DslContext context = DslContextManager.create(elements, types);
for (TypeElement annotation : annotations) {
for (Element element : env.getElementsAnnotatedWith(annotation)) {
if (element instanceof TypeElement) {
Generics.clear();
TypeElement typeElement = (TypeElement) element;
InterfaceName interfaceName = element.getAnnotation(InterfaceName.class);
String targetInterface = interfaceName.value();
Set<TypeDef> interfacesToGenerate = new LinkedHashSet<TypeDef>();
Collection<ExecutableElement> sorted = ElementFilter.methodsIn(typeElement.getEnclosedElements());
// 1st step generate generic interface for all types.
Set<TypeDef> genericInterfaces = executablesToInterfaces(context, sorted);
Set<TypeDef> genericAndScopeInterfaces = Nodes.TO_SCOPE.apply(genericInterfaces);
for (TypeDef clazz : genericAndScopeInterfaces) {
if (!TypeDefUtils.isEntryPoint(clazz)) {
interfacesToGenerate.add(clazz);
}
}
// 2nd step create dependency graph.
List<Method> methods = new ArrayList<Method>();
Set<Node<TypeDef>> graph = Nodes.TO_GRAPH.apply(genericAndScopeInterfaces);
for (Node<TypeDef> root : graph) {
Node<TypeDef> uncyclic = Nodes.TO_UNCYCLIC.apply(root);
Node<TypeDef> unwrapped = Nodes.TO_UNWRAPPED.apply(NodeContext.builder().withItem(uncyclic.getItem()).build());
TypeDef current = unwrapped.getItem();
// Just add the method with the direct return type.
if (unwrapped.getTransitions().isEmpty()) {
for (Method m : current.getMethods()) {
TypeRef returnType = m.getReturnType();
if (returnType instanceof ClassRef) {
TypeDef toUnwrap = GetDefinition.of((ClassRef) returnType);
methods.add(new MethodBuilder(m).withReturnType(Generics.UNWRAP.apply(toUnwrap).toInternalReference()).build());
} else if (returnType.getAttributes().containsKey(ORIGINAL_REF)) {
methods.add(new MethodBuilder(m).withReturnType((TypeRef) returnType.getAttributes().get(ORIGINAL_REF)).build());
} else {
methods.add(new MethodBuilder(m).withReturnType(returnType).build());
}
}
} else {
for (Method m : current.getMethods()) {
methods.add(new MethodBuilder(m).withReturnType(current.toUnboundedReference()).build());
}
interfacesToGenerate.add(Nodes.TO_ROOT.apply(unwrapped));
}
}
// Do generate the DSL interface
interfacesToGenerate.add(new TypeDefBuilder().withComments("Generated").withPackageName(Apt.getPackageElement(element).toString()).withName(targetInterface).withKind(Kind.INTERFACE).withModifiers(modifiersToInt(Modifier.PUBLIC)).withMethods(methods).build());
interfacesToGenerate.addAll(context.getDefinitionRepository().getDefinitions(IS_GENERATED));
for (TypeDef clazz : interfacesToGenerate) {
generate(clazz);
}
}
}
}
return true;
}
use of io.sundr.model.TypeRef in project sundrio by sundrio.
the class SimpleClassWithInheritanceTest method testBuilder.
@Test
public void testBuilder() throws FileNotFoundException {
TypeDef builder = ClazzAs.BUILDER.apply(simpleClassWithDateDef);
System.out.println(builder);
assertEquals(Kind.CLASS, builder.getKind());
assertEquals("SimpleClassWithDateBuilder", builder.getName());
assertEquals(1, builder.getExtendsList().size());
ClassRef superClass = builder.getImplementsList().iterator().next();
assertEquals(builderContext.getVisitableBuilderInterface().getName(), superClass.getName());
assertEquals(2, superClass.getArguments().size());
Iterator<TypeRef> argIterator = superClass.getArguments().iterator();
TypeRef ref = argIterator.next();
assertEquals("testpackage.SimpleClassWithDate", ref.render());
assertEquals("testpackage.SimpleClassWithDate", ref.toString());
ref = argIterator.next();
assertEquals("testpackage.SimpleClassWithDateBuilder", ref.render());
assertEquals("testpackage.SimpleClassWithDateBuilder", ref.toString());
}
Aggregations