langtools/src/share/classes/com/sun/tools/javac/file/Paths.java
changeset 10818 e95eb04c68cc
parent 10817 d91978895fac
child 10819 e1a8df45d8b0
equal deleted inserted replaced
10817:d91978895fac 10818:e95eb04c68cc
     1 /*
       
     2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package com.sun.tools.javac.file;
       
    27 
       
    28 import java.io.File;
       
    29 import java.io.IOException;
       
    30 import java.net.MalformedURLException;
       
    31 import java.net.URL;
       
    32 import java.util.HashMap;
       
    33 import java.util.HashSet;
       
    34 import java.util.Map;
       
    35 import java.util.Set;
       
    36 import java.util.Collection;
       
    37 import java.util.Collections;
       
    38 import java.util.LinkedHashSet;
       
    39 import java.util.StringTokenizer;
       
    40 import java.util.zip.ZipFile;
       
    41 import javax.tools.JavaFileManager.Location;
       
    42 
       
    43 import com.sun.tools.javac.code.Lint;
       
    44 import com.sun.tools.javac.util.ListBuffer;
       
    45 import com.sun.tools.javac.util.Log;
       
    46 import com.sun.tools.javac.util.Options;
       
    47 
       
    48 import static javax.tools.StandardLocation.*;
       
    49 import static com.sun.tools.javac.main.OptionName.*;
       
    50 
       
    51 /** This class converts command line arguments, environment variables
       
    52  *  and system properties (in File.pathSeparator-separated String form)
       
    53  *  into a boot class path, user class path, and source path (in
       
    54  *  Collection<String> form).
       
    55  *
       
    56  *  <p><b>This is NOT part of any supported API.
       
    57  *  If you write code that depends on this, you do so at your own risk.
       
    58  *  This code and its internal interfaces are subject to change or
       
    59  *  deletion without notice.</b>
       
    60  */
       
    61 public class Paths {
       
    62 
       
    63     /** The log to use for warning output */
       
    64     private Log log;
       
    65 
       
    66     /** Collection of command-line options */
       
    67     private Options options;
       
    68 
       
    69     /** Handler for -Xlint options */
       
    70     private Lint lint;
       
    71 
       
    72     /** Access to (possibly cached) file info */
       
    73     private FSInfo fsInfo;
       
    74 
       
    75     public Paths() {
       
    76         pathsForLocation = new HashMap<Location,Path>(16);
       
    77     }
       
    78 
       
    79     public void update(Log log, Options options, Lint lint, FSInfo fsInfo) {
       
    80         this.log = log;
       
    81         this.options = options;
       
    82         this.lint = lint;
       
    83         this.fsInfo = fsInfo;
       
    84     }
       
    85 
       
    86     /** Whether to warn about non-existent path elements */
       
    87     private boolean warn;
       
    88 
       
    89     private Map<Location, Path> pathsForLocation;
       
    90 
       
    91     private boolean inited = false; // TODO? caching bad?
       
    92 
       
    93     /**
       
    94      * rt.jar as found on the default bootclass path.  If the user specified a
       
    95      * bootclasspath, null is used.
       
    96      */
       
    97     private File defaultBootClassPathRtJar = null;
       
    98 
       
    99     /**
       
   100      *  Is bootclasspath the default?
       
   101      */
       
   102     private boolean isDefaultBootClassPath;
       
   103 
       
   104     Path getPathForLocation(Location location) {
       
   105         Path path = pathsForLocation.get(location);
       
   106         if (path == null)
       
   107             setPathForLocation(location, null);
       
   108         return pathsForLocation.get(location);
       
   109     }
       
   110 
       
   111     void setPathForLocation(Location location, Iterable<? extends File> path) {
       
   112         // TODO? if (inited) throw new IllegalStateException
       
   113         // TODO: otherwise reset sourceSearchPath, classSearchPath as needed
       
   114         Path p;
       
   115         if (path == null) {
       
   116             if (location == CLASS_PATH)
       
   117                 p = computeUserClassPath();
       
   118             else if (location == PLATFORM_CLASS_PATH)
       
   119                 p = computeBootClassPath(); // sets isDefaultBootClassPath
       
   120             else if (location == ANNOTATION_PROCESSOR_PATH)
       
   121                 p = computeAnnotationProcessorPath();
       
   122             else if (location == SOURCE_PATH)
       
   123                 p = computeSourcePath();
       
   124             else
       
   125                 // no defaults for other paths
       
   126                 p = null;
       
   127         } else {
       
   128             if (location == PLATFORM_CLASS_PATH) {
       
   129                 defaultBootClassPathRtJar = null;
       
   130                 isDefaultBootClassPath = false;
       
   131             }
       
   132             p = new Path();
       
   133             for (File f: path)
       
   134                 p.addFile(f, warn); // TODO: is use of warn appropriate?
       
   135         }
       
   136         pathsForLocation.put(location, p);
       
   137     }
       
   138 
       
   139     public boolean isDefaultBootClassPath() {
       
   140         lazy();
       
   141         return isDefaultBootClassPath;
       
   142     }
       
   143 
       
   144     protected void lazy() {
       
   145         if (!inited) {
       
   146             warn = lint.isEnabled(Lint.LintCategory.PATH);
       
   147 
       
   148             pathsForLocation.put(PLATFORM_CLASS_PATH, computeBootClassPath());
       
   149             pathsForLocation.put(CLASS_PATH, computeUserClassPath());
       
   150             pathsForLocation.put(SOURCE_PATH, computeSourcePath());
       
   151 
       
   152             inited = true;
       
   153         }
       
   154     }
       
   155 
       
   156     public Collection<File> bootClassPath() {
       
   157         lazy();
       
   158         return Collections.unmodifiableCollection(getPathForLocation(PLATFORM_CLASS_PATH));
       
   159     }
       
   160     public Collection<File> userClassPath() {
       
   161         lazy();
       
   162         return Collections.unmodifiableCollection(getPathForLocation(CLASS_PATH));
       
   163     }
       
   164     public Collection<File> sourcePath() {
       
   165         lazy();
       
   166         Path p = getPathForLocation(SOURCE_PATH);
       
   167         return p == null || p.size() == 0
       
   168             ? null
       
   169             : Collections.unmodifiableCollection(p);
       
   170     }
       
   171 
       
   172     boolean isDefaultBootClassPathRtJar(File file) {
       
   173         return file.equals(defaultBootClassPathRtJar);
       
   174     }
       
   175 
       
   176     /**
       
   177      * Split a path into its elements. Empty path elements will be ignored.
       
   178      * @param path The path to be split
       
   179      * @return The elements of the path
       
   180      */
       
   181     private static Iterable<File> getPathEntries(String path) {
       
   182         return getPathEntries(path, null);
       
   183     }
       
   184 
       
   185     /**
       
   186      * Split a path into its elements. If emptyPathDefault is not null, all
       
   187      * empty elements in the path, including empty elements at either end of
       
   188      * the path, will be replaced with the value of emptyPathDefault.
       
   189      * @param path The path to be split
       
   190      * @param emptyPathDefault The value to substitute for empty path elements,
       
   191      *  or null, to ignore empty path elements
       
   192      * @return The elements of the path
       
   193      */
       
   194     private static Iterable<File> getPathEntries(String path, File emptyPathDefault) {
       
   195         ListBuffer<File> entries = new ListBuffer<File>();
       
   196         int start = 0;
       
   197         while (start <= path.length()) {
       
   198             int sep = path.indexOf(File.pathSeparatorChar, start);
       
   199             if (sep == -1)
       
   200                 sep = path.length();
       
   201             if (start < sep)
       
   202                 entries.add(new File(path.substring(start, sep)));
       
   203             else if (emptyPathDefault != null)
       
   204                 entries.add(emptyPathDefault);
       
   205             start = sep + 1;
       
   206         }
       
   207         return entries;
       
   208     }
       
   209 
       
   210     private class Path extends LinkedHashSet<File> {
       
   211         private static final long serialVersionUID = 0;
       
   212 
       
   213         private boolean expandJarClassPaths = false;
       
   214         private Set<File> canonicalValues = new HashSet<File>();
       
   215 
       
   216         public Path expandJarClassPaths(boolean x) {
       
   217             expandJarClassPaths = x;
       
   218             return this;
       
   219         }
       
   220 
       
   221         /** What to use when path element is the empty string */
       
   222         private File emptyPathDefault = null;
       
   223 
       
   224         public Path emptyPathDefault(File x) {
       
   225             emptyPathDefault = x;
       
   226             return this;
       
   227         }
       
   228 
       
   229         public Path() { super(); }
       
   230 
       
   231         public Path addDirectories(String dirs, boolean warn) {
       
   232             boolean prev = expandJarClassPaths;
       
   233             expandJarClassPaths = true;
       
   234             try {
       
   235                 if (dirs != null)
       
   236                     for (File dir : getPathEntries(dirs))
       
   237                         addDirectory(dir, warn);
       
   238                 return this;
       
   239             } finally {
       
   240                 expandJarClassPaths = prev;
       
   241             }
       
   242         }
       
   243 
       
   244         public Path addDirectories(String dirs) {
       
   245             return addDirectories(dirs, warn);
       
   246         }
       
   247 
       
   248         private void addDirectory(File dir, boolean warn) {
       
   249             if (!dir.isDirectory()) {
       
   250                 if (warn)
       
   251                     log.warning(Lint.LintCategory.PATH,
       
   252                             "dir.path.element.not.found", dir);
       
   253                 return;
       
   254             }
       
   255 
       
   256             File[] files = dir.listFiles();
       
   257             if (files == null)
       
   258                 return;
       
   259 
       
   260             for (File direntry : files) {
       
   261                 if (isArchive(direntry))
       
   262                     addFile(direntry, warn);
       
   263             }
       
   264         }
       
   265 
       
   266         public Path addFiles(String files, boolean warn) {
       
   267             if (files != null) {
       
   268                 for (File file : getPathEntries(files, emptyPathDefault))
       
   269                     addFile(file, warn);
       
   270             }
       
   271             return this;
       
   272         }
       
   273 
       
   274         public Path addFiles(String files) {
       
   275             return addFiles(files, warn);
       
   276         }
       
   277 
       
   278         public void addFile(File file, boolean warn) {
       
   279             if (contains(file)) {
       
   280                 // discard duplicates
       
   281                 return;
       
   282             }
       
   283 
       
   284             if (! fsInfo.exists(file)) {
       
   285                 /* No such file or directory exists */
       
   286                 if (warn) {
       
   287                     log.warning(Lint.LintCategory.PATH,
       
   288                             "path.element.not.found", file);
       
   289                 }
       
   290                 super.add(file);
       
   291                 return;
       
   292             }
       
   293 
       
   294             File canonFile = fsInfo.getCanonicalFile(file);
       
   295             if (canonicalValues.contains(canonFile)) {
       
   296                 /* Discard duplicates and avoid infinite recursion */
       
   297                 return;
       
   298             }
       
   299 
       
   300             if (fsInfo.isFile(file)) {
       
   301                 /* File is an ordinary file. */
       
   302                 if (!isArchive(file)) {
       
   303                     /* Not a recognized extension; open it to see if
       
   304                      it looks like a valid zip file. */
       
   305                     try {
       
   306                         ZipFile z = new ZipFile(file);
       
   307                         z.close();
       
   308                         if (warn) {
       
   309                             log.warning(Lint.LintCategory.PATH,
       
   310                                     "unexpected.archive.file", file);
       
   311                         }
       
   312                     } catch (IOException e) {
       
   313                         // FIXME: include e.getLocalizedMessage in warning
       
   314                         if (warn) {
       
   315                             log.warning(Lint.LintCategory.PATH,
       
   316                                     "invalid.archive.file", file);
       
   317                         }
       
   318                         return;
       
   319                     }
       
   320                 }
       
   321             }
       
   322 
       
   323             /* Now what we have left is either a directory or a file name
       
   324                conforming to archive naming convention */
       
   325             super.add(file);
       
   326             canonicalValues.add(canonFile);
       
   327 
       
   328             if (expandJarClassPaths && fsInfo.isFile(file))
       
   329                 addJarClassPath(file, warn);
       
   330         }
       
   331 
       
   332         // Adds referenced classpath elements from a jar's Class-Path
       
   333         // Manifest entry.  In some future release, we may want to
       
   334         // update this code to recognize URLs rather than simple
       
   335         // filenames, but if we do, we should redo all path-related code.
       
   336         private void addJarClassPath(File jarFile, boolean warn) {
       
   337             try {
       
   338                 for (File f: fsInfo.getJarClassPath(jarFile)) {
       
   339                     addFile(f, warn);
       
   340                 }
       
   341             } catch (IOException e) {
       
   342                 log.error("error.reading.file", jarFile, JavacFileManager.getMessage(e));
       
   343             }
       
   344         }
       
   345     }
       
   346 
       
   347     private Path computeBootClassPath() {
       
   348         defaultBootClassPathRtJar = null;
       
   349         Path path = new Path();
       
   350 
       
   351         String bootclasspathOpt = options.get(BOOTCLASSPATH);
       
   352         String endorseddirsOpt = options.get(ENDORSEDDIRS);
       
   353         String extdirsOpt = options.get(EXTDIRS);
       
   354         String xbootclasspathPrependOpt = options.get(XBOOTCLASSPATH_PREPEND);
       
   355         String xbootclasspathAppendOpt = options.get(XBOOTCLASSPATH_APPEND);
       
   356 
       
   357         path.addFiles(xbootclasspathPrependOpt);
       
   358 
       
   359         if (endorseddirsOpt != null)
       
   360             path.addDirectories(endorseddirsOpt);
       
   361         else
       
   362             path.addDirectories(System.getProperty("java.endorsed.dirs"), false);
       
   363 
       
   364         if (bootclasspathOpt != null) {
       
   365             path.addFiles(bootclasspathOpt);
       
   366         } else {
       
   367             // Standard system classes for this compiler's release.
       
   368             String files = System.getProperty("sun.boot.class.path");
       
   369             path.addFiles(files, false);
       
   370             File rt_jar = new File("rt.jar");
       
   371             for (File file : getPathEntries(files)) {
       
   372                 if (new File(file.getName()).equals(rt_jar))
       
   373                     defaultBootClassPathRtJar = file;
       
   374             }
       
   375         }
       
   376 
       
   377         path.addFiles(xbootclasspathAppendOpt);
       
   378 
       
   379         // Strictly speaking, standard extensions are not bootstrap
       
   380         // classes, but we treat them identically, so we'll pretend
       
   381         // that they are.
       
   382         if (extdirsOpt != null)
       
   383             path.addDirectories(extdirsOpt);
       
   384         else
       
   385             path.addDirectories(System.getProperty("java.ext.dirs"), false);
       
   386 
       
   387         isDefaultBootClassPath =
       
   388                 (xbootclasspathPrependOpt == null) &&
       
   389                 (bootclasspathOpt == null) &&
       
   390                 (xbootclasspathAppendOpt == null);
       
   391 
       
   392         return path;
       
   393     }
       
   394 
       
   395     private Path computeUserClassPath() {
       
   396         String cp = options.get(CLASSPATH);
       
   397 
       
   398         // CLASSPATH environment variable when run from `javac'.
       
   399         if (cp == null) cp = System.getProperty("env.class.path");
       
   400 
       
   401         // If invoked via a java VM (not the javac launcher), use the
       
   402         // platform class path
       
   403         if (cp == null && System.getProperty("application.home") == null)
       
   404             cp = System.getProperty("java.class.path");
       
   405 
       
   406         // Default to current working directory.
       
   407         if (cp == null) cp = ".";
       
   408 
       
   409         return new Path()
       
   410             .expandJarClassPaths(true)        // Only search user jars for Class-Paths
       
   411             .emptyPathDefault(new File("."))  // Empty path elt ==> current directory
       
   412             .addFiles(cp);
       
   413     }
       
   414 
       
   415     private Path computeSourcePath() {
       
   416         String sourcePathArg = options.get(SOURCEPATH);
       
   417         if (sourcePathArg == null)
       
   418             return null;
       
   419 
       
   420         return new Path().addFiles(sourcePathArg);
       
   421     }
       
   422 
       
   423     private Path computeAnnotationProcessorPath() {
       
   424         String processorPathArg = options.get(PROCESSORPATH);
       
   425         if (processorPathArg == null)
       
   426             return null;
       
   427 
       
   428         return new Path().addFiles(processorPathArg);
       
   429     }
       
   430 
       
   431     /** The actual effective locations searched for sources */
       
   432     private Path sourceSearchPath;
       
   433 
       
   434     public Collection<File> sourceSearchPath() {
       
   435         if (sourceSearchPath == null) {
       
   436             lazy();
       
   437             Path sourcePath = getPathForLocation(SOURCE_PATH);
       
   438             Path userClassPath = getPathForLocation(CLASS_PATH);
       
   439             sourceSearchPath = sourcePath != null ? sourcePath : userClassPath;
       
   440         }
       
   441         return Collections.unmodifiableCollection(sourceSearchPath);
       
   442     }
       
   443 
       
   444     /** The actual effective locations searched for classes */
       
   445     private Path classSearchPath;
       
   446 
       
   447     public Collection<File> classSearchPath() {
       
   448         if (classSearchPath == null) {
       
   449             lazy();
       
   450             Path bootClassPath = getPathForLocation(PLATFORM_CLASS_PATH);
       
   451             Path userClassPath = getPathForLocation(CLASS_PATH);
       
   452             classSearchPath = new Path();
       
   453             classSearchPath.addAll(bootClassPath);
       
   454             classSearchPath.addAll(userClassPath);
       
   455         }
       
   456         return Collections.unmodifiableCollection(classSearchPath);
       
   457     }
       
   458 
       
   459     /** The actual effective locations for non-source, non-class files */
       
   460     private Path otherSearchPath;
       
   461 
       
   462     Collection<File> otherSearchPath() {
       
   463         if (otherSearchPath == null) {
       
   464             lazy();
       
   465             Path userClassPath = getPathForLocation(CLASS_PATH);
       
   466             Path sourcePath = getPathForLocation(SOURCE_PATH);
       
   467             if (sourcePath == null)
       
   468                 otherSearchPath = userClassPath;
       
   469             else {
       
   470                 otherSearchPath = new Path();
       
   471                 otherSearchPath.addAll(userClassPath);
       
   472                 otherSearchPath.addAll(sourcePath);
       
   473             }
       
   474         }
       
   475         return Collections.unmodifiableCollection(otherSearchPath);
       
   476     }
       
   477 
       
   478     /** Is this the name of an archive file? */
       
   479     private boolean isArchive(File file) {
       
   480         String n = file.getName().toLowerCase();
       
   481         return fsInfo.isFile(file)
       
   482             && (n.endsWith(".jar") || n.endsWith(".zip"));
       
   483     }
       
   484 
       
   485     /**
       
   486      * Utility method for converting a search path string to an array
       
   487      * of directory and JAR file URLs.
       
   488      *
       
   489      * Note that this method is called by apt and the DocletInvoker.
       
   490      *
       
   491      * @param path the search path string
       
   492      * @return the resulting array of directory and JAR file URLs
       
   493      */
       
   494     public static URL[] pathToURLs(String path) {
       
   495         StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
       
   496         URL[] urls = new URL[st.countTokens()];
       
   497         int count = 0;
       
   498         while (st.hasMoreTokens()) {
       
   499             URL url = fileToURL(new File(st.nextToken()));
       
   500             if (url != null) {
       
   501                 urls[count++] = url;
       
   502             }
       
   503         }
       
   504         if (urls.length != count) {
       
   505             URL[] tmp = new URL[count];
       
   506             System.arraycopy(urls, 0, tmp, 0, count);
       
   507             urls = tmp;
       
   508         }
       
   509         return urls;
       
   510     }
       
   511 
       
   512     /**
       
   513      * Returns the directory or JAR file URL corresponding to the specified
       
   514      * local file name.
       
   515      *
       
   516      * @param file the File object
       
   517      * @return the resulting directory or JAR file URL, or null if unknown
       
   518      */
       
   519     private static URL fileToURL(File file) {
       
   520         String name;
       
   521         try {
       
   522             name = file.getCanonicalPath();
       
   523         } catch (IOException e) {
       
   524             name = file.getAbsolutePath();
       
   525         }
       
   526         name = name.replace(File.separatorChar, '/');
       
   527         if (!name.startsWith("/")) {
       
   528             name = "/" + name;
       
   529         }
       
   530         // If the file does not exist, then assume that it's a directory
       
   531         if (!file.isFile()) {
       
   532             name = name + "/";
       
   533         }
       
   534         try {
       
   535             return new URL("file", "", name);
       
   536         } catch (MalformedURLException e) {
       
   537             throw new IllegalArgumentException(file.toString());
       
   538         }
       
   539     }
       
   540 }