Search in sources :

Example 6 with FileObject

use of javax.tools.FileObject in project camel by apache.

the class AnnotationProcessorHelper method loadResource.

public static String loadResource(ProcessingEnvironment processingEnv, String packageName, String fileName) {
    Filer filer = processingEnv.getFiler();
    FileObject resource;
    String relativeName = packageName + "/" + fileName;
    try {
        resource = filer.getResource(StandardLocation.CLASS_OUTPUT, "", relativeName);
    } catch (Throwable e) {
        return "Cannot load classpath resource: " + relativeName + " due: " + e.getMessage();
    }
    if (resource == null) {
        return null;
    }
    try {
        InputStream is = resource.openInputStream();
        return loadText(is, true);
    } catch (Exception e) {
        warning(processingEnv, "APT cannot load file: " + packageName + "/" + fileName);
    }
    return null;
}
Also used : InputStream(java.io.InputStream) FileObject(javax.tools.FileObject) Filer(javax.annotation.processing.Filer) IOException(java.io.IOException)

Example 7 with FileObject

use of javax.tools.FileObject in project qpid-broker-j by apache.

the class PluggableProcessor method loadExistingServicesFile.

private String loadExistingServicesFile(final Filer filer, String serviceName) {
    String relativeName = "META-INF/services/" + serviceName;
    try {
        FileObject existingFile = filer.getResource(StandardLocation.CLASS_OUTPUT, "", relativeName);
        BufferedReader r = new BufferedReader(new InputStreamReader(existingFile.openInputStream(), "UTF-8"));
        String line;
        while ((line = r.readLine()) != null) {
            if (!line.matches(" *#")) {
                factoryImplementations.get(serviceName).add(line);
            }
        }
        r.close();
    } catch (FileNotFoundException e) {
    // no existing file (ignore)
    } catch (IOException e) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error loading existing services file: " + relativeName + " - " + e.getLocalizedMessage());
    }
    return relativeName;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileObject(javax.tools.FileObject) IOException(java.io.IOException)

Example 8 with FileObject

use of javax.tools.FileObject 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 9 with FileObject

use of javax.tools.FileObject in project copybara by google.

the class MarkdownGenerator method processDoc.

private void processDoc(SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) throws ElementException, IOException {
    Multimap<String, String> docByElementType = ArrayListMultimap.create();
    for (Element element : elementsByAnnotation.get(SkylarkModule.class)) {
        TypeElement module = (TypeElement) element;
        StringBuilder sb = new StringBuilder();
        SkylarkModule skyModule = module.getAnnotation(SkylarkModule.class);
        if (!skyModule.documented()) {
            continue;
        }
        sb.append("## ").append(skyModule.name()).append("\n\n");
        sb.append(skyModule.doc());
        sb.append("\n\n");
        // Generate flags associated with the whole module
        sb.append(generateFlagsInfo(module));
        for (Element member : module.getEnclosedElements()) {
            sb.append(generateFunctionDocumentation(module, skyModule, member));
        }
        docByElementType.put(skyModule.name(), sb.toString());
    }
    for (String group : docByElementType.keySet()) {
        FileObject resource = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "", group + ".copybara.md");
        try (Writer writer = resource.openWriter()) {
            for (String groupValues : docByElementType.get(group)) {
                writer.append(groupValues).append("\n");
            }
        }
    }
}
Also used : SkylarkModule(com.google.devtools.build.lib.skylarkinterface.SkylarkModule) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) DocElement(com.google.copybara.doc.annotations.DocElement) Element(javax.lang.model.element.Element) FileObject(javax.tools.FileObject) Writer(java.io.Writer)

Example 10 with FileObject

use of javax.tools.FileObject in project graal by oracle.

the class InstrumentRegistrationProcessor method loadExistingTypes.

static void loadExistingTypes(ProcessingEnvironment env, List<TypeElement> instruments, String filename, String pre) {
    Set<String> typeNames = new HashSet<>();
    for (TypeElement type : instruments) {
        typeNames.add(ElementUtils.getQualifiedName(type));
    }
    Properties current = new Properties();
    try {
        FileObject object = env.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", filename);
        current.load(object.openInputStream());
    } catch (IOException e1) {
        env.getMessager().printMessage(Kind.NOTE, filename + e1.getMessage(), null);
    // does not exist yet.
    // better way to detect this?
    }
    for (int cnt = 1; ; cnt++) {
        String prefix = pre + cnt + ".";
        String className = current.getProperty(prefix + "className");
        if (className == null) {
            break;
        }
        env.getMessager().printMessage(Kind.NOTE, filename + className, null);
        TypeElement foundType = env.getElementUtils().getTypeElement(className);
        if (foundType != null && !typeNames.contains(ElementUtils.getQualifiedName(foundType))) {
            instruments.add(foundType);
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) FileObject(javax.tools.FileObject) IOException(java.io.IOException) Properties(java.util.Properties) SortedProperties(com.oracle.truffle.dsl.processor.LanguageRegistrationProcessor.SortedProperties) HashSet(java.util.HashSet)

Aggregations

FileObject (javax.tools.FileObject)91 IOException (java.io.IOException)57 TypeElement (javax.lang.model.element.TypeElement)19 File (java.io.File)18 PrintWriter (java.io.PrintWriter)16 Element (javax.lang.model.element.Element)14 Writer (java.io.Writer)13 Filer (javax.annotation.processing.Filer)13 BufferedWriter (java.io.BufferedWriter)12 ArrayList (java.util.ArrayList)12 OutputStream (java.io.OutputStream)11 JavaFileObject (javax.tools.JavaFileObject)11 OutputStreamWriter (java.io.OutputStreamWriter)10 Properties (java.util.Properties)10 InputStream (java.io.InputStream)8 URI (java.net.URI)8 FilerException (javax.annotation.processing.FilerException)7 MainInfo (com.predic8.membrane.annot.model.MainInfo)6 BufferedReader (java.io.BufferedReader)6 FileWriter (java.io.FileWriter)6