📄 paths.java
字号:
/* * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */package com.sun.tools.javac.util;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.HashSet;import java.util.Map;import java.util.Set;import java.util.jar.JarFile;import java.util.jar.Manifest;import java.util.jar.Attributes;import java.util.Collection;import java.util.Collections;import java.util.LinkedHashSet;import java.util.Iterator;import java.util.StringTokenizer;import java.util.zip.ZipException;import java.util.zip.ZipFile;import com.sun.tools.javac.code.Lint;import com.sun.tools.javac.util.Context;import com.sun.tools.javac.util.Log;import com.sun.tools.javac.util.Options;import com.sun.tools.javac.util.Position;import java.util.ArrayList;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;import javax.tools.JavaFileManager.Location;import static com.sun.tools.javac.main.OptionName.*;import static javax.tools.StandardLocation.*;/** This class converts command line arguments, environment variables * and system properties (in File.pathSeparator-separated String form) * into a boot class path, user class path, and source path (in * Collection<String> form). * * <p><b>This is NOT part of any API supported by Sun Microsystems. If * you write code that depends on this, you do so at your own risk. * This code and its internal interfaces are subject to change or * deletion without notice.</b> */public class Paths { /** The context key for the todo list */ protected static final Context.Key<Paths> pathsKey = new Context.Key<Paths>(); /** Get the Paths instance for this context. */ public static Paths instance(Context context) { Paths instance = context.get(pathsKey); if (instance == null) instance = new Paths(context); return instance; } /** The log to use for warning output */ private Log log; /** Collection of command-line options */ private Options options; /** Handler for -Xlint options */ private Lint lint; private static boolean NON_BATCH_MODE = System.getProperty("nonBatchMode") != null;// TODO: Use -XD compiler switch for this. private static Map<File, PathEntry> pathExistanceCache = new ConcurrentHashMap<File, PathEntry>(); private static Map<File, java.util.List<String>> manifestEntries = new ConcurrentHashMap<File, java.util.List<String>>(); private static Map<File, Boolean> isDirectory = new ConcurrentHashMap<File, Boolean>(); private static Lock lock = new ReentrantLock(); public static void clearPathExistanceCache() { pathExistanceCache.clear(); } static class PathEntry { boolean exists = false; boolean isFile = false; File cannonicalPath = null; } protected Paths(Context context) { context.put(pathsKey, this); pathsForLocation = new HashMap<Location,Path>(16); setContext(context); } void setContext(Context context) { log = Log.instance(context); options = Options.instance(context); lint = Lint.instance(context); } /** Whether to warn about non-existent path elements */ private boolean warn; private Map<Location, Path> pathsForLocation; private boolean inited = false; // TODO? caching bad? /** * rt.jar as found on the default bootclass path. If the user specified a * bootclasspath, null is used. */ private File bootClassPathRtJar = null; Path getPathForLocation(Location location) { Path path = pathsForLocation.get(location); if (path == null) setPathForLocation(location, null); return pathsForLocation.get(location); } void setPathForLocation(Location location, Iterable<? extends File> path) { // TODO? if (inited) throw new IllegalStateException // TODO: otherwise reset sourceSearchPath, classSearchPath as needed Path p; if (path == null) { if (location == CLASS_PATH) p = computeUserClassPath(); else if (location == PLATFORM_CLASS_PATH) p = computeBootClassPath(); else if (location == ANNOTATION_PROCESSOR_PATH) p = computeAnnotationProcessorPath(); else if (location == SOURCE_PATH) p = computeSourcePath(); else // no defaults for other paths p = null; } else { p = new Path(); for (File f: path) p.addFile(f, warn); // TODO: is use of warn appropriate? } pathsForLocation.put(location, p); } protected void lazy() { if (!inited) { warn = lint.isEnabled(Lint.LintCategory.PATH); pathsForLocation.put(PLATFORM_CLASS_PATH, computeBootClassPath()); pathsForLocation.put(CLASS_PATH, computeUserClassPath()); pathsForLocation.put(SOURCE_PATH, computeSourcePath()); inited = true; } } public Collection<File> bootClassPath() { lazy(); return Collections.unmodifiableCollection(getPathForLocation(PLATFORM_CLASS_PATH)); } public Collection<File> userClassPath() { lazy(); return Collections.unmodifiableCollection(getPathForLocation(CLASS_PATH)); } public Collection<File> sourcePath() { lazy(); Path p = getPathForLocation(SOURCE_PATH); return p == null || p.size() == 0 ? null : Collections.unmodifiableCollection(p); } boolean isBootClassPathRtJar(File file) { return file.equals(bootClassPathRtJar); } private static class PathIterator implements Iterable<String> { private int pos = 0; private final String path; private final String emptyPathDefault; public PathIterator(String path, String emptyPathDefault) { this.path = path; this.emptyPathDefault = emptyPathDefault; } public PathIterator(String path) { this(path, null); } public Iterator<String> iterator() { return new Iterator<String>() { public boolean hasNext() { return pos <= path.length(); } public String next() { int beg = pos; int end = path.indexOf(File.pathSeparator, beg); if (end == -1) end = path.length(); pos = end + 1; if (beg == end && emptyPathDefault != null) return emptyPathDefault; else return path.substring(beg, end); } public void remove() { throw new UnsupportedOperationException(); } }; } } private class Path extends LinkedHashSet<File> { private static final long serialVersionUID = 0; private boolean expandJarClassPaths = false; private Set<File> canonicalValues = new HashSet<File>(); public Path expandJarClassPaths(boolean x) { expandJarClassPaths = x; return this; } /** What to use when path element is the empty string */ private String emptyPathDefault = null; public Path emptyPathDefault(String x) { emptyPathDefault = x; return this; } public Path() { super(); } public Path addDirectories(String dirs, boolean warn) { if (dirs != null) for (String dir : new PathIterator(dirs)) addDirectory(dir, warn); return this; } public Path addDirectories(String dirs) { return addDirectories(dirs, warn); } private void addDirectory(String dir, boolean warn) { if (! new File(dir).isDirectory()) { if (warn) log.warning("dir.path.element.not.found", dir); return; } File[] files = new File(dir).listFiles(); if (files == null) return; for (File direntry : files) { if (isArchive(direntry)) addFile(direntry, warn); } } public Path addFiles(String files, boolean warn) { if (files != null) for (String file : new PathIterator(files, emptyPathDefault)) addFile(file, warn); return this; } public Path addFiles(String files) { return addFiles(files, warn); } public Path addFile(String file, boolean warn) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -