use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class TemplateTransformationProcessor method selectFromResource.
public void selectFromResource(Elements elements, Filer filer, ResourceSelector selector, Map<String, TypeDef> definitions) {
try {
FileObject fileObject = filer.getResource(StandardLocation.CLASS_PATH, "", selector.value());
try (BufferedReader reader = new BufferedReader(new InputStreamReader(fileObject.openInputStream()))) {
List<String> lines = reader.lines().map(String::trim).filter(l -> !Strings.isNullOrEmpty(l)).collect(Collectors.toList());
AptContext aptContext = AptContext.getContext();
Map<String, TypeDef> map = lines.stream().map(l -> elements.getTypeElement(l)).filter(e -> e instanceof TypeElement).map(e -> Adapters.adaptType(e, aptContext)).collect(Collectors.toMap(e -> e.getFullyQualifiedName(), e -> e));
definitions.putAll(map);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class TemplateTransformationProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
Elements elements = processingEnv.getElementUtils();
Types types = processingEnv.getTypeUtils();
Filer filer = processingEnv.getFiler();
AptContext aptContext = AptContext.create(elements, types, DefinitionRepository.getRepository());
Map<TemplateTransformation, Map<String, TypeDef>> annotatedTypes = new HashMap<>();
for (TypeElement typeElement : annotations) {
for (Element element : env.getElementsAnnotatedWith(typeElement)) {
TemplateTransformations transformations = element.getAnnotation(TemplateTransformations.class);
TemplateTransformation transformation = element.getAnnotation(TemplateTransformation.class);
List<TemplateTransformation> all = new ArrayList<>();
if (transformation != null) {
all.add(transformation);
}
if (transformations != null) {
for (TemplateTransformation t : transformations.value()) {
all.add(t);
}
}
TypeDef def = new TypeDefBuilder(Adapters.adaptType(Apt.getClassElement(element), aptContext)).build();
for (TemplateTransformation t : all) {
if (!annotatedTypes.containsKey(t)) {
annotatedTypes.put(t, new HashMap<>());
}
// If there are no selectors processes the annotated class
if (transformations == null) {
annotatedTypes.get(t).put(def.getFullyQualifiedName(), def);
} else if (transformations.annotations().length > 0) {
for (AnnotationSelector selector : transformations.annotations()) {
selectAnnotated(env, types, selector, annotatedTypes.get(t));
}
} else if (transformations.packages().length > 0) {
for (PackageSelector selector : transformations.packages()) {
selectPackages(elements, selector, annotatedTypes.get(t));
}
} else if (transformations.resources().length > 0) {
for (ResourceSelector selector : transformations.resources()) {
selectFromResource(elements, filer, selector, annotatedTypes.get(t));
}
} else {
annotatedTypes.get(t).put(def.getFullyQualifiedName(), def);
}
}
}
for (Map.Entry<TemplateTransformation, Map<String, TypeDef>> entry : annotatedTypes.entrySet()) {
TemplateTransformation transformation = entry.getKey();
Map<String, TypeDef> annotated = entry.getValue();
try {
if (transformation.gather()) {
URL templateUrl = readTemplateURL(filer, null, transformation.value());
TemplateRenderer<Map> renderer = TemplateRenderers.getTemplateRenderer(Map.class, templateUrl).orElseThrow(() -> new IllegalStateException("No template renderer found for:" + templateUrl));
CodeGenerator.newGenerator(Map.class).withRenderer(renderer).withOutput(new GenericAptOutput<Map>(filer, renderer, transformation.outputPath())).skipping(i -> false).generate(annotated);
} else {
for (TypeDef typeDef : annotated.values()) {
URL templateUrl = readTemplateURL(filer, typeDef.getPackageName(), transformation.value());
TemplateRenderer<TypeDef> renderer = TemplateRenderers.getTemplateRenderer(TypeDef.class, templateUrl).orElseThrow(() -> new IllegalStateException("No template renderer found for:" + templateUrl));
Function<TypeDef, String> identifier = t -> io.sundr.model.utils.Types.parseFullyQualifiedName(renderer.render(t));
CodeGenerator.newGenerator(TypeDef.class).withRenderer(renderer).withIdentifier(identifier).withOutput(new TypeDefAptOutput(filer, renderer)).skipping(t -> TypeLookup.lookup(identifier.apply(typeDef), AptContext.getContext()).isPresent()).generate(typeDef);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return false;
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class ClassDirective method render.
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException {
String block = "";
TypeDef clazz = null;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
if (node.jjtGetChild(i) != null) {
if (!(node.jjtGetChild(i) instanceof ASTBlock)) {
// reading and casting inline parameters
if (i == 0) {
clazz = (TypeDef) node.jjtGetChild(i).value(context);
} else {
break;
}
} else {
// reading block content and rendering it
try {
StringWriter blockContent = new StringWriter();
node.jjtGetChild(i).render(context, blockContent);
block = blockContent.toString();
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
writeClazz(writer, clazz, block);
return true;
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class StringTemplateRendererTest method testStringTemplateRenderer.
@Test
public void testStringTemplateRenderer() throws Exception {
StringTemplateRenderer<TypeDef> renderer = new StringTemplateRenderer<>("$model.fullyQualifiedName$");
TypeDef string = TypeDef.forName(String.class.getName());
String result = renderer.render(string);
assertEquals(String.class.getName(), result);
}
use of io.sundr.model.TypeDef in project sundrio by sundrio.
the class VelocityRendererTest method testVelocityRendererInline.
@Test
public void testVelocityRendererInline() throws Exception {
VelocityRenderer<TypeDef> renderer = new VelocityRenderer<>("${model.getFullyQualifiedName()}");
TypeDef string = TypeDef.forName(String.class.getName());
String result = renderer.render(string);
assertEquals(String.class.getName(), result);
}
Aggregations