Search in sources :

Example 1 with Lang

use of io.vertx.codetrans.Lang 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)

Aggregations

Lang (io.vertx.codetrans.Lang)1 CodeTranslate (io.vertx.codetrans.annotations.CodeTranslate)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 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