6419701: DefaultFileManager clean up: URI.create

Tue, 08 Sep 2009 11:12:13 -0700

author
jjg
date
Tue, 08 Sep 2009 11:12:13 -0700
changeset 400
35e29f51a7c3
parent 399
90c28923e449
child 401
dd98acd9f717

6419701: DefaultFileManager clean up: URI.create
6483788: DefaultFileManager.ZipFileObject.toUri() fails to escape space characters
6501502: JSR 199: FileObject.toUri should return file:///c:/ or file:/c:/ not file://c:/
6877206: JavaFileObject.toUri returns bogus URI (win)
6877223: tests @ignored because of issues with File.toURI on Windows
Reviewed-by: mcimadamore, alanb

src/share/classes/com/sun/tools/javac/file/BaseFileObject.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/file/JavacFileManager.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/file/RegularFileObject.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/file/SymbolArchive.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/file/ZipArchive.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/file/ZipFileIndexArchive.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/jvm/ClassReader.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javadoc/ClassDocImpl.java file | annotate | diff | comparison | revisions
test/tools/javac/Diagnostics/6769027/tester.properties file | annotate | diff | comparison | revisions
test/tools/javac/api/6440333/T6440333.java file | annotate | diff | comparison | revisions
test/tools/javac/api/Sibling.java file | annotate | diff | comparison | revisions
test/tools/javac/api/T6483788.java file | annotate | diff | comparison | revisions
test/tools/javac/api/T6501502.java file | annotate | diff | comparison | revisions
test/tools/javac/api/T6877206.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/file/BaseFileObject.java	Thu Sep 03 18:34:17 2009 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/file/BaseFileObject.java	Tue Sep 08 11:12:13 2009 -0700
     1.3 @@ -29,6 +29,8 @@
     1.4  import java.io.IOException;
     1.5  import java.io.InputStreamReader;
     1.6  import java.io.Reader;
     1.7 +import java.net.URI;
     1.8 +import java.net.URISyntaxException;
     1.9  import java.nio.charset.CharsetDecoder;
    1.10  import javax.lang.model.element.Modifier;
    1.11  import javax.lang.model.element.NestingKind;
    1.12 @@ -93,7 +95,26 @@
    1.13          return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
    1.14      }
    1.15  
    1.16 +    protected static URI createJarUri(File jarFile, String entryName) {
    1.17 +        URI jarURI = jarFile.toURI().normalize();
    1.18 +        String separator = entryName.startsWith("/") ? "!" : "!/";
    1.19 +        try {
    1.20 +            // The jar URI convention appears to be not to re-encode the jarURI
    1.21 +            return new URI("jar:" + jarURI + separator + entryName);
    1.22 +        } catch (URISyntaxException e) {
    1.23 +            throw new CannotCreateUriError(jarURI + separator + entryName, e);
    1.24 +        }
    1.25 +    }
    1.26 +
    1.27 +    /** Used when URLSyntaxException is thrown unexpectedly during
    1.28 +     *  implementations of (Base)FileObject.toURI(). */
    1.29 +    protected static class CannotCreateUriError extends Error {
    1.30 +        private static final long serialVersionUID = 9101708840997613546L;
    1.31 +        public CannotCreateUriError(String value, Throwable cause) {
    1.32 +            super(value, cause);
    1.33 +        }
    1.34 +    }
    1.35 +
    1.36      /** The file manager that created this JavaFileObject. */
    1.37      protected final JavacFileManager fileManager;
    1.38 -
    1.39  }
     2.1 --- a/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Thu Sep 03 18:34:17 2009 -0700
     2.2 +++ b/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Tue Sep 08 11:12:13 2009 -0700
     2.3 @@ -26,6 +26,7 @@
     2.4  package com.sun.tools.javac.file;
     2.5  
     2.6  import java.io.ByteArrayOutputStream;
     2.7 +import java.io.Closeable;
     2.8  import java.io.File;
     2.9  import java.io.FileInputStream;
    2.10  import java.io.FileNotFoundException;
    2.11 @@ -36,6 +37,7 @@
    2.12  import java.lang.reflect.Constructor;
    2.13  import java.net.MalformedURLException;
    2.14  import java.net.URI;
    2.15 +import java.net.URISyntaxException;
    2.16  import java.net.URL;
    2.17  import java.net.URLClassLoader;
    2.18  import java.nio.ByteBuffer;
    2.19 @@ -77,7 +79,6 @@
    2.20  import com.sun.tools.javac.util.Log;
    2.21  import com.sun.tools.javac.util.Options;
    2.22  
    2.23 -import java.io.Closeable;
    2.24  import static javax.tools.StandardLocation.*;
    2.25  import static com.sun.tools.javac.main.OptionName.*;
    2.26  
    2.27 @@ -437,6 +438,7 @@
    2.28              return Collections.emptySet();
    2.29          }
    2.30  
    2.31 +        @Override
    2.32          public String toString() {
    2.33              return "MissingArchive[" + zipFileName + "]";
    2.34          }
    2.35 @@ -654,10 +656,10 @@
    2.36      private final ByteBufferCache byteBufferCache;
    2.37  
    2.38      CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
    2.39 -        Charset charset = (this.charset == null)
    2.40 +        Charset cs = (this.charset == null)
    2.41              ? Charset.forName(encodingName)
    2.42              : this.charset;
    2.43 -        CharsetDecoder decoder = charset.newDecoder();
    2.44 +        CharsetDecoder decoder = cs.newDecoder();
    2.45  
    2.46          CodingErrorAction action;
    2.47          if (ignoreEncodingErrors)
    2.48 @@ -892,7 +894,7 @@
    2.49          nullCheck(location);
    2.50          // validatePackageName(packageName);
    2.51          nullCheck(packageName);
    2.52 -        if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701
    2.53 +        if (!isRelativeUri(relativeName))
    2.54              throw new IllegalArgumentException("Invalid relative name: " + relativeName);
    2.55          RelativeFile name = packageName.length() == 0
    2.56              ? new RelativeFile(relativeName)
    2.57 @@ -946,7 +948,7 @@
    2.58          nullCheck(location);
    2.59          // validatePackageName(packageName);
    2.60          nullCheck(packageName);
    2.61 -        if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701
    2.62 +        if (!isRelativeUri(relativeName))
    2.63              throw new IllegalArgumentException("relativeName is invalid");
    2.64          RelativeFile name = packageName.length() == 0
    2.65              ? new RelativeFile(relativeName)
    2.66 @@ -1085,6 +1087,15 @@
    2.67          return first != '.' && first != '/';
    2.68      }
    2.69  
    2.70 +    // Convenience method
    2.71 +    protected static boolean isRelativeUri(String u) {
    2.72 +        try {
    2.73 +            return isRelativeUri(new URI(u));
    2.74 +        } catch (URISyntaxException e) {
    2.75 +            return false;
    2.76 +        }
    2.77 +    }
    2.78 +
    2.79      /**
    2.80       * Converts a relative file name to a relative URI.  This is
    2.81       * different from File.toURI as this method does not canonicalize
    2.82 @@ -1099,7 +1110,7 @@
    2.83      public static String getRelativeName(File file) {
    2.84          if (!file.isAbsolute()) {
    2.85              String result = file.getPath().replace(File.separatorChar, '/');
    2.86 -            if (JavacFileManager.isRelativeUri(URI.create(result))) // FIXME 6419701
    2.87 +            if (isRelativeUri(result))
    2.88                  return result;
    2.89          }
    2.90          throw new IllegalArgumentException("Invalid relative path: " + file);
     3.1 --- a/src/share/classes/com/sun/tools/javac/file/RegularFileObject.java	Thu Sep 03 18:34:17 2009 -0700
     3.2 +++ b/src/share/classes/com/sun/tools/javac/file/RegularFileObject.java	Tue Sep 08 11:12:13 2009 -0700
     3.3 @@ -34,7 +34,6 @@
     3.4  import java.io.OutputStreamWriter;
     3.5  import java.io.Writer;
     3.6  import java.net.URI;
     3.7 -import java.net.URISyntaxException;
     3.8  import java.nio.ByteBuffer;
     3.9  import java.nio.CharBuffer;
    3.10  import java.nio.charset.CharsetDecoder;
    3.11 @@ -73,6 +72,7 @@
    3.12          return new FileInputStream(f);
    3.13      }
    3.14  
    3.15 +    @Override
    3.16      protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
    3.17          return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
    3.18      }
    3.19 @@ -147,6 +147,7 @@
    3.20      }
    3.21  
    3.22      @Deprecated
    3.23 +    @Override
    3.24      public String getPath() {
    3.25          return f.getPath();
    3.26      }
    3.27 @@ -201,11 +202,6 @@
    3.28      }
    3.29  
    3.30      public URI toUri() {
    3.31 -        try {
    3.32 -            String path = f.getAbsolutePath().replace(File.separatorChar, '/');
    3.33 -            return new URI("file://" + path).normalize();
    3.34 -        } catch (URISyntaxException ex) {
    3.35 -            return f.toURI();
    3.36 -        }
    3.37 +        return f.toURI().normalize();
    3.38      }
    3.39  }
     4.1 --- a/src/share/classes/com/sun/tools/javac/file/SymbolArchive.java	Thu Sep 03 18:34:17 2009 -0700
     4.2 +++ b/src/share/classes/com/sun/tools/javac/file/SymbolArchive.java	Tue Sep 08 11:12:13 2009 -0700
     4.3 @@ -73,12 +73,14 @@
     4.4          map.put(dirname, list);
     4.5      }
     4.6  
     4.7 +    @Override
     4.8      public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
     4.9          RelativeDirectory prefix_subdir = new RelativeDirectory(prefix, subdirectory.path);
    4.10          ZipEntry ze = new RelativeFile(prefix_subdir, file).getZipEntry(zdir);
    4.11          return new SymbolFileObject(this, file, ze);
    4.12      }
    4.13  
    4.14 +    @Override
    4.15      public String toString() {
    4.16          return "SymbolArchive[" + zdir.getName() + "]";
    4.17      }
     5.1 --- a/src/share/classes/com/sun/tools/javac/file/ZipArchive.java	Thu Sep 03 18:34:17 2009 -0700
     5.2 +++ b/src/share/classes/com/sun/tools/javac/file/ZipArchive.java	Tue Sep 08 11:12:13 2009 -0700
     5.3 @@ -122,6 +122,7 @@
     5.4          zdir.close();
     5.5      }
     5.6  
     5.7 +    @Override
     5.8      public String toString() {
     5.9          return "ZipArchive[" + zdir.getName() + "]";
    5.10      }
    5.11 @@ -154,6 +155,7 @@
    5.12              throw new UnsupportedOperationException();
    5.13          }
    5.14  
    5.15 +        @Override
    5.16          protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
    5.17              return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
    5.18          }
    5.19 @@ -177,6 +179,7 @@
    5.20          }
    5.21  
    5.22          @Deprecated
    5.23 +        @Override
    5.24          public String getPath() {
    5.25              return zarch.zdir.getName() + "(" + entry + ")";
    5.26          }
    5.27 @@ -235,9 +238,8 @@
    5.28          }
    5.29  
    5.30          public URI toUri() {
    5.31 -            String zipName = new File(getZipName()).toURI().normalize().getPath();
    5.32 -            String entryName = getZipEntryName();
    5.33 -            return URI.create("jar:" + zipName + "!" + entryName);
    5.34 +            File zipFile = new File(getZipName());
    5.35 +            return createJarUri(zipFile, entry.getName());
    5.36          }
    5.37  
    5.38          @Override
     6.1 --- a/src/share/classes/com/sun/tools/javac/file/ZipFileIndexArchive.java	Thu Sep 03 18:34:17 2009 -0700
     6.2 +++ b/src/share/classes/com/sun/tools/javac/file/ZipFileIndexArchive.java	Tue Sep 08 11:12:13 2009 -0700
     6.3 @@ -72,7 +72,7 @@
     6.4      public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
     6.5          RelativeFile fullZipFileName = new RelativeFile(subdirectory, file);
     6.6          ZipFileIndex.Entry entry = zfIndex.getZipIndexEntry(fullZipFileName);
     6.7 -        JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile().getPath());
     6.8 +        JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile());
     6.9          return ret;
    6.10      }
    6.11  
    6.12 @@ -84,6 +84,7 @@
    6.13          zfIndex.close();
    6.14      }
    6.15  
    6.16 +    @Override
    6.17      public String toString() {
    6.18          return "ZipFileIndexArchive[" + zfIndex + "]";
    6.19      }
    6.20 @@ -111,10 +112,10 @@
    6.21  
    6.22          /** The name of the zip file where this entry resides.
    6.23           */
    6.24 -        String zipName;
    6.25 +        File zipName;
    6.26  
    6.27  
    6.28 -        ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndex.Entry entry, String zipFileName) {
    6.29 +        ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndex.Entry entry, File zipFileName) {
    6.30              super(fileManager);
    6.31              this.name = entry.getFileName();
    6.32              this.zfIndex = zfIndex;
    6.33 @@ -130,6 +131,7 @@
    6.34              return inputStream;
    6.35          }
    6.36  
    6.37 +        @Override
    6.38          protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
    6.39              return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
    6.40          }
    6.41 @@ -157,6 +159,7 @@
    6.42  
    6.43          /** @deprecated see bug 6410637 */
    6.44          @Deprecated
    6.45 +        @Override
    6.46          public String getPath() {
    6.47              return zipName + "(" + entry.getName() + ")";
    6.48          }
    6.49 @@ -183,7 +186,7 @@
    6.50          }
    6.51  
    6.52          public String getZipName() {
    6.53 -            return zipName;
    6.54 +            return zipName.getPath();
    6.55          }
    6.56  
    6.57          public String getZipEntryName() {
    6.58 @@ -191,9 +194,10 @@
    6.59          }
    6.60  
    6.61          public URI toUri() {
    6.62 -            String zipName = new File(getZipName()).toURI().normalize().getPath();
    6.63 -            String entryName = getZipEntryName();
    6.64 -            return URI.create("jar:" + zipName + "!" + entryName);
    6.65 +            if (zfIndex.symbolFilePrefix != null)
    6.66 +                return createJarUri(zipName, zfIndex.symbolFilePrefix.path + entry.getName());
    6.67 +            else
    6.68 +                return createJarUri(zipName, entry.getName());
    6.69          }
    6.70  
    6.71          private byte[] read() throws IOException {
     7.1 --- a/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Thu Sep 03 18:34:17 2009 -0700
     7.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/ClassReader.java	Tue Sep 08 11:12:13 2009 -0700
     7.3 @@ -27,6 +27,7 @@
     7.4  
     7.5  import java.io.*;
     7.6  import java.net.URI;
     7.7 +import java.net.URISyntaxException;
     7.8  import java.nio.CharBuffer;
     7.9  import java.util.EnumSet;
    7.10  import java.util.HashMap;
    7.11 @@ -2614,7 +2615,11 @@
    7.12          }
    7.13  
    7.14          public URI toUri() {
    7.15 -            return URI.create(name.toString());
    7.16 +            try {
    7.17 +                return new URI(null, name.toString(), null);
    7.18 +            } catch (URISyntaxException e) {
    7.19 +                throw new CannotCreateUriError(name.toString(), e);
    7.20 +            }
    7.21          }
    7.22  
    7.23          @Override
     8.1 --- a/src/share/classes/com/sun/tools/javadoc/ClassDocImpl.java	Thu Sep 03 18:34:17 2009 -0700
     8.2 +++ b/src/share/classes/com/sun/tools/javadoc/ClassDocImpl.java	Tue Sep 08 11:12:13 2009 -0700
     8.3 @@ -297,7 +297,7 @@
     8.4                          po instanceof SourcePositionImpl) {
     8.5                      URI uri = ((SourcePositionImpl) po).filename.toUri();
     8.6                      if ("file".equals(uri.getScheme())) {
     8.7 -                        File f = new File(uri.getPath());
     8.8 +                        File f = new File(uri);
     8.9                          File dir = f.getParentFile();
    8.10                          if (dir != null) {
    8.11                              File pf = new File(dir, "package.html");
     9.1 --- a/test/tools/javac/Diagnostics/6769027/tester.properties	Thu Sep 03 18:34:17 2009 -0700
     9.2 +++ b/test/tools/javac/Diagnostics/6769027/tester.properties	Tue Sep 08 11:12:13 2009 -0700
     9.3 @@ -3,11 +3,11 @@
     9.4  
     9.5  compiler.err.double=\
     9.6      This is a test error message.\n\
     9.7 -	This is another line of the above error message {0}
     9.8 +        This is another line of the above error message {0}
     9.9  
    9.10  compiler.misc.single=\
    9.11      This is a test subdiagnostic {0}
    9.12  
    9.13  compiler.misc.double=\
    9.14      This is a test subdiagnostic.\n\
    9.15 -	This is another line of the above subdiagnostic {0}
    9.16 +        This is another line of the above subdiagnostic {0}
    10.1 --- a/test/tools/javac/api/6440333/T6440333.java	Thu Sep 03 18:34:17 2009 -0700
    10.2 +++ b/test/tools/javac/api/6440333/T6440333.java	Tue Sep 08 11:12:13 2009 -0700
    10.3 @@ -26,7 +26,6 @@
    10.4   * @bug     6440333
    10.5   * @summary SimpleJavaFileObject.toString() generates URI with some extra message
    10.6   * @author  Peter von der Ah\u00e9
    10.7 - * @ignore 6877223 test ignored because of issues with File.toUri on Windows (6877206)
    10.8   * @library ../lib
    10.9   * @compile T6440333.java
   10.10   * @run main T6440333
   10.11 @@ -34,6 +33,7 @@
   10.12  
   10.13  import java.io.File;
   10.14  import java.io.IOException;
   10.15 +import java.net.URI;
   10.16  import javax.tools.JavaFileObject;
   10.17  
   10.18  public class T6440333 extends ToolTester {
   10.19 @@ -41,14 +41,10 @@
   10.20          File path = test_src.getCanonicalFile();
   10.21          File src = new File(new File(path, "."), "T6440333.java");
   10.22          JavaFileObject fo = fm.getJavaFileObjects(src).iterator().next();
   10.23 -        String expect = src.getCanonicalFile().getPath().replace(File.separatorChar, '/');
   10.24 +        URI expect = src.getCanonicalFile().toURI();
   10.25          System.err.println("Expect " + expect);
   10.26 -        // CURRENTLY, the following line fails on Windows because a file C:/w/jjg/...
   10.27 -        // returns a URI file://C/w/jjg... which incorrectly encodes the drive letter
   10.28 -        // in the URI authority.   This is against the spec that the authority is
   10.29 -        // undefined and breaks the contract that new File(f.toURI()).equals(f.getAbsoluteFile())
   10.30 -        System.err.println("Got: " +  fo.toUri().getPath());
   10.31 -        if (!expect.equals(fo.toUri().getPath())) {
   10.32 +        System.err.println("Found  " + fo.toUri());
   10.33 +        if (!expect.equals(fo.toUri())) {
   10.34              throw new AssertionError();
   10.35          }
   10.36      }
    11.1 --- a/test/tools/javac/api/Sibling.java	Thu Sep 03 18:34:17 2009 -0700
    11.2 +++ b/test/tools/javac/api/Sibling.java	Tue Sep 08 11:12:13 2009 -0700
    11.3 @@ -26,7 +26,6 @@
    11.4   * @bug     6399602
    11.5   * @summary Verify that files are created relative to sibling
    11.6   * @author  Peter von der Ah\u00e9
    11.7 - * @ignore 6877223 test ignored because of issues with File.toUri on Windows (6877206)
    11.8   */
    11.9  
   11.10  import java.io.File;
   11.11 @@ -48,10 +47,9 @@
   11.12                                                              "foo.bar.baz.Test",
   11.13                                                              CLASS,
   11.14                                                              sibling);
   11.15 -        String name =
   11.16 -            new File("Test.class").getAbsolutePath().replace(File.separatorChar, '/');
   11.17 -        if (!classFile.toUri().getPath().equals(name))
   11.18 -            throw new AssertionError("Expected " + name + ", got " +
   11.19 -                                     classFile.toUri().getPath());
   11.20 +        File file = new File("Test.class").getAbsoluteFile();
   11.21 +        if (!classFile.toUri().equals(file.toURI()))
   11.22 +            throw new AssertionError("Expected " + file.toURI() + ", got " +
   11.23 +                                     classFile.toUri());
   11.24      }
   11.25  }
    12.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2 +++ b/test/tools/javac/api/T6483788.java	Tue Sep 08 11:12:13 2009 -0700
    12.3 @@ -0,0 +1,70 @@
    12.4 +/*
    12.5 + * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
    12.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    12.7 + *
    12.8 + * This code is free software; you can redistribute it and/or modify it
    12.9 + * under the terms of the GNU General Public License version 2 only, as
   12.10 + * published by the Free Software Foundation.
   12.11 + *
   12.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
   12.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   12.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   12.15 + * version 2 for more details (a copy is included in the LICENSE file that
   12.16 + * accompanied this code).
   12.17 + *
   12.18 + * You should have received a copy of the GNU General Public License version
   12.19 + * 2 along with this work; if not, write to the Free Software Foundation,
   12.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   12.21 + *
   12.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   12.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
   12.24 + * have any questions.
   12.25 + */
   12.26 +
   12.27 +/*
   12.28 + * @test
   12.29 + * @bug 6483788
   12.30 + * @summary DefaultFileManager.ZipFileObject.toUri() fails to escape space characters
   12.31 + */
   12.32 +
   12.33 +import java.io.*;
   12.34 +import java.net.*;
   12.35 +import java.util.Collections;
   12.36 +import java.util.jar.*;
   12.37 +import java.util.zip.*;
   12.38 +import javax.tools.*;
   12.39 +
   12.40 +public class T6483788 {
   12.41 +    public static void main(String[] args) throws Exception {
   12.42 +        new T6483788().run();
   12.43 +    }
   12.44 +
   12.45 +    void run() throws Exception {
   12.46 +        File jar = createJar();
   12.47 +        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
   12.48 +        StandardJavaFileManager fm = c.getStandardFileManager(null, null, null);
   12.49 +        fm.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(jar));
   12.50 +        JavaFileObject fo = fm.getJavaFileForInput(StandardLocation.CLASS_PATH, "dummy", JavaFileObject.Kind.CLASS);
   12.51 +        System.err.println("file: " + fo);
   12.52 +        URI uri = fo.toUri();
   12.53 +        System.err.println("uri: " + uri);
   12.54 +        if (uri.toString().contains(" "))
   12.55 +            throw new Exception("unexpected space character found");
   12.56 +    }
   12.57 +
   12.58 +    File createJar() throws IOException {
   12.59 +        byte[] dummy_data = new byte[10];
   12.60 +        File f = new File("a b.jar");
   12.61 +        OutputStream out = new FileOutputStream(f);
   12.62 +        try {
   12.63 +            JarOutputStream jar = new JarOutputStream(out);
   12.64 +            jar.putNextEntry(new ZipEntry("dummy.class"));
   12.65 +            jar.write(dummy_data);
   12.66 +            jar.close();
   12.67 +        } finally {
   12.68 +            out.close();
   12.69 +        }
   12.70 +        return f;
   12.71 +    }
   12.72 +}
   12.73 +
    13.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2 +++ b/test/tools/javac/api/T6501502.java	Tue Sep 08 11:12:13 2009 -0700
    13.3 @@ -0,0 +1,72 @@
    13.4 +/*
    13.5 + * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
    13.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    13.7 + *
    13.8 + * This code is free software; you can redistribute it and/or modify it
    13.9 + * under the terms of the GNU General Public License version 2 only, as
   13.10 + * published by the Free Software Foundation.
   13.11 + *
   13.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
   13.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   13.15 + * version 2 for more details (a copy is included in the LICENSE file that
   13.16 + * accompanied this code).
   13.17 + *
   13.18 + * You should have received a copy of the GNU General Public License version
   13.19 + * 2 along with this work; if not, write to the Free Software Foundation,
   13.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   13.21 + *
   13.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   13.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
   13.24 + * have any questions.
   13.25 + */
   13.26 +
   13.27 +/*
   13.28 + * @test
   13.29 + * @bug 6501502 6877206 6483788
   13.30 + * @summary JSR 199: FileObject.toUri should return file:///c:/ or file:/c:/ not file://c:/
   13.31 + */
   13.32 +
   13.33 +import java.io.*;
   13.34 +import java.net.URI;
   13.35 +import javax.tools.*;
   13.36 +
   13.37 +public class T6501502 {
   13.38 +    public static void main(String... args) throws Exception {
   13.39 +        new T6501502().run();
   13.40 +    }
   13.41 +
   13.42 +    // The spec for java.io.File includes the following:
   13.43 +    //      For a given abstract pathname f it is guaranteed that
   13.44 +    //          new File( f.toURI()).equals( f.getAbsoluteFile())
   13.45 +    // For JavaFileObject we test as follows:
   13.46 +    //      new File( CONVERT_TO_FILEOBJECT(f).toURI()).equals( f.getAbsoluteFile())
   13.47 +    // to verify that we get reasonable URIs returned from toURI.
   13.48 +    // To make this a general test, and not just a Windows test,
   13.49 +    // we test a number of platform-independent paths.
   13.50 +    void run() throws Exception {
   13.51 +        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
   13.52 +        fm = c.getStandardFileManager(null, null, null);
   13.53 +        System.err.println(System.getProperties());
   13.54 +        File tmpDir = new File(System.getProperty("java.io.tmpdir"));
   13.55 +        File testSrcDir = new File(System.getProperty("test.src"));
   13.56 +        File testClassesDir = new File(System.getProperty("test.classes"));
   13.57 +        test(new File("abc.tmp"));
   13.58 +        test(new File(tmpDir, "bad.file"));
   13.59 +        test(new File(testSrcDir, "T6501501.java"));
   13.60 +        test(new File(testClassesDir, "T6501501.class"));
   13.61 +        test(new File("a b"));
   13.62 +    }
   13.63 +
   13.64 +    void test(File f) throws Exception {
   13.65 +        System.err.println("test " + f);
   13.66 +        FileObject fo = fm.getJavaFileObjects(f).iterator().next();
   13.67 +        URI uri = fo.toUri();
   13.68 +        System.err.println("FileObject uri: " + uri);
   13.69 +        if (!new File(uri).equals(f.getAbsoluteFile()))
   13.70 +            throw new Exception("unexpected URI returned");
   13.71 +    }
   13.72 +
   13.73 +    StandardJavaFileManager fm;
   13.74 +}
   13.75 +
    14.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2 +++ b/test/tools/javac/api/T6877206.java	Tue Sep 08 11:12:13 2009 -0700
    14.3 @@ -0,0 +1,263 @@
    14.4 +/*
    14.5 + * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
    14.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    14.7 + *
    14.8 + * This code is free software; you can redistribute it and/or modify it
    14.9 + * under the terms of the GNU General Public License version 2 only, as
   14.10 + * published by the Free Software Foundation.
   14.11 + *
   14.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
   14.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   14.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14.15 + * version 2 for more details (a copy is included in the LICENSE file that
   14.16 + * accompanied this code).
   14.17 + *
   14.18 + * You should have received a copy of the GNU General Public License version
   14.19 + * 2 along with this work; if not, write to the Free Software Foundation,
   14.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   14.21 + *
   14.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   14.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
   14.24 + * have any questions.
   14.25 + */
   14.26 +
   14.27 +/*
   14.28 + * @test
   14.29 + * @bug 6877206
   14.30 + * @summary JavaFileObject.toUri returns bogus URI (win)
   14.31 + */
   14.32 +
   14.33 +import java.io.*;
   14.34 +import java.net.*;
   14.35 +import java.util.*;
   14.36 +import java.util.jar.*;
   14.37 +import java.util.zip.*;
   14.38 +import javax.tools.*;
   14.39 +
   14.40 +import com.sun.tools.javac.file.JavacFileManager;
   14.41 +import com.sun.tools.javac.util.Context;
   14.42 +import com.sun.tools.javac.util.Options;
   14.43 +
   14.44 +// Test URIs returned from JavacFileManager and its support classes.
   14.45 +// For a variety of file objects, verify the validity of FileObject.toUri()
   14.46 +// by verifying the URI exists and points to the same contents as the file
   14.47 +// object itself
   14.48 +
   14.49 +public class T6877206 {
   14.50 +    public static void main(String... args) throws Exception {
   14.51 +        new T6877206().run();
   14.52 +    }
   14.53 +
   14.54 +    Set<String> foundClasses = new TreeSet<String>();
   14.55 +    Set<String> foundJars = new TreeSet<String>();
   14.56 +
   14.57 +    void run() throws Exception {
   14.58 +        File rt_jar = findRtJar();
   14.59 +
   14.60 +        // names for entries to be created in directories and jar files
   14.61 +        String[] entries = { "p/A.class", "p/resources/A-1.jpg" };
   14.62 +
   14.63 +        // test various combinations of directories and jar files, intended to
   14.64 +        // cover all sources of URIs within JavacFileManager's support classes
   14.65 +
   14.66 +        test(createFileManager(), createDir("dir", entries), "p", entries.length);
   14.67 +        test(createFileManager(), createDir("a b/dir", entries), "p", entries.length);
   14.68 +
   14.69 +        for (boolean useJavaUtilZip: new boolean[] { false, true }) {
   14.70 +            test(createFileManager(useJavaUtilZip), createJar("jar", entries), "p", entries.length);
   14.71 +            test(createFileManager(useJavaUtilZip), createJar("jar jar", entries), "p", entries.length);
   14.72 +
   14.73 +            for (boolean useSymbolFile: new boolean[] { false, true }) {
   14.74 +                test(createFileManager(useJavaUtilZip, useSymbolFile), rt_jar, "java.lang.ref", -1);
   14.75 +            }
   14.76 +        }
   14.77 +
   14.78 +        // Verify that we hit all the impl classes we intended
   14.79 +        checkCoverage("classes", foundClasses,
   14.80 +                "RegularFileObject", "SymbolFileObject", "ZipFileIndexFileObject", "ZipFileObject");
   14.81 +
   14.82 +        // Verify that we hit the jar files we intended, specifically ct.sym as well as rt.jar
   14.83 +        checkCoverage("jar files", foundJars,
   14.84 +                "ct.sym", "jar", "jar jar", "rt.jar");
   14.85 +    }
   14.86 +
   14.87 +    // use a new file manager for each test
   14.88 +    void test(StandardJavaFileManager fm, File f, String pkg, int expect) throws Exception {
   14.89 +        JarURLConnection c;
   14.90 +        System.err.println("Test " + f);
   14.91 +        try {
   14.92 +            fm.setLocation(StandardLocation.CLASS_PATH, Collections.singleton(f));
   14.93 +
   14.94 +            int count = 0;
   14.95 +            for (JavaFileObject fo: fm.list(StandardLocation.CLASS_PATH,
   14.96 +                    pkg, EnumSet.allOf(JavaFileObject.Kind.class), true)) {
   14.97 +                System.err.println("checking " + fo);
   14.98 +                // record the file object class name for coverage checks later
   14.99 +                foundClasses.add(fo.getClass().getSimpleName());
  14.100 +                testFileObject(fo);
  14.101 +                count++;
  14.102 +            }
  14.103 +
  14.104 +            if (expect > 0 && count != expect)
  14.105 +                throw new Exception("wrong number of entries found: "
  14.106 +                        + count + ", expected " + expect);
  14.107 +        } finally {
  14.108 +            fm.close();
  14.109 +        }
  14.110 +    }
  14.111 +
  14.112 +    void testFileObject(JavaFileObject fo) throws Exception {
  14.113 +        // test the validity of the result of toUri() by using URLConnection
  14.114 +        // and comparing the results of reading from the connection with the
  14.115 +        // result of reading from the file object directly.
  14.116 +        URI uri = fo.toUri();
  14.117 +        System.err.println("uri: " + uri);
  14.118 +
  14.119 +        URLConnection urlconn = uri.toURL().openConnection();
  14.120 +        if (urlconn instanceof JarURLConnection) {
  14.121 +            JarURLConnection jarconn = (JarURLConnection) urlconn;
  14.122 +            File f = new File(jarconn.getJarFile().getName());
  14.123 +            // record access to the jar file for coverage checks later
  14.124 +            foundJars.add(f.getName());
  14.125 +        }
  14.126 +
  14.127 +        try {
  14.128 +            byte[] uriData = read(urlconn.getInputStream());
  14.129 +            byte[] foData = read(fo.openInputStream());
  14.130 +            if (!Arrays.equals(uriData, foData)) {
  14.131 +                if (uriData.length != foData.length)
  14.132 +                    throw new Exception("data size differs: uri data "
  14.133 +                            + uriData.length + " bytes, fo data " + foData.length+ " bytes");
  14.134 +                for (int i = 0; i < uriData.length; i++) {
  14.135 +                    if (uriData[i] != foData[i])
  14.136 +                    throw new Exception("unexpected data returned at offset " + i
  14.137 +                            + ", uri data " + uriData[i] + ", fo data " + foData[i]);
  14.138 +                }
  14.139 +                throw new AssertionError("cannot find difference");
  14.140 +            }
  14.141 +        } finally {
  14.142 +            // In principle, simply closing the result of urlconn.getInputStream()
  14.143 +            // should have been sufficient. But the internal JarURLConnection
  14.144 +            // does not close the JarFile in an expeditious manner, thus preventing
  14.145 +            // jtreg from deleting the jar file before starting the next test.
  14.146 +            // Therefore we force access to the JarURLConnection to close the
  14.147 +            // JarFile when necessary.
  14.148 +            if (urlconn instanceof JarURLConnection) {
  14.149 +                JarURLConnection jarconn = (JarURLConnection) urlconn;
  14.150 +                jarconn.getJarFile().close();
  14.151 +            }
  14.152 +        }
  14.153 +    }
  14.154 +
  14.155 +    void checkCoverage(String label, Set<String> found, String... expect) throws Exception {
  14.156 +        Set<String> e = new TreeSet<String>(Arrays.asList(expect));
  14.157 +        if (!found.equals(e)) {
  14.158 +            e.removeAll(found);
  14.159 +            throw new Exception("expected " + label + " not used: " + e);
  14.160 +        }
  14.161 +    }
  14.162 +
  14.163 +    JavacFileManager createFileManager() {
  14.164 +        return createFileManager(false, false);
  14.165 +    }
  14.166 +
  14.167 +    JavacFileManager createFileManager(boolean useJavaUtilZip) {
  14.168 +        return createFileManager(useJavaUtilZip, false);
  14.169 +    }
  14.170 +
  14.171 +    JavacFileManager createFileManager(boolean useJavaUtilZip, boolean useSymbolFile) {
  14.172 +        // javac should really not be using system properties like this
  14.173 +        // -- it should really be using (hidden) options -- but until then
  14.174 +        // take care to leave system properties as we find them, so as not
  14.175 +        // to adversely affect other tests that might follow.
  14.176 +        String prev = System.getProperty("useJavaUtilZip");
  14.177 +        boolean resetProperties = false;
  14.178 +        try {
  14.179 +            if (useJavaUtilZip) {
  14.180 +                System.setProperty("useJavaUtilZip", "true");
  14.181 +                resetProperties = true;
  14.182 +            } else if (System.getProperty("useJavaUtilZip") != null) {
  14.183 +                System.getProperties().remove("useJavaUtilZip");
  14.184 +                resetProperties = true;
  14.185 +            }
  14.186 +
  14.187 +            Context c = new Context();
  14.188 +            if (!useSymbolFile) {
  14.189 +                Options options = Options.instance(c);
  14.190 +                options.put("ignore.symbol.file", "true");
  14.191 +            }
  14.192 +
  14.193 +            return new JavacFileManager(c, false, null);
  14.194 +        } finally {
  14.195 +            if (resetProperties) {
  14.196 +                if (prev == null) {
  14.197 +                    System.getProperties().remove("useJavaUtilZip");
  14.198 +                } else {
  14.199 +                    System.setProperty("useJavaUtilZip", prev);
  14.200 +                }
  14.201 +            }
  14.202 +        }
  14.203 +    }
  14.204 +
  14.205 +    File createDir(String name, String... entries) throws Exception {
  14.206 +        File dir = new File(name);
  14.207 +        if (!dir.mkdirs())
  14.208 +            throw new Exception("cannot create directories " + dir);
  14.209 +        for (String e: entries) {
  14.210 +            writeFile(new File(dir, e), e);
  14.211 +        }
  14.212 +        return dir;
  14.213 +    }
  14.214 +
  14.215 +    File createJar(String name, String... entries) throws IOException {
  14.216 +        File jar = new File(name);
  14.217 +        OutputStream out = new FileOutputStream(jar);
  14.218 +        try {
  14.219 +            JarOutputStream jos = new JarOutputStream(out);
  14.220 +            for (String e: entries) {
  14.221 +                jos.putNextEntry(new ZipEntry(e));
  14.222 +                jos.write(e.getBytes());
  14.223 +            }
  14.224 +            jos.close();
  14.225 +        } finally {
  14.226 +            out.close();
  14.227 +        }
  14.228 +        return jar;
  14.229 +    }
  14.230 +
  14.231 +    File findRtJar() throws Exception {
  14.232 +        File java_home = new File(System.getProperty("java.home"));
  14.233 +        if (java_home.getName().equals("jre"))
  14.234 +            java_home = java_home.getParentFile();
  14.235 +        File rt_jar = new File(new File(new File(java_home, "jre"), "lib"), "rt.jar");
  14.236 +        if (!rt_jar.exists())
  14.237 +            throw new Exception("can't find rt.jar");
  14.238 +        return rt_jar;
  14.239 +    }
  14.240 +
  14.241 +    byte[] read(InputStream in) throws IOException {
  14.242 +        byte[] data = new byte[1024];
  14.243 +        int offset = 0;
  14.244 +        try {
  14.245 +            int n;
  14.246 +            while ((n = in.read(data, offset, data.length - offset)) != -1) {
  14.247 +                offset += n;
  14.248 +                if (offset == data.length)
  14.249 +                    data = Arrays.copyOf(data, 2 * data.length);
  14.250 +            }
  14.251 +        } finally {
  14.252 +            in.close();
  14.253 +        }
  14.254 +        return Arrays.copyOf(data, offset);
  14.255 +    }
  14.256 +
  14.257 +    void writeFile(File f, String s) throws IOException {
  14.258 +        f.getParentFile().mkdirs();
  14.259 +        FileWriter out = new FileWriter(f);
  14.260 +        try {
  14.261 +            out.write(s);
  14.262 +        } finally {
  14.263 +            out.close();
  14.264 +        }
  14.265 +    }
  14.266 +}

mercurial