Java Code Examples for javax.tools.FileObject#openInputStream()

The following examples show how to use javax.tools.FileObject#openInputStream() . 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: ConfigLoader.java    From deptective with Apache License 2.0 6 votes vote down vote up
private InputStream getConfigStream(Optional<Path> configFile, JavaFileManager jfm) {
    try {
        if (configFile.isPresent()) {
            return Files.newInputStream(configFile.get());
        }
        else {
            FileObject file = jfm.getFileForInput(StandardLocation.SOURCE_PATH, "", "deptective.json");

            if (file != null) {
                return file.openInputStream();
            }

            file = jfm.getFileForInput(StandardLocation.CLASS_PATH, "", "META-INF/deptective.json");

            if (file != null) {
                return file.openInputStream();
            }
        }
    }
    catch (IOException e) {
        throw new RuntimeException("Failed to load Deptective configuration file", e);
    }

    return null;
}
 
Example 2
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 3
Source File: ResourcecifyProcessor.java    From sundrio with Apache License 2.0 6 votes vote down vote up
/**
 * Copy one {@link FileObject} into an other.
 * @param source    The source {@link FileObject}.
 * @param target    The target {@link FileObject}.
 * @throws IOException
 */
public static void copy(FileObject source, FileObject target) throws IOException {
    InputStream in = source.openInputStream();
    OutputStream out = target.openOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int len = in.read(buffer);
        while (len != -1) {
            out.write(buffer, 0, len);
            len = in.read(buffer);
        }
    } finally {
        in.close();
        out.close();
    }
}
 
Example 4
Source File: ConfigLoader.java    From deptective with Apache License 2.0 6 votes vote down vote up
private InputStream getConfigStream(Optional<Path> configFile, JavaFileManager jfm) {
    try {
        if (configFile.isPresent()) {
            return Files.newInputStream(configFile.get());
        }
        else {
            FileObject file = jfm.getFileForInput(StandardLocation.SOURCE_PATH, "", "deptective.json");

            if (file != null) {
                return file.openInputStream();
            }

            file = jfm.getFileForInput(StandardLocation.CLASS_PATH, "", "META-INF/deptective.json");

            if (file != null) {
                return file.openInputStream();
            }
        }
    }
    catch (IOException e) {
        throw new RuntimeException("Failed to load Deptective configuration file", e);
    }

    return null;
}
 
Example 5
Source File: PropertiesDocReader.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void readMessages()
{
	try
	{
		FileObject resource = environment.getFiler().getResource(StandardLocation.SOURCE_PATH, 
				"", properties.getMessagesName() + ".properties");
		
		Properties messages = new Properties();
		try (InputStream in = resource.openInputStream())
		{
			messages.load(in);
		}
		
		propertyMessages = messages;
	}
	catch (IOException e)
	{
		environment.getMessager().printMessage(Kind.WARNING, "Failed to read source of " 
				+ properties.getMessagesName() + ".properties: " + e.getMessage());
	}
}
 
Example 6
Source File: MultiReleaseJarAwareSJFM.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
    int n = name.lastIndexOf('.');
    String pkg = n == -1 ? "" : name.substring(0, n);
    String cls = name.substring(n + 1) + ".class";
    byte[] b;
    try {
        FileObject obj = jfm.getFileForInput(jloc, pkg, cls);
        try (InputStream is = obj.openInputStream()) {
            b = is.readAllBytes();
        }
    } catch (IOException x) {
        throw new ClassNotFoundException(x.getMessage(), x);
    }
    return defineClass(name, b, 0, b.length);
}
 
Example 7
Source File: ResourcesTest.java    From doma with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileObjectImpl_openInputStream() throws Exception {
  File file = File.createTempFile("aaa", null);
  try {
    FileObject fileObject = new Resources.FileObjectImpl(file.toPath());
    try (InputStream is = fileObject.openInputStream()) {
      is.read();
    }
  } finally {
    file.delete();
  }
}
 
Example 8
Source File: ClassUsageTrackerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void readingJavaAnnotationProcessorFileFromGetFileForOutputShouldNotBeTracked()
    throws IOException {
  FileObject fileObject =
      fileManager.getFileForOutput(ANNOTATION_PROCESSOR_PATH, null, SINGLE_FILE_NAME, null);

  fileObject.openInputStream();
  assertNoFilesRead();
}
 
Example 9
Source File: ClassUsageTrackerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void readingNonJavaAnnotationProcessorFileFromGetFileForOutputShouldNotBeTracked()
    throws IOException {
  FileObject fileObject =
      fileManager.getFileForOutput(
          ANNOTATION_PROCESSOR_PATH, null, SINGLE_NON_JAVA_FILE_NAME, null);

  fileObject.openInputStream();
  assertNoFilesRead();
}
 
Example 10
Source File: AnnotationProcessor.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
  try {
    FileObject resource =
        processingEnv.getFiler().getResource(StandardLocation.CLASS_PATH, "", "read_file.txt");

    InputStream stream = resource.openInputStream();
    stream.read();
  } catch (IOException e) {
    processingEnv
        .getMessager()
        .printMessage(Diagnostic.Kind.ERROR, "Could not open read_file.txt");
  }
  return false;
}
 
Example 11
Source File: Gen.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void writeIfChanged(byte[] b, FileObject file) throws IOException {
    boolean mustWrite = false;
    String event = "[No need to update file ";

    if (force) {
        mustWrite = true;
        event = "[Forcefully writing file ";
    } else {
        InputStream in;
        byte[] a;
        try {
            // regrettably, there's no API to get the length in bytes
            // for a FileObject, so we can't short-circuit reading the
            // file here
            in = file.openInputStream();
            a = readBytes(in);
            if (!Arrays.equals(a, b)) {
                mustWrite = true;
                event = "[Overwriting file ";

            }
        } catch (FileNotFoundException e) {
            mustWrite = true;
            event = "[Creating file ";
        }
    }

    if (util.verbose)
        util.log(event + file + "]");

    if (mustWrite) {
        OutputStream out = file.openOutputStream();
        out.write(b); /* No buffering, just one big write! */
        out.close();
    }
}
 
Example 12
Source File: Gen.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void writeIfChanged(byte[] b, FileObject file) throws IOException {
    boolean mustWrite = false;
    String event = "[No need to update file ";

    if (force) {
        mustWrite = true;
        event = "[Forcefully writing file ";
    } else {
        InputStream in;
        byte[] a;
        try {
            // regrettably, there's no API to get the length in bytes
            // for a FileObject, so we can't short-circuit reading the
            // file here
            in = file.openInputStream();
            a = readBytes(in);
            if (!Arrays.equals(a, b)) {
                mustWrite = true;
                event = "[Overwriting file ";

            }
        } catch (FileNotFoundException e) {
            mustWrite = true;
            event = "[Creating file ";
        }
    }

    if (util.verbose)
        util.log(event + file + "]");

    if (mustWrite) {
        OutputStream out = file.openOutputStream();
        out.write(b); /* No buffering, just one big write! */
        out.close();
    }
}
 
Example 13
Source File: ClassUsageTrackerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void readingNonJavaFileFromGetFileForInputShouldBeTracked() throws IOException {
  FileObject fileObject = fileManager.getFileForInput(null, null, SINGLE_NON_JAVA_FILE_NAME);

  fileObject.openInputStream();
  assertTrue(fileWasRead(TEST_JAR_PATH, SINGLE_NON_JAVA_FILE_NAME));
}
 
Example 14
Source File: Gen.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void writeIfChanged(byte[] b, FileObject file) throws IOException {
    boolean mustWrite = false;
    String event = "[No need to update file ";

    if (force) {
        mustWrite = true;
        event = "[Forcefully writing file ";
    } else {
        InputStream in;
        byte[] a;
        try {
            // regrettably, there's no API to get the length in bytes
            // for a FileObject, so we can't short-circuit reading the
            // file here
            in = file.openInputStream();
            a = readBytes(in);
            if (!Arrays.equals(a, b)) {
                mustWrite = true;
                event = "[Overwriting file ";

            }
        } catch (FileNotFoundException | NoSuchFileException e) {
            mustWrite = true;
            event = "[Creating file ";
        }
    }

    if (util.verbose)
        util.log(event + file + "]");

    if (mustWrite) {
        OutputStream out = file.openOutputStream();
        out.write(b); /* No buffering, just one big write! */
        out.close();
    }
}
 
Example 15
Source File: ClassUsageTrackerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void readingJavaAnnotationProcessorFileFromGetFileForInputShouldNotBeTracked()
    throws IOException {
  FileObject fileObject =
      fileManager.getFileForInput(ANNOTATION_PROCESSOR_PATH, null, SINGLE_FILE_NAME);

  fileObject.openInputStream();
  assertNoFilesRead();
}
 
Example 16
Source File: MIMEResolverProcessor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private byte[] generateInstanceResolver(FileObject fo, Element e, File f,
        Registration r) throws LayerGenerationException {
    try {
        InputStream is = fo.openInputStream();
        org.openide.filesystems.FileObject tmp = FileUtil.createMemoryFileSystem().getRoot().createData("resolver.xml");
        OutputStream os = tmp.getOutputStream();
        for (;;) {
            int ch = is.read();
            if (ch == -1) {
                break;
            }
            os.write(ch);
        }
        os.close();
        is.close();
        MIMEResolver resolver = MIMEResolverImpl.forDescriptor(tmp, false);
        setFileChooserRelatedAttributes(r, resolver, f);
        final byte[] almostResult = MIMEResolverImpl.toStream(resolver);
        // XXX: it would be slightly shorter to return the array directly,
        // but the XMLFileSystem insist on deserializing the value, it does
        // not support returning plain byte[]
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(out);
        oos.writeObject(almostResult);
        oos.close();
        return out.toByteArray();
    } catch (IOException ex) {
        final LayerGenerationException le = new LayerGenerationException("Cannot process " + fo, e);
        le.initCause(ex);
        throw le;
    }
}
 
Example 17
Source File: Gen.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void writeIfChanged(byte[] b, FileObject file) throws IOException {
    boolean mustWrite = false;
    String event = "[No need to update file ";

    if (force) {
        mustWrite = true;
        event = "[Forcefully writing file ";
    } else {
        InputStream in;
        byte[] a;
        try {
            // regrettably, there's no API to get the length in bytes
            // for a FileObject, so we can't short-circuit reading the
            // file here
            in = file.openInputStream();
            a = readBytes(in);
            if (!Arrays.equals(a, b)) {
                mustWrite = true;
                event = "[Overwriting file ";

            }
        } catch (FileNotFoundException e) {
            mustWrite = true;
            event = "[Creating file ";
        }
    }

    if (util.verbose)
        util.log(event + file + "]");

    if (mustWrite) {
        OutputStream out = file.openOutputStream();
        out.write(b); /* No buffering, just one big write! */
        out.close();
    }
}
 
Example 18
Source File: SlimConfigurationProcessor.java    From spring-init with Apache License 2.0 5 votes vote down vote up
public void loadState() {
	Properties properties = new Properties();
	try {
		FileObject resource = filer.getResource(StandardLocation.CLASS_OUTPUT, "",
				SLIM_STATE_PATH);
		try (InputStream stream = resource.openInputStream();) {
			properties.load(stream);
		}
		messager.printMessage(Kind.NOTE,
				"Loading imports information from previous build:" + properties);
		for (Map.Entry<Object, Object> property : properties.entrySet()) {
			String annotationType = (String) property.getKey(); // registrarinitializer.XXXX.YYY.ZZZ
			String k = annotationType.substring("import.".length());
			TypeElement kte = utils.asTypeElement(k);
			for (String v : ((String) property.getValue()).split(",")) {
				TypeElement vte = utils.asTypeElement(v);
				if (kte == null || vte == null) {
					// TODO need to cope with types being removed across incremental
					// builds - is this ok?
					messager.printMessage(Kind.NOTE,
							"Looks like a type has been removed, ignoring registrar entry "
									+ k + "=" + v + " resolved to " + kte + "="
									+ vte);
				}
				else {
					imports.addImport(kte, vte);
				}
			}
		}
		messager.printMessage(Kind.NOTE,
				"Loaded " + properties.size() + " import definitions");
	}
	catch (IOException e) {
		messager.printMessage(Kind.NOTE,
				"Cannot load " + SLIM_STATE_PATH + " (normal on first full build)");
	}
}
 
Example 19
Source File: Gen.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private void writeIfChanged(byte[] b, FileObject file) throws IOException {
    boolean mustWrite = false;
    String event = "[No need to update file ";

    if (force) {
        mustWrite = true;
        event = "[Forcefully writing file ";
    } else {
        InputStream in;
        byte[] a;
        try {
            // regrettably, there's no API to get the length in bytes
            // for a FileObject, so we can't short-circuit reading the
            // file here
            in = file.openInputStream();
            a = readBytes(in);
            if (!Arrays.equals(a, b)) {
                mustWrite = true;
                event = "[Overwriting file ";

            }
        } catch (FileNotFoundException e) {
            mustWrite = true;
            event = "[Creating file ";
        }
    }

    if (util.verbose)
        util.log(event + file + "]");

    if (mustWrite) {
        OutputStream out = file.openOutputStream();
        out.write(b); /* No buffering, just one big write! */
        out.close();
    }
}
 
Example 20
Source File: ClassUsageTrackerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void readingJavaFileFromGetFileForInputShouldBeTracked() throws IOException {
  FileObject fileObject = fileManager.getFileForInput(null, null, SINGLE_FILE_NAME);

  fileObject.openInputStream();
  assertFilesRead(TEST_JAR_PATH, SINGLE_FILE_NAME);
}