use of io.sundr.model.TypeDefBuilder 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.TypeDefBuilder in project sundrio by sundrio.
the class Combine method extractParameters.
private static final Set<TypeParamDef> extractParameters(TypeDef typeDef) {
final Set<TypeParamDef> result = new LinkedHashSet<TypeParamDef>();
final Set<TypeParamRef> refs = new LinkedHashSet<TypeParamRef>();
TypeDef ignored = new TypeDefBuilder(typeDef).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.TypeDefBuilder in project sundrio by sundrio.
the class TypeDefElementVisitor method visit.
public TypeDefBuilder visit(Element e) {
if (e instanceof TypeElement) {
return new TypeDefBuilder(context.getDefinitionRepository().register(new TypeDefBuilder(typeAdapterFunction.apply((TypeElement) e)).build()));
}
String name = e.getSimpleName().toString();
builder.withName(name);
if (e.getKind() == ElementKind.INTERFACE) {
builder.withKind(Kind.INTERFACE);
} else if (e.getKind() == ElementKind.ENUM) {
builder.withKind(Kind.ENUM);
} else if (e.getKind() == ElementKind.ANNOTATION_TYPE) {
builder.withKind(Kind.ANNOTATION);
} else {
builder.withKind(Kind.CLASS);
}
if (e.getEnclosingElement() instanceof PackageElement) {
String packageName = e.getEnclosingElement().toString();
builder.withPackageName(packageName);
}
return builder;
}
use of io.sundr.model.TypeDefBuilder in project sundrio by sundrio.
the class TemplateTransformationProcessor method selectAnnotated.
public void selectAnnotated(RoundEnvironment env, Types types, AnnotationSelector selector, Map<String, TypeDef> definitions) {
for (Object o : env.getElementsAnnotatedWith((TypeElement) types.asElement(annotationMirror(selector)))) {
if (o instanceof TypeElement) {
TypeDef typeDef = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement((Element) o), AptContext.getContext())).build();
definitions.put(typeDef.getFullyQualifiedName(), typeDef);
}
}
}
use of io.sundr.model.TypeDefBuilder in project sundrio by sundrio.
the class TemplateTransformationProcessor method selectPackages.
public void selectPackages(Elements elements, PackageSelector selector, Map<String, TypeDef> definitions) {
Pattern pattern = Pattern.compile(selector.pattern());
PackageElement packageElement = elements.getPackageElement(selector.value());
List<TypeElement> typeElements = new ArrayList<>();
if (packageElement != null) {
for (Element e : packageElement.getEnclosedElements()) {
if (e instanceof TypeElement) {
typeElements.add((TypeElement) e);
}
}
} else {
TypeElement e = elements.getTypeElement(selector.value());
if (e != null) {
typeElements.add(e);
}
}
for (TypeElement typeElement : typeElements) {
TypeDef typeDef = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement(typeElement), AptContext.getContext())).build();
Matcher m = pattern.matcher(typeDef.getName());
if (m.matches()) {
definitions.put(typeDef.getFullyQualifiedName(), typeDef);
}
}
}
Aggregations