Search in sources :

Example 1 with CodeTranslate

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;
    }
}
Also used : JavaScriptLang(io.vertx.codetrans.lang.js.JavaScriptLang) GroovyLang(io.vertx.codetrans.lang.groovy.GroovyLang) Lang(io.vertx.codetrans.Lang) RubyLang(io.vertx.codetrans.lang.ruby.RubyLang) IOException(java.io.IOException) CodeTranslate(io.vertx.codetrans.annotations.CodeTranslate) TypeMirror(javax.lang.model.type.TypeMirror) FileObject(javax.tools.FileObject) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 2 with CodeTranslate

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);
}
Also used : CodeTranslate(io.vertx.codetrans.annotations.CodeTranslate) TestSuite(io.vertx.ext.unit.TestSuite) Async(io.vertx.ext.unit.Async) TestOptions(io.vertx.ext.unit.TestOptions) HttpServer(io.vertx.core.http.HttpServer) Vertx(io.vertx.core.Vertx) ReportOptions(io.vertx.ext.unit.report.ReportOptions) HttpClient(io.vertx.core.http.HttpClient) TestSuite(io.vertx.ext.unit.TestSuite) TestOptions(io.vertx.ext.unit.TestOptions) Async(io.vertx.ext.unit.Async) HttpClient(io.vertx.core.http.HttpClient) ReportOptions(io.vertx.ext.unit.report.ReportOptions) CodeTranslate(io.vertx.codetrans.annotations.CodeTranslate)

Aggregations

CodeTranslate (io.vertx.codetrans.annotations.CodeTranslate)2 Lang (io.vertx.codetrans.Lang)1 GroovyLang (io.vertx.codetrans.lang.groovy.GroovyLang)1 JavaScriptLang (io.vertx.codetrans.lang.js.JavaScriptLang)1 RubyLang (io.vertx.codetrans.lang.ruby.RubyLang)1 Vertx (io.vertx.core.Vertx)1 HttpClient (io.vertx.core.http.HttpClient)1 HttpServer (io.vertx.core.http.HttpServer)1 Async (io.vertx.ext.unit.Async)1 TestOptions (io.vertx.ext.unit.TestOptions)1 TestSuite (io.vertx.ext.unit.TestSuite)1 ReportOptions (io.vertx.ext.unit.report.ReportOptions)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 TypeMirror (javax.lang.model.type.TypeMirror)1 FileObject (javax.tools.FileObject)1