JDK-6450078 : DefaultFileManager.RegularFileObject.toUri produces bogus URI
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6
  • Priority: P2
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2006-07-18
  • Updated: 2010-04-03
  • Resolved: 2006-07-19
Related Reports
Duplicate :  
Description
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.)

Comments
EVALUATION Re. method naming: I agree with Josh's advice generally, just thought that consistency with the existing and clearly analogous File.toURI would take precedence. Not important.
20-07-2006

EVALUATION This is a duplicate of 6419926. The method toUri is capitalized as so based on advice found in Joshua Bloch's book "Effective Java". He asks the question, what would you rather see HTTPURL or HttpUrl?
19-07-2006