Example usage for javax.tools FileObject openReader

List of usage examples for javax.tools FileObject openReader

Introduction

In this page you can find the example usage for javax.tools FileObject openReader.

Prototype

Reader openReader(boolean ignoreEncodingErrors) throws IOException;

Source Link

Document

Returns a reader for this object.

Usage

From source file:io.fabric8.kubernetes.generator.processor.AbstractKubernetesAnnotationProcessor.java

KubernetesResource readJson(String fileName) {
    try {/*w w  w.  j  av a2  s . c  om*/
        FileObject fileObject = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "",
                (fileName == null ? KUBERNETES_JSON : fileName));
        try (Reader reader = fileObject.openReader(false)) {
            return MAPPER.readValue(reader, KubernetesResource.class);
        }
    } catch (IOException e) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, fileName + " JSON not found.");
    }
    return null;
}

From source file:org.callimachusproject.auth.DigestPasswordAccessor.java

private String readString(FileObject file) {
    try {/*from  www.  j  a v  a  2  s.  co m*/
        Reader reader = file.openReader(true);
        if (reader == null)
            return null;
        try {
            return new Scanner(reader).next();
        } finally {
            reader.close();
        }
    } catch (IOException | NoSuchElementException e) {
        logger.error(file.toUri().toASCIIString(), e);
        return null;
    }
}

From source file:net.minecrell.quartz.mappings.processor.MappingsGeneratorProcessor.java

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (roundEnv.processingOver()) {
        return false;
    }//from   w ww.  j a  v  a2  s  . co  m

    List<TypeElement> mappingClasses = new ArrayList<>();

    for (Element element : roundEnv.getElementsAnnotatedWith(Mapping.class)) {
        if (element instanceof TypeElement) {
            mappingClasses.add((TypeElement) element);
        }
    }

    if (mappingClasses.isEmpty()) {
        return true;
    }

    try {
        FileObject file = this.processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "",
                "mappings.json");

        Map<String, MappedClass> mappings;
        try (Reader reader = file.openReader(false)) {
            mappings = Mappings.read(reader);
        } catch (IOException ignored) {
            mappings = new HashMap<>();
        }

        ClassMapper classMappings = createMapper(mappingClasses);

        // We need to remap the descriptors of the fields and methods, use ASM for convenience
        Remapper unmapper = classMappings.createUnmapper();

        for (TypeElement mappingClass : mappingClasses) {
            String internalName = getInternalName(mappingClass);

            Mapping annotation = mappingClass.getAnnotation(Mapping.class);
            String mappedName = annotation.value();
            if (mappedName.isEmpty()) {
                mappedName = internalName;
            }

            MappedClass mapping = new MappedClass(mappedName);

            Accessible accessible = mappingClass.getAnnotation(Accessible.class);
            if (accessible != null) {
                mapping.getAccess().put("", parseAccessible(accessible));
            }

            for (Element element : mappingClass.getEnclosedElements()) {
                accessible = element.getAnnotation(Accessible.class);

                Constructor constructor = element.getAnnotation(Constructor.class);
                if (constructor != null) {
                    if (accessible != null) {
                        String constructorDesc = getDescriptor((ExecutableElement) element);

                        mapping.getAccess().put("<init>" + constructorDesc, parseAccessible(accessible));
                    }
                    continue;
                }

                annotation = element.getAnnotation(Mapping.class);
                if (annotation == null) {
                    continue;
                }

                mappedName = annotation.value();
                checkArgument(!mappedName.isEmpty(), "Mapping detection is not supported yet");

                switch (element.getKind()) {
                case METHOD:
                    ExecutableElement method = (ExecutableElement) element;
                    String methodName = method.getSimpleName().toString();
                    String methodDesc = getDescriptor(method);
                    mapping.getMethods().put(mappedName + unmapper.mapMethodDesc(methodDesc), methodName);

                    if (accessible != null) {
                        mapping.getAccess().put(methodName + methodDesc, parseAccessible(accessible));
                    }

                    break;
                case FIELD:
                case ENUM_CONSTANT:
                    VariableElement field = (VariableElement) element;
                    String fieldName = field.getSimpleName().toString();
                    mapping.getFields().put(mappedName + ':' + unmapper.mapDesc(getDescriptor(field)),
                            fieldName);

                    if (accessible != null) {
                        mapping.getAccess().put(fieldName, parseAccessible(accessible));
                    }

                    break;
                default:
                }
            }

            mappings.put(internalName, mapping);
        }

        // Generate JSON output
        file = this.processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", "mappings.json");
        try (Writer writer = file.openWriter()) {
            Mappings.write(writer, mappings);
        }

        return true;
    } catch (IOException e) {
        this.processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, ExceptionUtils.getStackTrace(e));
        throw new RuntimeException("Failed to create mappings.json", e);
    }
}

From source file:com.predic8.membrane.annot.SpringConfigurationXSDGeneratingAnnotationProcessor.java

@SuppressWarnings("unchecked")
private void read() {
    if (cache != null)
        return;//  w  w w. ja  v a 2  s  .  com

    cache = new HashMap<Class<? extends Annotation>, HashSet<Element>>();

    try {
        FileObject o = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "",
                "META-INF/membrane.cache");
        BufferedReader r = new BufferedReader(o.openReader(false));
        try {
            if (!CACHE_FILE_FORMAT_VERSION.equals(r.readLine()))
                return;
            HashSet<Element> currentSet = null;
            Class<? extends Annotation> annotationClass = null;
            while (true) {
                String line = r.readLine();
                if (line == null)
                    break;
                if (line.startsWith(" ")) {
                    line = line.substring(1);
                    TypeElement element = null;
                    try {
                        element = processingEnv.getElementUtils().getTypeElement(line);
                    } catch (RuntimeException e) {
                        // do nothing (Eclipse)
                    }
                    if (element != null) {
                        if (element.getAnnotation(annotationClass) != null)
                            currentSet.add(element);
                    }
                } else {
                    try {
                        annotationClass = (Class<? extends Annotation>) getClass().getClassLoader()
                                .loadClass(line);
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                    }
                    currentSet = new HashSet<Element>();
                    cache.put(annotationClass, currentSet);
                }
            }
        } finally {
            r.close();
        }
    } catch (FileNotFoundException e) {
        // do nothing (Maven)
    } catch (IOException e) {
        // do nothing (Eclipse)
    }

    for (Set<Element> e : cache.values()) {
        String status = "read " + e.size();
        log(status);
    }

}

From source file:org.callimachusproject.auth.DetachedRealm.java

private String readString(FileObject file) {
    try {//  w ww .java2 s .  c  o  m
        Reader reader = file.openReader(true);
        if (reader == null)
            return null;
        try {
            return new Scanner(reader).next();
        } finally {
            reader.close();
        }
    } catch (IOException e) {
        logger.error(e.toString(), e);
        return null;
    }
}