📄 javacfilemanager.java
字号:
String dirname = name.substring(0, i+1); String basename = name.substring(i+1); return a.getFileObject(dirname, basename); } } } return null; } public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException { nullCheck(location); // validateClassName(className); nullCheck(className); nullCheck(kind); if (!sourceOrClass.contains(kind)) throw new IllegalArgumentException("Invalid kind " + kind); return getFileForOutput(location, externalizeFileName(className, kind), sibling); } public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException { nullCheck(location); // validatePackageName(packageName); nullCheck(packageName); if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701 throw new IllegalArgumentException("relativeName is invalid"); String name = packageName.length() == 0 ? relativeName : new File(externalizeFileName(packageName), relativeName).getPath(); return getFileForOutput(location, name, sibling); } private JavaFileObject getFileForOutput(Location location, String fileName, FileObject sibling) throws IOException { File dir; if (location == CLASS_OUTPUT) { if (getClassOutDir() != null) { dir = getClassOutDir(); } else { File siblingDir = null; if (sibling != null && sibling instanceof RegularFileObject) { siblingDir = ((RegularFileObject)sibling).f.getParentFile(); } return new RegularFileObject(new File(siblingDir, baseName(fileName))); } } else if (location == SOURCE_OUTPUT) { dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir()); } else { Iterable<? extends File> path = paths.getPathForLocation(location); dir = null; for (File f: path) { dir = f; break; } } File file = (dir == null ? new File(fileName) : new File(dir, fileName)); return new RegularFileObject(file); } public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles( Iterable<? extends File> files) { ArrayList<RegularFileObject> result; if (files instanceof Collection) result = new ArrayList<RegularFileObject>(((Collection)files).size()); else result = new ArrayList<RegularFileObject>(); for (File f: files) result.add(new RegularFileObject(nullCheck(f))); return result; } public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) { return getJavaFileObjectsFromFiles(Arrays.asList(nullCheck(files))); } public void setLocation(Location location, Iterable<? extends File> path) throws IOException { nullCheck(location); paths.lazy(); final File dir = location.isOutputLocation() ? getOutputDirectory(path) : null; if (location == CLASS_OUTPUT) classOutDir = getOutputLocation(dir, D); else if (location == SOURCE_OUTPUT) sourceOutDir = getOutputLocation(dir, S); else paths.setPathForLocation(location, path); } // where private File getOutputDirectory(Iterable<? extends File> path) throws IOException { if (path == null) return null; Iterator<? extends File> pathIter = path.iterator(); if (!pathIter.hasNext()) throw new IllegalArgumentException("empty path for directory"); File dir = pathIter.next(); if (pathIter.hasNext()) throw new IllegalArgumentException("path too long for directory"); if (!dir.exists()) throw new FileNotFoundException(dir + ": does not exist"); else if (!dir.isDirectory()) throw new IOException(dir + ": not a directory"); return dir; } private File getOutputLocation(File dir, OptionName defaultOptionName) { if (dir != null) return dir; String arg = options.get(defaultOptionName); if (arg == null) return null; return new File(arg); } public Iterable<? extends File> getLocation(Location location) { nullCheck(location); paths.lazy(); if (location == CLASS_OUTPUT) { return (getClassOutDir() == null ? null : List.of(getClassOutDir())); } else if (location == SOURCE_OUTPUT) { return (getSourceOutDir() == null ? null : List.of(getSourceOutDir())); } else return paths.getPathForLocation(location); } private File getClassOutDir() { if (classOutDir == uninited) classOutDir = getOutputLocation(null, D); return classOutDir; } private File getSourceOutDir() { if (sourceOutDir == uninited) sourceOutDir = getOutputLocation(null, S); return sourceOutDir; } /** * Enforces the specification of a "relative" URI as used in * {@linkplain #getFileForInput(Location,String,URI) * getFileForInput}. This method must follow the rules defined in * that method, do not make any changes without consulting the * specification. */ protected static boolean isRelativeUri(URI uri) { if (uri.isAbsolute()) return false; String path = uri.normalize().getPath(); if (path.length() == 0 /* isEmpty() is mustang API */) return false; char first = path.charAt(0); return first != '.' && first != '/'; } /** * Converts a relative file name to a relative URI. This is * different from File.toURI as this method does not canonicalize * the file before creating the URI. Furthermore, no schema is * used. * @param file a relative file name * @return a relative URI * @throws IllegalArgumentException if the file name is not * relative according to the definition given in {@link * javax.tools.JavaFileManager#getFileForInput} */ public static String getRelativeName(File file) { if (!file.isAbsolute()) { String result = file.getPath().replace(File.separatorChar, '/'); if (JavacFileManager.isRelativeUri(URI.create(result))) // FIXME 6419701 return result; } throw new IllegalArgumentException("Invalid relative path: " + file); } @SuppressWarnings("deprecation") // bug 6410637 protected static String getJavacFileName(FileObject file) { if (file instanceof BaseFileObject) return ((BaseFileObject)file).getPath(); URI uri = file.toUri(); String scheme = uri.getScheme(); if (scheme == null || scheme.equals("file") || scheme.equals("jar")) return uri.getPath(); else return uri.toString(); } @SuppressWarnings("deprecation") // bug 6410637 protected static String getJavacBaseFileName(FileObject file) { if (file instanceof BaseFileObject) return ((BaseFileObject)file).getName(); URI uri = file.toUri(); String scheme = uri.getScheme(); if (scheme == null || scheme.equals("file") || scheme.equals("jar")) { String path = uri.getPath(); if (path == null) return null; if (scheme != null && scheme.equals("jar")) path = path.substring(path.lastIndexOf('!') + 1); return path.substring(path.lastIndexOf('/') + 1); } else { return uri.toString(); } } private static <T> T nullCheck(T o) { o.getClass(); // null check return o; } private static <T> Iterable<T> nullCheck(Iterable<T> it) { for (T t : it) t.getClass(); // null check return it; } /** * A subclass of JavaFileObject representing regular files. */ private class RegularFileObject extends BaseFileObject { /** Have the parent directories been created? */ private boolean hasParents=false; /** The file's name. */ private String name; /** The underlying file. */ final File f; public RegularFileObject(File f) { this(f.getName(), f); } public RegularFileObject(String name, File f) { if (f.isDirectory()) throw new IllegalArgumentException("directories not supported"); this.name = name; this.f = f; } public InputStream openInputStream() throws IOException { return new FileInputStream(f); } protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { return JavacFileManager.this.getDecoder(getEncodingName(), ignoreEncodingErrors); } public OutputStream openOutputStream() throws IOException { ensureParentDirectoriesExist(); return new FileOutputStream(f); } public Writer openWriter() throws IOException { ensureParentDirectoriesExist(); return new OutputStreamWriter(new FileOutputStream(f), getEncodingName()); } private void ensureParentDirectoriesExist() throws IOException { if (!hasParents) { File parent = f.getParentFile(); if (parent != null && !parent.exists()) { if (!parent.mkdirs()) { // if the mkdirs failed, it may be because another process concurrently // created the directory, so check if the directory got created // anyway before throwing an exception if (!parent.exists() || !parent.isDirectory()) throw new IOException("could not create parent directories"); } } hasParents = true; } } /** @deprecated see bug 6410637 */ @Deprecated public String getName() { return name; } public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) { cn.getClass(); // null check if (kind == Kind.OTHER && getKind() != kind) return false; String n = cn + kind.extension; if (name.equals(n)) return true; if (name.equalsIgnoreCase(n)) { try { // allow for Windows return (f.getCanonicalFile().getName().equals(n)); } catch (IOException e) { } } return false; } /** @deprecated see bug 6410637 */ @Deprecated public String getPath() { return f.getPath(); } public long getLastModified() { return f.lastModified(); } public boolean delete() { return f.delete(); } public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { SoftReference<CharBuffer> r = contentCache.get(this); CharBuffer cb = (r == null ? null : r.get()); if (cb == null) { InputStream in = new FileInputStream(f); try { ByteBuffer bb = makeByteBuffer(in); JavaFileObject prev = log.useSource(this); try { cb = decode(bb, ignoreEncodingErrors); } finally {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -