Java Code Examples for javax.annotation.processing.Filer#getResource()

The following examples show how to use javax.annotation.processing.Filer#getResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationUtils.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
public static String getContent(Filer filer, String c){
    String outputFile = "META-INF/konduit-serving/" + c;
    try {
        FileObject file = filer.getResource(StandardLocation.CLASS_OUTPUT, "", outputFile);
        InputStream is = file.openInputStream();
        StringBuilder sb = new StringBuilder();
        try (Reader r = new BufferedReader(new InputStreamReader(is))) {
            int ch = 0;
            while ((ch = r.read()) != -1) {
                sb.append((char) ch);
            }
        }
        return sb.toString();
    } catch (IOException e){
        throw new RuntimeException("ERROR READING FILE", e);
    }
}
 
Example 2
Source File: PropNameInterStageStore.java    From litho with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method for obtaining resources from a {@link Filer}, taking care of some javac
 * peculiarities.
 */
private static Optional<FileObject> getResource(
    final Filer filer,
    final JavaFileManager.Location location,
    final String packageName,
    final String filePath) {
  try {
    final FileObject resource = filer.getResource(location, packageName, filePath);
    resource.openInputStream().close();
    return Optional.of(resource);
  } catch (final Exception e) {
    // ClientCodeException can be thrown by a bug in the javac ClientCodeWrapper
    if (!(e instanceof FileNotFoundException
        || e.getClass().getName().equals("com.sun.tools.javac.util.ClientCodeException"))) {
      throw new RuntimeException(
          String.format("Error opening resource %s/%s", packageName, filePath), e.getCause());
    }
    return Optional.empty();
  }
}
 
Example 3
Source File: AnnotationProcessorUtil.java    From kumuluzee with MIT License 6 votes vote down vote up
private static FileObject readOldFile(Set<String> content, String resourceName, Filer filer) throws IOException {
    Reader reader = null;
    try {
        final FileObject resource = filer.getResource(StandardLocation.CLASS_OUTPUT, "", resourceName);
        reader = resource.openReader(true);
        AnnotationProcessorUtil.readOldFile(content, reader);
        return resource;
    } catch (FileNotFoundException e) {
        // close reader, return null
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return null;
}
 
Example 4
Source File: VelocityTransformationProcessor.java    From sundrio with Apache License 2.0 6 votes vote down vote up
private String readTemplate(Filer filer, String pkg, String template) throws IOException {
    FileObject o;
    if (template == null) {
        throw new IllegalArgumentException("Template in:" + VelocityTransformation.class.getName() + " cannot be null.");
    }

    String targetPkg = pkg == null || (template != null && template.startsWith("/"))
            ? ""
            : pkg;

    String targetTemplate = template.startsWith("/") ? template.substring(1) : template;

    try {
        o = filer.getResource(StandardLocation.SOURCE_PATH, targetPkg, targetTemplate);
    } catch (IOException e) {
        try {
            o = filer.getResource(StandardLocation.CLASS_PATH, targetPkg, targetTemplate);
        } catch (IOException ex) {
            throw e;
        }
    }
    if (o == null) {
        throw new IOException("Template resource: " + template+ " couldn't be found in sources or classpath.");
    }
    return o.getCharContent(false).toString();
}
 
Example 5
Source File: AnnotatedMixins.java    From Mixin with MIT License 6 votes vote down vote up
public Properties getProperties() {
    if (this.properties == null) {
        this.properties = new Properties();

        try {
            Filer filer = this.processingEnv.getFiler();
            FileObject propertyFile = filer.getResource(StandardLocation.SOURCE_PATH, "", "mixin.properties");
            if (propertyFile != null) {
                InputStream inputStream = propertyFile.openInputStream();
                this.properties.load(inputStream);
                inputStream.close();
            }
        } catch (Exception ex) {
            // ignore
        }
    }

    return this.properties;
}
 
Example 6
Source File: AnnotationUtils.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
public static boolean fileExists(Filer filer, String c){
    String outputFile = "META-INF/konduit-serving/" + c;
    try {
        FileObject file = filer.getResource(StandardLocation.CLASS_OUTPUT, "", outputFile);
        return file != null;
    } catch (IOException e){
        return false;
    }
}
 
Example 7
Source File: TreeShimsCopier.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annos, RoundEnvironment roundEnv) {
    for (Element el : roundEnv.getRootElements()) {
        if (el.getKind() != ElementKind.CLASS)
            continue;
        TypeElement type = (TypeElement) el;
        String qualName = type.getQualifiedName().toString();
        String targetPackage = ALLOWED_CLASSES2TARGET_PACKAGE.get(qualName);
        if (targetPackage != null) {
            try {
                Filer filer = processingEnv.getFiler();
                FileObject fo = filer.getResource(StandardLocation.SOURCE_PATH, ((PackageElement) type.getEnclosingElement()).getQualifiedName().toString(), type.getSimpleName() + ".java");
                URI source = fo.toUri();
                StringBuilder path2Shims = new StringBuilder();
                int p = qualName.split("\\.").length;
                for (int i = 0; i < p; i++) {
                    path2Shims.append("../");
                }
                path2Shims.append("../java.source.base/src/org/netbeans/modules/java/source/TreeShims.java");
                URI treeShims = source.resolve(path2Shims.toString());
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try (InputStream in = treeShims.toURL().openStream()) {
                    int r;

                    while ((r = in.read()) != (-1)) {
                        baos.write(r);
                    }
                }
                String content = new String(baos.toByteArray(), "UTF-8");
                content = content.replace("package org.netbeans.modules.java.source;", "package " + targetPackage + ";");
                try (OutputStream out = filer.createSourceFile(targetPackage + ".TreeShims", type).openOutputStream()) {
                    out.write(content.getBytes("UTF-8"));
                }
            } catch (IOException ex) {
                throw new IllegalStateException(ex);
            }
        }
    }
    return false;
}