src/share/classes/com/sun/tools/javac/file/JavacFileManager.java

changeset 103
e571266ae14f
parent 62
07c916ecfc71
child 106
ceaa6549687a
     1.1 --- a/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Fri Aug 22 11:46:29 2008 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java	Tue Aug 26 14:52:59 2008 -0700
     1.3 @@ -65,6 +65,8 @@
     1.4  import javax.tools.StandardJavaFileManager;
     1.5  
     1.6  import com.sun.tools.javac.code.Source;
     1.7 +import com.sun.tools.javac.file.RelativePath.RelativeFile;
     1.8 +import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
     1.9  import com.sun.tools.javac.main.JavacOption;
    1.10  import com.sun.tools.javac.main.OptionName;
    1.11  import com.sun.tools.javac.main.RecognizedOptions;
    1.12 @@ -75,8 +77,8 @@
    1.13  import com.sun.tools.javac.util.Log;
    1.14  import com.sun.tools.javac.util.Options;
    1.15  
    1.16 +import static javax.tools.StandardLocation.*;
    1.17  import static com.sun.tools.javac.main.OptionName.*;
    1.18 -import static javax.tools.StandardLocation.*;
    1.19  
    1.20  /**
    1.21   * This class provides access to the source, class and other files
    1.22 @@ -84,9 +86,6 @@
    1.23   */
    1.24  public class JavacFileManager implements StandardJavaFileManager {
    1.25  
    1.26 -    private static final String[] symbolFileLocation = { "lib", "ct.sym" };
    1.27 -    private static final String symbolFilePrefix = "META-INF/sym/rt.jar/";
    1.28 -
    1.29      boolean useZipFileIndex;
    1.30  
    1.31      private static boolean CHECK_ZIP_TIMESTAMP = false;
    1.32 @@ -267,6 +266,7 @@
    1.33              printAscii("Invalid class name: \"%s\"", name);
    1.34          }
    1.35      }
    1.36 +
    1.37      private static void printAscii(String format, Object... args) {
    1.38          String message;
    1.39          try {
    1.40 @@ -278,27 +278,12 @@
    1.41          System.out.println(message);
    1.42      }
    1.43  
    1.44 -    /** Return external representation of name,
    1.45 -     *  converting '.' to File.separatorChar.
    1.46 -     */
    1.47 -    private static String externalizeFileName(CharSequence name) {
    1.48 -        return name.toString().replace('.', File.separatorChar);
    1.49 -    }
    1.50 -
    1.51 -    private static String externalizeFileName(CharSequence n, JavaFileObject.Kind kind) {
    1.52 -        return externalizeFileName(n) + kind.extension;
    1.53 -    }
    1.54 -
    1.55 -    private static String baseName(String fileName) {
    1.56 -        return fileName.substring(fileName.lastIndexOf(File.separatorChar) + 1);
    1.57 -    }
    1.58 -
    1.59      /**
    1.60       * Insert all files in subdirectory `subdirectory' of `directory' which end
    1.61       * in one of the extensions in `extensions' into packageSym.
    1.62       */
    1.63      private void listDirectory(File directory,
    1.64 -                               String subdirectory,
    1.65 +                               RelativeDirectory subdirectory,
    1.66                                 Set<JavaFileObject.Kind> fileKinds,
    1.67                                 boolean recurse,
    1.68                                 ListBuffer<JavaFileObject> l) {
    1.69 @@ -329,22 +314,6 @@
    1.70                      return;
    1.71                  }
    1.72              }
    1.73 -            if (subdirectory.length() != 0) {
    1.74 -                if (!useZipFileIndex) {
    1.75 -                    subdirectory = subdirectory.replace('\\', '/');
    1.76 -                    if (!subdirectory.endsWith("/")) subdirectory = subdirectory + "/";
    1.77 -                }
    1.78 -                else {
    1.79 -                    if (File.separatorChar == '/') {
    1.80 -                        subdirectory = subdirectory.replace('\\', '/');
    1.81 -                    }
    1.82 -                    else {
    1.83 -                        subdirectory = subdirectory.replace('/', '\\');
    1.84 -                    }
    1.85 -
    1.86 -                    if (!subdirectory.endsWith(File.separator)) subdirectory = subdirectory + File.separator;
    1.87 -                }
    1.88 -            }
    1.89  
    1.90              List<String> files = archive.getFiles(subdirectory);
    1.91              if (files != null) {
    1.92 @@ -356,8 +325,8 @@
    1.93                  }
    1.94              }
    1.95              if (recurse) {
    1.96 -                for (String s: archive.getSubdirectories()) {
    1.97 -                    if (s.startsWith(subdirectory) && !s.equals(subdirectory)) {
    1.98 +                for (RelativeDirectory s: archive.getSubdirectories()) {
    1.99 +                    if (subdirectory.contains(s)) {
   1.100                          // Because the archive map is a flat list of directories,
   1.101                          // the enclosing loop will pick up all child subdirectories.
   1.102                          // Therefore, there is no need to recurse deeper.
   1.103 @@ -366,9 +335,7 @@
   1.104                  }
   1.105              }
   1.106          } else {
   1.107 -            File d = subdirectory.length() != 0
   1.108 -                ? new File(directory, subdirectory)
   1.109 -                : directory;
   1.110 +            File d = subdirectory.getFile(directory);
   1.111              if (!caseMapCheck(d, subdirectory))
   1.112                  return;
   1.113  
   1.114 @@ -381,7 +348,7 @@
   1.115                  if (f.isDirectory()) {
   1.116                      if (recurse && SourceVersion.isIdentifier(fname)) {
   1.117                          listDirectory(directory,
   1.118 -                                      subdirectory + File.separator + fname,
   1.119 +                                      new RelativeDirectory(subdirectory, fname),
   1.120                                        fileKinds,
   1.121                                        recurse,
   1.122                                        l);
   1.123 @@ -411,7 +378,7 @@
   1.124       *  ends in a string of characters with the same case as given name.
   1.125       *  Ignore file separators in both path and name.
   1.126       */
   1.127 -    private boolean caseMapCheck(File f, String name) {
   1.128 +    private boolean caseMapCheck(File f, RelativePath name) {
   1.129          if (fileSystemIsCaseSensitive) return true;
   1.130          // Note that getCanonicalPath() returns the case-sensitive
   1.131          // spelled file name.
   1.132 @@ -422,12 +389,12 @@
   1.133              return false;
   1.134          }
   1.135          char[] pcs = path.toCharArray();
   1.136 -        char[] ncs = name.toCharArray();
   1.137 +        char[] ncs = name.path.toCharArray();
   1.138          int i = pcs.length - 1;
   1.139          int j = ncs.length - 1;
   1.140          while (i >= 0 && j >= 0) {
   1.141              while (i >= 0 && pcs[i] == File.separatorChar) i--;
   1.142 -            while (j >= 0 && ncs[j] == File.separatorChar) j--;
   1.143 +            while (j >= 0 && ncs[j] == '/') j--;
   1.144              if (i >= 0 && j >= 0) {
   1.145                  if (pcs[i] != ncs[j]) return false;
   1.146                  i--;
   1.147 @@ -444,13 +411,13 @@
   1.148      public interface Archive {
   1.149          void close() throws IOException;
   1.150  
   1.151 -        boolean contains(String name);
   1.152 +        boolean contains(RelativePath name);
   1.153  
   1.154 -        JavaFileObject getFileObject(String subdirectory, String file);
   1.155 +        JavaFileObject getFileObject(RelativeDirectory subdirectory, String file);
   1.156  
   1.157 -        List<String> getFiles(String subdirectory);
   1.158 +        List<String> getFiles(RelativeDirectory subdirectory);
   1.159  
   1.160 -        Set<String> getSubdirectories();
   1.161 +        Set<RelativeDirectory> getSubdirectories();
   1.162      }
   1.163  
   1.164      public class MissingArchive implements Archive {
   1.165 @@ -458,30 +425,38 @@
   1.166          public MissingArchive(File name) {
   1.167              zipFileName = name;
   1.168          }
   1.169 -        public boolean contains(String name) {
   1.170 +        public boolean contains(RelativePath name) {
   1.171              return false;
   1.172          }
   1.173  
   1.174          public void close() {
   1.175          }
   1.176  
   1.177 -        public JavaFileObject getFileObject(String subdirectory, String file) {
   1.178 +        public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
   1.179              return null;
   1.180          }
   1.181  
   1.182 -        public List<String> getFiles(String subdirectory) {
   1.183 +        public List<String> getFiles(RelativeDirectory subdirectory) {
   1.184              return List.nil();
   1.185          }
   1.186  
   1.187 -        public Set<String> getSubdirectories() {
   1.188 +        public Set<RelativeDirectory> getSubdirectories() {
   1.189              return Collections.emptySet();
   1.190          }
   1.191 +
   1.192 +        public String toString() {
   1.193 +            return "MissingArchive[" + zipFileName + "]";
   1.194 +        }
   1.195      }
   1.196  
   1.197      /** A directory of zip files already opened.
   1.198       */
   1.199      Map<File, Archive> archives = new HashMap<File,Archive>();
   1.200  
   1.201 +    private static final String[] symbolFileLocation = { "lib", "ct.sym" };
   1.202 +    private static final RelativeDirectory symbolFilePrefix
   1.203 +            = new RelativeDirectory("META-INF/sym/rt.jar/");
   1.204 +
   1.205      /** Open a new zip file directory.
   1.206       */
   1.207      protected Archive openArchive(File zipFileName) throws IOException {
   1.208 @@ -540,8 +515,12 @@
   1.209                      if (!useZipFileIndex) {
   1.210                          archive = new ZipArchive(this, zdir);
   1.211                      } else {
   1.212 -                        archive = new ZipFileIndexArchive(this, ZipFileIndex.getZipFileIndex(zipFileName, null,
   1.213 -                                usePreindexedCache, preindexCacheLocation, options.get("writezipindexfiles") != null));
   1.214 +                        archive = new ZipFileIndexArchive(this,
   1.215 +                                ZipFileIndex.getZipFileIndex(zipFileName,
   1.216 +                                    null,
   1.217 +                                    usePreindexedCache,
   1.218 +                                    preindexCacheLocation,
   1.219 +                                    options.get("writezipindexfiles") != null));
   1.220                      }
   1.221                  }
   1.222                  else {
   1.223 @@ -551,10 +530,10 @@
   1.224                      else {
   1.225                          archive = new ZipFileIndexArchive(this,
   1.226                                  ZipFileIndex.getZipFileIndex(zipFileName,
   1.227 -                                symbolFilePrefix,
   1.228 -                                usePreindexedCache,
   1.229 -                                preindexCacheLocation,
   1.230 -                                options.get("writezipindexfiles") != null));
   1.231 +                                    symbolFilePrefix,
   1.232 +                                    usePreindexedCache,
   1.233 +                                    preindexCacheLocation,
   1.234 +                                    options.get("writezipindexfiles") != null));
   1.235                      }
   1.236                  }
   1.237              } catch (FileNotFoundException ex) {
   1.238 @@ -796,7 +775,7 @@
   1.239          Iterable<? extends File> path = getLocation(location);
   1.240          if (path == null)
   1.241              return List.nil();
   1.242 -        String subdirectory = externalizeFileName(packageName);
   1.243 +        RelativeDirectory subdirectory = RelativeDirectory.forPackage(packageName);
   1.244          ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>();
   1.245  
   1.246          for (File directory : path)
   1.247 @@ -877,7 +856,7 @@
   1.248          nullCheck(kind);
   1.249          if (!sourceOrClass.contains(kind))
   1.250              throw new IllegalArgumentException("Invalid kind " + kind);
   1.251 -        return getFileForInput(location, externalizeFileName(className, kind));
   1.252 +        return getFileForInput(location, RelativeFile.forClass(className, kind));
   1.253      }
   1.254  
   1.255      public FileObject getFileForInput(Location location,
   1.256 @@ -890,35 +869,32 @@
   1.257          nullCheck(packageName);
   1.258          if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701
   1.259              throw new IllegalArgumentException("Invalid relative name: " + relativeName);
   1.260 -        String name = packageName.length() == 0
   1.261 -            ? relativeName
   1.262 -            : new File(externalizeFileName(packageName), relativeName).getPath();
   1.263 +        RelativeFile name = packageName.length() == 0
   1.264 +            ? new RelativeFile(relativeName)
   1.265 +            : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName);
   1.266          return getFileForInput(location, name);
   1.267      }
   1.268  
   1.269 -    private JavaFileObject getFileForInput(Location location, String name) throws IOException {
   1.270 +    private JavaFileObject getFileForInput(Location location, RelativeFile name) throws IOException {
   1.271          Iterable<? extends File> path = getLocation(location);
   1.272          if (path == null)
   1.273              return null;
   1.274  
   1.275          for (File dir: path) {
   1.276              if (dir.isDirectory()) {
   1.277 -                File f = new File(dir, name.replace('/', File.separatorChar));
   1.278 +                File f = name.getFile(dir);
   1.279                  if (f.exists())
   1.280                      return new RegularFileObject(this, f);
   1.281              } else {
   1.282                  Archive a = openArchive(dir);
   1.283                  if (a.contains(name)) {
   1.284 -                    int i = name.lastIndexOf('/');
   1.285 -                    String dirname = name.substring(0, i+1);
   1.286 -                    String basename = name.substring(i+1);
   1.287 -                    return a.getFileObject(dirname, basename);
   1.288 +                    return a.getFileObject(name.dirname(), name.basename());
   1.289                  }
   1.290  
   1.291              }
   1.292          }
   1.293 +
   1.294          return null;
   1.295 -
   1.296      }
   1.297  
   1.298      public JavaFileObject getJavaFileForOutput(Location location,
   1.299 @@ -933,7 +909,7 @@
   1.300          nullCheck(kind);
   1.301          if (!sourceOrClass.contains(kind))
   1.302              throw new IllegalArgumentException("Invalid kind " + kind);
   1.303 -        return getFileForOutput(location, externalizeFileName(className, kind), sibling);
   1.304 +        return getFileForOutput(location, RelativeFile.forClass(className, kind), sibling);
   1.305      }
   1.306  
   1.307      public FileObject getFileForOutput(Location location,
   1.308 @@ -947,14 +923,14 @@
   1.309          nullCheck(packageName);
   1.310          if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701
   1.311              throw new IllegalArgumentException("relativeName is invalid");
   1.312 -        String name = packageName.length() == 0
   1.313 -            ? relativeName
   1.314 -            : new File(externalizeFileName(packageName), relativeName).getPath();
   1.315 +        RelativeFile name = packageName.length() == 0
   1.316 +            ? new RelativeFile(relativeName)
   1.317 +            : new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName);
   1.318          return getFileForOutput(location, name, sibling);
   1.319      }
   1.320  
   1.321      private JavaFileObject getFileForOutput(Location location,
   1.322 -                                            String fileName,
   1.323 +                                            RelativeFile fileName,
   1.324                                              FileObject sibling)
   1.325          throws IOException
   1.326      {
   1.327 @@ -967,7 +943,7 @@
   1.328                  if (sibling != null && sibling instanceof RegularFileObject) {
   1.329                      siblingDir = ((RegularFileObject)sibling).f.getParentFile();
   1.330                  }
   1.331 -                return new RegularFileObject(this, new File(siblingDir, baseName(fileName)));
   1.332 +                return new RegularFileObject(this, new File(siblingDir, fileName.basename()));
   1.333              }
   1.334          } else if (location == SOURCE_OUTPUT) {
   1.335              dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir());
   1.336 @@ -980,7 +956,7 @@
   1.337              }
   1.338          }
   1.339  
   1.340 -        File file = (dir == null ? new File(fileName) : new File(dir, fileName));
   1.341 +        File file = fileName.getFile(dir); // null-safe
   1.342          return new RegularFileObject(this, file);
   1.343  
   1.344      }

mercurial