📄 javacfilemanager.java
字号:
} catch (IOException e) { } } } private Map<JavaFileObject, SoftReference<CharBuffer>> contentCache = new HashMap<JavaFileObject, SoftReference<CharBuffer>>(); private String defaultEncodingName; private String getDefaultEncodingName() { if (defaultEncodingName == null) { defaultEncodingName = new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding(); } return defaultEncodingName; } protected String getEncodingName() { String encName = options.get(OptionName.ENCODING); if (encName == null) return getDefaultEncodingName(); else return encName; } protected Source getSource() { String sourceName = options.get(OptionName.SOURCE); Source source = null; if (sourceName != null) source = Source.lookup(sourceName); return (source != null ? source : Source.DEFAULT); } /** * Make a byte buffer from an input stream. */ private ByteBuffer makeByteBuffer(InputStream in) throws IOException { int limit = in.available(); if (mmappedIO && in instanceof FileInputStream) { // Experimental memory mapped I/O FileInputStream fin = (FileInputStream)in; return fin.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, limit); } if (limit < 1024) limit = 1024; ByteBuffer result = byteBufferCache.get(limit); int position = 0; while (in.available() != 0) { if (position >= limit) // expand buffer result = ByteBuffer. allocate(limit <<= 1). put((ByteBuffer)result.flip()); int count = in.read(result.array(), position, limit - position); if (count < 0) break; result.position(position += count); } return (ByteBuffer)result.flip(); } /** * A single-element cache of direct byte buffers. */ private static class ByteBufferCache { private ByteBuffer cached; ByteBuffer get(int capacity) { if (capacity < 20480) capacity = 20480; ByteBuffer result = (cached != null && cached.capacity() >= capacity) ? (ByteBuffer)cached.clear() : ByteBuffer.allocate(capacity + capacity>>1); cached = null; return result; } void put(ByteBuffer x) { cached = x; } } private final ByteBufferCache byteBufferCache; private CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) { Charset charset = (this.charset == null) ? Charset.forName(encodingName) : this.charset; CharsetDecoder decoder = charset.newDecoder(); CodingErrorAction action; if (ignoreEncodingErrors) action = CodingErrorAction.REPLACE; else action = CodingErrorAction.REPORT; return decoder .onMalformedInput(action) .onUnmappableCharacter(action); } /** * Decode a ByteBuffer into a CharBuffer. */ private CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) { String encodingName = getEncodingName(); CharsetDecoder decoder; try { decoder = getDecoder(encodingName, ignoreEncodingErrors); } catch (IllegalCharsetNameException e) { log.error("unsupported.encoding", encodingName); return (CharBuffer)CharBuffer.allocate(1).flip(); } catch (UnsupportedCharsetException e) { log.error("unsupported.encoding", encodingName); return (CharBuffer)CharBuffer.allocate(1).flip(); } // slightly overestimate the buffer size to avoid reallocation. float factor = decoder.averageCharsPerByte() * 0.8f + decoder.maxCharsPerByte() * 0.2f; CharBuffer dest = CharBuffer. allocate(10 + (int)(inbuf.remaining()*factor)); while (true) { CoderResult result = decoder.decode(inbuf, dest, true); dest.flip(); if (result.isUnderflow()) { // done reading // make sure there is at least one extra character if (dest.limit() == dest.capacity()) { dest = CharBuffer.allocate(dest.capacity()+1).put(dest); dest.flip(); } return dest; } else if (result.isOverflow()) { // buffer too small; expand int newCapacity = 10 + dest.capacity() + (int)(inbuf.remaining()*decoder.maxCharsPerByte()); dest = CharBuffer.allocate(newCapacity).put(dest); } else if (result.isMalformed() || result.isUnmappable()) { // bad character in input // report coding error (warn only pre 1.5) if (!getSource().allowEncodingErrors()) { log.error(new SimpleDiagnosticPosition(dest.limit()), "illegal.char.for.encoding", charset == null ? encodingName : charset.name()); } else { log.warning(new SimpleDiagnosticPosition(dest.limit()), "illegal.char.for.encoding", charset == null ? encodingName : charset.name()); } // skip past the coding error inbuf.position(inbuf.position() + result.length()); // undo the flip() to prepare the output buffer // for more translation dest.position(dest.limit()); dest.limit(dest.capacity()); dest.put((char)0xfffd); // backward compatible } else { throw new AssertionError(result); } } // unreached } public ClassLoader getClassLoader(Location location) { nullCheck(location); Iterable<? extends File> path = getLocation(location); if (path == null) return null; ListBuffer<URL> lb = new ListBuffer<URL>(); for (File f: path) { try { lb.append(f.toURI().toURL()); } catch (MalformedURLException e) { throw new AssertionError(e); } } return new URLClassLoader(lb.toArray(new URL[lb.size()]), getClass().getClassLoader()); } public Iterable<JavaFileObject> list(Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException { // validatePackageName(packageName); nullCheck(packageName); nullCheck(kinds); Iterable<? extends File> path = getLocation(location); if (path == null) return List.nil(); String subdirectory = externalizeFileName(packageName); ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>(); for (File directory : path) listDirectory(directory, subdirectory, kinds, recurse, results); return results.toList(); } public String inferBinaryName(Location location, JavaFileObject file) { file.getClass(); // null check location.getClass(); // null check // Need to match the path semantics of list(location, ...) Iterable<? extends File> path = getLocation(location); if (path == null) { //System.err.println("Path for " + location + " is null"); return null; } //System.err.println("Path for " + location + " is " + path); if (file instanceof RegularFileObject) { RegularFileObject r = (RegularFileObject) file; String rPath = r.getPath(); //System.err.println("RegularFileObject " + file + " " +r.getPath()); for (File dir: path) { //System.err.println("dir: " + dir); String dPath = dir.getPath(); if (!dPath.endsWith(File.separator)) dPath += File.separator; if (rPath.regionMatches(true, 0, dPath, 0, dPath.length()) && new File(rPath.substring(0, dPath.length())).equals(new File(dPath))) { String relativeName = rPath.substring(dPath.length()); return removeExtension(relativeName).replace(File.separatorChar, '.'); } } } else if (file instanceof ZipFileObject) { ZipFileObject z = (ZipFileObject) file; String entryName = z.getZipEntryName(); if (entryName.startsWith(symbolFilePrefix)) entryName = entryName.substring(symbolFilePrefix.length()); return removeExtension(entryName).replace('/', '.'); } else if (file instanceof ZipFileIndexFileObject) { ZipFileIndexFileObject z = (ZipFileIndexFileObject) file; String entryName = z.getZipEntryName(); if (entryName.startsWith(symbolFilePrefix)) entryName = entryName.substring(symbolFilePrefix.length()); return removeExtension(entryName).replace(File.separatorChar, '.'); } else throw new IllegalArgumentException(file.getClass().getName()); // System.err.println("inferBinaryName failed for " + file); return null; } // where private static String removeExtension(String fileName) { int lastDot = fileName.lastIndexOf("."); return (lastDot == -1 ? fileName : fileName.substring(0, lastDot)); } public boolean isSameFile(FileObject a, FileObject b) { nullCheck(a); nullCheck(b); if (!(a instanceof BaseFileObject)) throw new IllegalArgumentException("Not supported: " + a); if (!(b instanceof BaseFileObject)) throw new IllegalArgumentException("Not supported: " + b); return a.equals(b); } public boolean handleOption(String current, Iterator<String> remaining) { for (JavacOption o: javacFileManagerOptions) { if (o.matches(current)) { if (o.hasArg()) { if (remaining.hasNext()) { if (!o.process(options, current, remaining.next())) return true; } } else { if (!o.process(options, current)) return true; } // operand missing, or process returned false throw new IllegalArgumentException(current); } } return false; } // where private static JavacOption[] javacFileManagerOptions = RecognizedOptions.getJavacFileManagerOptions( new RecognizedOptions.GrumpyHelper()); public int isSupportedOption(String option) { for (JavacOption o : javacFileManagerOptions) { if (o.matches(option)) return o.hasArg() ? 1 : 0; } return -1; } public boolean hasLocation(Location location) { return getLocation(location) != null; } public JavaFileObject getJavaFileForInput(Location location, String className, JavaFileObject.Kind kind) throws IOException { nullCheck(location); // validateClassName(className); nullCheck(className); nullCheck(kind); if (!sourceOrClass.contains(kind)) throw new IllegalArgumentException("Invalid kind " + kind); return getFileForInput(location, externalizeFileName(className, kind)); } public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException { nullCheck(location); // validatePackageName(packageName); nullCheck(packageName); if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701 throw new IllegalArgumentException("Invalid relative name: " + relativeName); String name = packageName.length() == 0 ? relativeName : new File(externalizeFileName(packageName), relativeName).getPath(); return getFileForInput(location, name); } private JavaFileObject getFileForInput(Location location, String name) throws IOException { Iterable<? extends File> path = getLocation(location); if (path == null) return null; for (File dir: path) { if (dir.isDirectory()) { File f = new File(dir, name.replace('/', File.separatorChar)); if (f.exists()) return new RegularFileObject(f); } else { Archive a = openArchive(dir); if (a.contains(name)) { int i = name.lastIndexOf('/');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -