Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6450078

DefaultFileManager.RegularFileObject.toUri produces bogus URI

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P2 P2
    • None
    • 6
    • tools
    • None
    • x86
    • linux

      When receiving error diagnostics from the compiler:

      Diagnostic<? extends JavaFileObject> d;
      URI u = d.getSource().toUri();
      File f = new File(u);

      does not work. You need to use e.g.

      File f = new File(u.toString());

      since the "URI" is just a pathname, not a real 'file'-protocol URI.

      In j2se/src/share/classes/com/sun/tools/javac/util/DefaultFileManager.java, it can be seen that the impl of RegularFileObject.toUri() [BTW why the inconsistent capitalization vs. File.toURI?] is incorrect:

              public URI toUri() {
                  try {
                      return new URI(f.getPath());
                  } catch (URISyntaxException ex) {
                      return f.toURI();
                  }
              }

      which should read simply:

              public URI toUri() {
                  return f.toURI();
              }

      Similarly, ZipFileObject gets it wrong too:

              public URI toUri() {
                  String zipName = new File(getZipName()).toURI().getPath();
                  String entryName = getZipEntryName();
                  return URI.create("jar:" + zipName + "!" + entryName);
              }

      should read:

              public URI toUri() {
                  String zipName = new File(getZipName()).toURI().toString();
                  String entryName = getZipEntryName();
                  return URI.create("jar:" + zipName + "!" + entryName);
              }

      (Not sure whether URI.toString() is appropriate here or whether e.g. URI.toASCIIString() should be used.)

            ahe Peter Ahe
            jglick Jesse Glick (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: