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

The following examples show how to use javax.tools.JavaFileObject#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: AbstractPathArchive.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isMultiRelease() {
    Boolean res = multiRelease;
    if (res == null) {
        res = Boolean.FALSE;
        if (FileObjects.JAR.equals(root.getFileSystem().provider().getScheme())) {
            try {
                final JavaFileObject jfo = getFile("META-INF/MANIFEST.MF"); //NOI18N
                if (jfo != null) {
                    try(final InputStream in = new BufferedInputStream(jfo.openInputStream())) {
                        res = FileObjects.isMultiVersionArchive(in);
                    }
                }
            } catch (IOException ioe) {
                LOG.log(
                        Level.WARNING,
                        "Cannot read: {0} manifest",    //NOI18N
                        rootURI.toString());
            }
        }
        multiRelease = res;
    }
    return res;
}
 
Example 2
Source File: StandardDocFileFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Open an input stream for the file.
 *
 * @throws DocFileIOException if there is a problem while opening stream
 */
@Override
public InputStream openInputStream() throws DocFileIOException {
    try {
        JavaFileObject fo = getJavaFileObjectForInput(file);
        return new BufferedInputStream(fo.openInputStream());
    } catch (IOException e) {
        throw new DocFileIOException(this, DocFileIOException.Mode.READ, e);
    }
}
 
Example 3
Source File: ClassUsageTrackerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void readingFileFromGetJavaFileForOutputShouldBeTracked() throws IOException {
  JavaFileObject javaFileObject =
      fileManager.getJavaFileForOutput(null, SINGLE_FILE_NAME, JavaFileObject.Kind.CLASS, null);

  javaFileObject.openInputStream();
  assertFilesRead(TEST_JAR_PATH, SINGLE_FILE_NAME);
}
 
Example 4
Source File: ClassUsageTrackerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void readingFileFromGetJavaFileObjectsFileOverloadShouldNotBeTracked() throws IOException {
  Iterable<? extends JavaFileObject> javaFileObjects =
      fileManager.getJavaFileObjects(SINGLE_FILE);

  for (JavaFileObject javaFileObject : javaFileObjects) {
    javaFileObject.openInputStream();
  }

  assertNoFilesRead();
}
 
Example 5
Source File: ClassUsageTrackerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void readingAnnotationProcessorFileFromGetJavaFileForInputShouldBeTracked()
    throws IOException {
  JavaFileObject javaFileObject =
      fileManager.getJavaFileForInput(
          ANNOTATION_PROCESSOR_PATH, SINGLE_FILE_NAME, JavaFileObject.Kind.CLASS);

  javaFileObject.openInputStream();
  assertNoFilesRead();
}
 
Example 6
Source File: BridgeHarness.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that every bridge in the generated classfile has a matching bridge
 * annotation in the bridge map
 */
protected void checkBridges(JavaFileObject jfo) {
    try (InputStream is = jfo.openInputStream()) {
        ClassFile cf = ClassFile.read(is);
        System.err.println("checking: " + cf.getName());

        List<Bridge> bridgeList = bridgesMap.get(cf.getName());
        if (bridgeList == null) {
            //no bridges - nothing to check;
            bridgeList = List.nil();
        }

        for (Method m : cf.methods) {
            if (m.access_flags.is(AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE)) {
                //this is a bridge - see if there's a match in the bridge list
                Bridge match = null;
                for (Bridge b : bridgeList) {
                    if (b.value().equals(descriptor(m, cf.constant_pool))) {
                        match = b;
                        break;
                    }
                }
                if (match == null) {
                    error("No annotation for bridge method: " + descriptor(m, cf.constant_pool));
                } else {
                    bridgeList = drop(bridgeList, match);
                }
            }
        }
        if (bridgeList.nonEmpty()) {
            error("Redundant bridge annotation found: " + bridgeList.head.value());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + jfo.toUri() +": " + e);
    }
}
 
Example 7
Source File: Probe.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String... args) throws IOException {
    if (args.length != 1) {
        System.err.println("Not enough arguments.");
        System.err.println("Usage:");
        System.err.println("    java " + Probe.class.getName() + " <output-file>");
        return ;
    }

    File outFile = new File(args[0]);
    Charset cs = Charset.forName("UTF-8");
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
    OutputStream out = new FileOutputStream(outFile);

    try {
        Iterable<JavaFileObject> bcpFiles =
                fm.list(StandardLocation.PLATFORM_CLASS_PATH, "", EnumSet.of(Kind.CLASS), true);

        for (JavaFileObject jfo : bcpFiles) {
            InputStream in = new BufferedInputStream(jfo.openInputStream());
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                StringBuilder textual = new StringBuilder();
                int read;

                while ((read = in.read()) != (-1)) {
                    baos.write(read);
                    textual.append(String.format("%02x", read));
                }

                textual.append("\n");
                out.write(textual.toString().getBytes(cs));
            } finally {
                in.close();
            }
        }
    } finally {
        out.close();
    }
}
 
Example 8
Source File: BridgeHarness.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that every bridge in the generated classfile has a matching bridge
 * annotation in the bridge map
 */
protected void checkBridges(JavaFileObject jfo) {
    try (InputStream is = jfo.openInputStream()) {
        ClassFile cf = ClassFile.read(is);
        System.err.println("checking: " + cf.getName());

        List<Bridge> bridgeList = bridgesMap.get(cf.getName());
        if (bridgeList == null) {
            //no bridges - nothing to check;
            bridgeList = List.nil();
        }

        for (Method m : cf.methods) {
            if (m.access_flags.is(AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE)) {
                //this is a bridge - see if there's a match in the bridge list
                Bridge match = null;
                for (Bridge b : bridgeList) {
                    if (b.value().equals(descriptor(m, cf.constant_pool))) {
                        match = b;
                        break;
                    }
                }
                if (match == null) {
                    error("No annotation for bridge method: " + descriptor(m, cf.constant_pool));
                } else {
                    bridgeList = drop(bridgeList, match);
                }
            }
        }
        if (bridgeList.nonEmpty()) {
            error("Redundant bridge annotation found: " + bridgeList.head.value());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + jfo.toUri() +": " + e);
    }
}
 
Example 9
Source File: BridgeHarness.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that every bridge in the generated classfile has a matching bridge
 * annotation in the bridge map
 */
protected void checkBridges(JavaFileObject jfo) {
    try (InputStream is = jfo.openInputStream()) {
        ClassFile cf = ClassFile.read(is);
        System.err.println("checking: " + cf.getName());

        List<Bridge> bridgeList = bridgesMap.get(cf.getName());
        if (bridgeList == null) {
            //no bridges - nothing to check;
            bridgeList = List.nil();
        }

        for (Method m : cf.methods) {
            if (m.access_flags.is(AccessFlags.ACC_SYNTHETIC | AccessFlags.ACC_BRIDGE)) {
                //this is a bridge - see if there's a match in the bridge list
                Bridge match = null;
                for (Bridge b : bridgeList) {
                    if (b.value().equals(descriptor(m, cf.constant_pool))) {
                        match = b;
                        break;
                    }
                }
                if (match == null) {
                    error("No annotation for bridge method: " + descriptor(m, cf.constant_pool));
                } else {
                    bridgeList = drop(bridgeList, match);
                }
            }
        }
        if (bridgeList.nonEmpty()) {
            error("Redundant bridge annotation found: " + bridgeList.head.value());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new Error("error reading " + jfo.toUri() +": " + e);
    }
}
 
Example 10
Source File: JavaFileManagerClassLoader.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Reads all class file data into a byte array from the given file
 * object.
 *
 * @param classFile the class file to read.
 * @return the class data.
 * @throws IOException if an I/O error occurs.
 */
private byte[] readClassData(JavaFileObject classFile) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[4096];
    InputStream classStream = classFile.openInputStream();
    int n = classStream.read(buf);
    while (n > 0) {
        bos.write(buf, 0, n);
        n = classStream.read(buf);
    }
    classStream.close();
    return bos.toByteArray();
}
 
Example 11
Source File: ClassUsageTrackerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void readingAnnotationProcessorFileFromGetJavaFileForOutputShouldNotBeTracked()
    throws IOException {
  JavaFileObject javaFileObject =
      fileManager.getJavaFileForOutput(
          ANNOTATION_PROCESSOR_PATH, SINGLE_FILE_NAME, JavaFileObject.Kind.CLASS, null);

  javaFileObject.openInputStream();
  assertNoFilesRead();
}
 
Example 12
Source File: ProfileSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@NonNull
private Object findProfile(@NonNull final String binaryName) {
    Object res = UNKNOWN;
    final StringBuilder sb = new StringBuilder(binaryName);
    sb.append('.'); //NOI18N
    sb.append(FileObjects.CLASS);
    try {
        final JavaFileObject jfo = ctSym.getFile(sb.toString());
        if (jfo != null) {
            try (InputStream in = jfo.openInputStream()) {
                final ClassFile cf = new ClassFile(in);
                final Annotation a = cf.getAnnotation(ClassName.getClassName(ANNOTATION_PROFILE));
                if (a == null) {
                    res = Profile.COMPACT1;
                } else {
                    final AnnotationComponent ac = a.getComponent(ANNOTATION_VALUE);
                    res = profileFromAnnotationComponent(ac);
                }
            }
        }
    } catch (IOException ioe) {
        LOG.log(
                Level.INFO,
                "Cannot read class: {0}, reason: {1}",  //NOI18N
                new Object[]{
                    sb,
                    ioe.getMessage()
                });
    }
    return res;
}
 
Example 13
Source File: ClassUsageTrackerTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void testWindowsPathsDontBlowUp() throws IOException {
  JavaFileObject javaFileObject =
      fileManager.getJavaFileForInput(null, WINDOWS_FILE_NAME, JavaFileObject.Kind.CLASS);

  javaFileObject.openInputStream();
  assertFilesRead(WINDOWS_JAR_PATH, WINDOWS_FILE_NAME);
}
 
Example 14
Source File: StandardDocFileFactory.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/** Open an input stream for the file. */
public InputStream openInputStream() throws IOException {
    JavaFileObject fo = getJavaFileObjectForInput(file);
    return new BufferedInputStream(fo.openInputStream());
}
 
Example 15
Source File: PathDocFileFactory.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/** Open an input stream for the file. */
public InputStream openInputStream() throws IOException {
    JavaFileObject fo = getJavaFileObjectForInput(file);
    return new BufferedInputStream(fo.openInputStream());
}
 
Example 16
Source File: StandardDocFileFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/** Open an input stream for the file. */
public InputStream openInputStream() throws IOException {
    JavaFileObject fo = getJavaFileObjectForInput(file);
    return new BufferedInputStream(fo.openInputStream());
}
 
Example 17
Source File: PathDocFileFactory.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/** Open an input stream for the file. */
public InputStream openInputStream() throws IOException {
    JavaFileObject fo = getJavaFileObjectForInput(file);
    return new BufferedInputStream(fo.openInputStream());
}
 
Example 18
Source File: StandardDocFileFactory.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/** Open an input stream for the file. */
public InputStream openInputStream() throws IOException {
    JavaFileObject fo = getJavaFileObjectForInput(file);
    return new BufferedInputStream(fo.openInputStream());
}
 
Example 19
Source File: StandardDocFileFactory.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/** Open an input stream for the file. */
public InputStream openInputStream() throws IOException {
    JavaFileObject fo = getJavaFileObjectForInput(file);
    return new BufferedInputStream(fo.openInputStream());
}
 
Example 20
Source File: TestBase.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns class file that is read from {@code fileObject}.
 *
 * @param fileObject a file object
 * @return class file that is read from {@code fileObject}
 * @throws IOException if I/O error occurs
 * @throws ConstantPoolException if constant pool error occurs
 */
public ClassFile readClassFile(JavaFileObject fileObject) throws IOException, ConstantPoolException {
    try (InputStream is = fileObject.openInputStream()) {
        return readClassFile(is);
    }
}