use of io.vertx.codetrans.annotations.CodeTranslate in project vertx-examples by vert-x3.
the class CodeTransProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
if (log != null) {
log.close();
}
return false;
}
if (outputDir != null && (outputDir.exists() || outputDir.mkdirs())) {
List<ExecutableElement> methodElts = new ArrayList<>();
try {
PrintWriter log = getLogger();
// Process all verticles automatically
TypeMirror verticleType = processingEnv.getElementUtils().getTypeElement(Verticle.class.getName()).asType();
for (Element rootElt : roundEnv.getRootElements()) {
Set<Modifier> modifiers = rootElt.getModifiers();
if (rootElt.getKind() == ElementKind.CLASS && !modifiers.contains(Modifier.ABSTRACT) && modifiers.contains(Modifier.PUBLIC) && processingEnv.getTypeUtils().isSubtype(rootElt.asType(), verticleType)) {
TypeElement typeElt = (TypeElement) rootElt;
for (Element enclosedElt : typeElt.getEnclosedElements()) {
if (enclosedElt.getKind() == ElementKind.METHOD) {
ExecutableElement methodElt = (ExecutableElement) enclosedElt;
if (methodElt.getSimpleName().toString().equals("start") && methodElt.getParameters().isEmpty()) {
methodElts.add(methodElt);
}
}
}
}
}
// Process CodeTranslate annotations
roundEnv.getElementsAnnotatedWith(CodeTranslate.class).forEach(annotatedElt -> {
methodElts.add((ExecutableElement) annotatedElt);
});
// Generate
for (ExecutableElement methodElt : methodElts) {
TypeElement typeElt = (TypeElement) methodElt.getEnclosingElement();
FileObject obj = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", typeElt.getQualifiedName().toString().replace('.', '/') + ".java");
File srcFolder = new File(obj.toUri()).getParentFile();
String filename = Case.SNAKE.format(Case.CAMEL.parse(typeElt.getSimpleName().toString()));
for (Lang lang : langs) {
if (isSkipped(typeElt, lang)) {
log.write("Skipping " + lang.getExtension() + " translation for " + typeElt.getQualifiedName() + "#" + methodElt.getSimpleName());
continue;
}
String folderPath = processingEnv.getElementUtils().getPackageOf(typeElt).getQualifiedName().toString().replace('.', '/');
File dstFolder = new File(new File(outputDir, lang.getExtension()), folderPath);
if (dstFolder.exists() || dstFolder.mkdirs()) {
try {
String translation = translator.translate(methodElt, lang);
File f = new File(dstFolder, filename + "." + lang.getExtension());
Files.write(f.toPath(), translation.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
log.println("Generated " + f.getAbsolutePath());
copyDirRec(srcFolder, dstFolder, log);
} catch (Exception e) {
log.println("Skipping generation of " + typeElt.getQualifiedName());
e.printStackTrace(log);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
} else {
return false;
}
}
use of io.vertx.codetrans.annotations.CodeTranslate in project vertx-examples by vert-x3.
the class VertxUnitTest method run.
// Not yet detected
@CodeTranslate
public void run() {
TestOptions options = new TestOptions().addReporter(new ReportOptions().setTo("console"));
TestSuite suite = TestSuite.create("io.vertx.example.unit.test.VertxUnitTest");
suite.before(context -> {
vertx = Vertx.vertx();
vertx.createHttpServer().requestHandler(req -> req.response().end("foo")).listen(8080, context.asyncAssertSuccess());
});
suite.after(context -> {
vertx.close(context.asyncAssertSuccess());
});
// Specifying the test names seems ugly...
suite.test("some_test1", context -> {
// Send a request and get a response
HttpClient client = vertx.createHttpClient();
Async async = context.async();
client.getNow(8080, "localhost", "/", resp -> {
resp.bodyHandler(body -> context.assertEquals("foo", body.toString("UTF-8")));
client.close();
async.complete();
});
});
suite.test("some_test2", context -> {
// Deploy and undeploy a verticle
vertx.deployVerticle("io.vertx.example.unit.SomeVerticle", context.asyncAssertSuccess(deploymentID -> {
vertx.undeploy(deploymentID, context.asyncAssertSuccess());
}));
});
suite.run(options);
}
Aggregations