src/share/classes/com/sun/tools/javac/util/BaseFileManager.java

Thu, 24 May 2018 16:48:51 +0800

author
aoqi
date
Thu, 24 May 2018 16:48:51 +0800
changeset 3295
859dc787b52b
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.util;
aoqi@0 27
aoqi@0 28 import java.io.ByteArrayOutputStream;
aoqi@0 29 import java.io.Closeable;
aoqi@0 30 import java.io.IOException;
aoqi@0 31 import java.io.InputStream;
aoqi@0 32 import java.io.OutputStreamWriter;
aoqi@0 33 import java.lang.ref.SoftReference;
aoqi@0 34 import java.lang.reflect.Constructor;
aoqi@0 35 import java.net.URL;
aoqi@0 36 import java.net.URLClassLoader;
aoqi@0 37 import java.nio.ByteBuffer;
aoqi@0 38 import java.nio.CharBuffer;
aoqi@0 39 import java.nio.charset.Charset;
aoqi@0 40 import java.nio.charset.CharsetDecoder;
aoqi@0 41 import java.nio.charset.CoderResult;
aoqi@0 42 import java.nio.charset.CodingErrorAction;
aoqi@0 43 import java.nio.charset.IllegalCharsetNameException;
aoqi@0 44 import java.nio.charset.UnsupportedCharsetException;
aoqi@0 45 import java.util.Collection;
aoqi@0 46 import java.util.HashMap;
aoqi@0 47 import java.util.Iterator;
aoqi@0 48 import java.util.Map;
aoqi@0 49 import java.util.Set;
aoqi@0 50 import javax.tools.JavaFileObject;
aoqi@0 51 import javax.tools.JavaFileObject.Kind;
aoqi@0 52
aoqi@0 53 import com.sun.tools.javac.code.Lint;
aoqi@0 54 import com.sun.tools.javac.code.Source;
aoqi@0 55 import com.sun.tools.javac.file.FSInfo;
aoqi@0 56 import com.sun.tools.javac.file.Locations;
aoqi@0 57 import com.sun.tools.javac.main.Option;
aoqi@0 58 import com.sun.tools.javac.main.OptionHelper;
aoqi@0 59 import com.sun.tools.javac.main.OptionHelper.GrumpyHelper;
aoqi@0 60 import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
aoqi@0 61
aoqi@0 62 /**
aoqi@0 63 * Utility methods for building a filemanager.
aoqi@0 64 * There are no references here to file-system specific objects such as
aoqi@0 65 * java.io.File or java.nio.file.Path.
aoqi@0 66 */
aoqi@0 67 public abstract class BaseFileManager {
aoqi@0 68 protected BaseFileManager(Charset charset) {
aoqi@0 69 this.charset = charset;
aoqi@0 70 byteBufferCache = new ByteBufferCache();
aoqi@0 71 locations = createLocations();
aoqi@0 72 }
aoqi@0 73
aoqi@0 74 /**
aoqi@0 75 * Set the context for JavacPathFileManager.
aoqi@0 76 */
aoqi@0 77 public void setContext(Context context) {
aoqi@0 78 log = Log.instance(context);
aoqi@0 79 options = Options.instance(context);
aoqi@0 80 classLoaderClass = options.get("procloader");
aoqi@0 81 locations.update(log, options, Lint.instance(context), FSInfo.instance(context));
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 protected Locations createLocations() {
aoqi@0 85 return new Locations();
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 /**
aoqi@0 89 * The log to be used for error reporting.
aoqi@0 90 */
aoqi@0 91 public Log log;
aoqi@0 92
aoqi@0 93 /**
aoqi@0 94 * User provided charset (through javax.tools).
aoqi@0 95 */
aoqi@0 96 protected Charset charset;
aoqi@0 97
aoqi@0 98 protected Options options;
aoqi@0 99
aoqi@0 100 protected String classLoaderClass;
aoqi@0 101
aoqi@0 102 protected Locations locations;
aoqi@0 103
aoqi@0 104 protected Source getSource() {
aoqi@0 105 String sourceName = options.get(Option.SOURCE);
aoqi@0 106 Source source = null;
aoqi@0 107 if (sourceName != null)
aoqi@0 108 source = Source.lookup(sourceName);
aoqi@0 109 return (source != null ? source : Source.DEFAULT);
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 protected ClassLoader getClassLoader(URL[] urls) {
aoqi@0 113 ClassLoader thisClassLoader = getClass().getClassLoader();
aoqi@0 114
aoqi@0 115 // Allow the following to specify a closeable classloader
aoqi@0 116 // other than URLClassLoader.
aoqi@0 117
aoqi@0 118 // 1: Allow client to specify the class to use via hidden option
aoqi@0 119 if (classLoaderClass != null) {
aoqi@0 120 try {
aoqi@0 121 Class<? extends ClassLoader> loader =
aoqi@0 122 Class.forName(classLoaderClass).asSubclass(ClassLoader.class);
aoqi@0 123 Class<?>[] constrArgTypes = { URL[].class, ClassLoader.class };
aoqi@0 124 Constructor<? extends ClassLoader> constr = loader.getConstructor(constrArgTypes);
aoqi@0 125 return constr.newInstance(new Object[] { urls, thisClassLoader });
aoqi@0 126 } catch (Throwable t) {
aoqi@0 127 // ignore errors loading user-provided class loader, fall through
aoqi@0 128 }
aoqi@0 129 }
aoqi@0 130 return new URLClassLoader(urls, thisClassLoader);
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 // <editor-fold defaultstate="collapsed" desc="Option handling">
aoqi@0 134 public boolean handleOption(String current, Iterator<String> remaining) {
aoqi@0 135 OptionHelper helper = new GrumpyHelper(log) {
aoqi@0 136 @Override
aoqi@0 137 public String get(Option option) {
aoqi@0 138 return options.get(option.getText());
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 @Override
aoqi@0 142 public void put(String name, String value) {
aoqi@0 143 options.put(name, value);
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 @Override
aoqi@0 147 public void remove(String name) {
aoqi@0 148 options.remove(name);
aoqi@0 149 }
aoqi@0 150 };
aoqi@0 151 for (Option o: javacFileManagerOptions) {
aoqi@0 152 if (o.matches(current)) {
aoqi@0 153 if (o.hasArg()) {
aoqi@0 154 if (remaining.hasNext()) {
aoqi@0 155 if (!o.process(helper, current, remaining.next()))
aoqi@0 156 return true;
aoqi@0 157 }
aoqi@0 158 } else {
aoqi@0 159 if (!o.process(helper, current))
aoqi@0 160 return true;
aoqi@0 161 }
aoqi@0 162 // operand missing, or process returned false
aoqi@0 163 throw new IllegalArgumentException(current);
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 return false;
aoqi@0 168 }
aoqi@0 169 // where
aoqi@0 170 private static final Set<Option> javacFileManagerOptions =
aoqi@0 171 Option.getJavacFileManagerOptions();
aoqi@0 172
aoqi@0 173 public int isSupportedOption(String option) {
aoqi@0 174 for (Option o : javacFileManagerOptions) {
aoqi@0 175 if (o.matches(option))
aoqi@0 176 return o.hasArg() ? 1 : 0;
aoqi@0 177 }
aoqi@0 178 return -1;
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 public abstract boolean isDefaultBootClassPath();
aoqi@0 182
aoqi@0 183 // </editor-fold>
aoqi@0 184
aoqi@0 185 // <editor-fold defaultstate="collapsed" desc="Encoding">
aoqi@0 186 private String defaultEncodingName;
aoqi@0 187 private String getDefaultEncodingName() {
aoqi@0 188 if (defaultEncodingName == null) {
aoqi@0 189 defaultEncodingName =
aoqi@0 190 new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
aoqi@0 191 }
aoqi@0 192 return defaultEncodingName;
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 public String getEncodingName() {
aoqi@0 196 String encName = options.get(Option.ENCODING);
aoqi@0 197 if (encName == null)
aoqi@0 198 return getDefaultEncodingName();
aoqi@0 199 else
aoqi@0 200 return encName;
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
aoqi@0 204 String encodingName = getEncodingName();
aoqi@0 205 CharsetDecoder decoder;
aoqi@0 206 try {
aoqi@0 207 decoder = getDecoder(encodingName, ignoreEncodingErrors);
aoqi@0 208 } catch (IllegalCharsetNameException e) {
aoqi@0 209 log.error("unsupported.encoding", encodingName);
aoqi@0 210 return (CharBuffer)CharBuffer.allocate(1).flip();
aoqi@0 211 } catch (UnsupportedCharsetException e) {
aoqi@0 212 log.error("unsupported.encoding", encodingName);
aoqi@0 213 return (CharBuffer)CharBuffer.allocate(1).flip();
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 // slightly overestimate the buffer size to avoid reallocation.
aoqi@0 217 float factor =
aoqi@0 218 decoder.averageCharsPerByte() * 0.8f +
aoqi@0 219 decoder.maxCharsPerByte() * 0.2f;
aoqi@0 220 CharBuffer dest = CharBuffer.
aoqi@0 221 allocate(10 + (int)(inbuf.remaining()*factor));
aoqi@0 222
aoqi@0 223 while (true) {
aoqi@0 224 CoderResult result = decoder.decode(inbuf, dest, true);
aoqi@0 225 dest.flip();
aoqi@0 226
aoqi@0 227 if (result.isUnderflow()) { // done reading
aoqi@0 228 // make sure there is at least one extra character
aoqi@0 229 if (dest.limit() == dest.capacity()) {
aoqi@0 230 dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
aoqi@0 231 dest.flip();
aoqi@0 232 }
aoqi@0 233 return dest;
aoqi@0 234 } else if (result.isOverflow()) { // buffer too small; expand
aoqi@0 235 int newCapacity =
aoqi@0 236 10 + dest.capacity() +
aoqi@0 237 (int)(inbuf.remaining()*decoder.maxCharsPerByte());
aoqi@0 238 dest = CharBuffer.allocate(newCapacity).put(dest);
aoqi@0 239 } else if (result.isMalformed() || result.isUnmappable()) {
aoqi@0 240 // bad character in input
aoqi@0 241
aoqi@0 242 // report coding error (warn only pre 1.5)
aoqi@0 243 if (!getSource().allowEncodingErrors()) {
aoqi@0 244 log.error(new SimpleDiagnosticPosition(dest.limit()),
aoqi@0 245 "illegal.char.for.encoding",
aoqi@0 246 charset == null ? encodingName : charset.name());
aoqi@0 247 } else {
aoqi@0 248 log.warning(new SimpleDiagnosticPosition(dest.limit()),
aoqi@0 249 "illegal.char.for.encoding",
aoqi@0 250 charset == null ? encodingName : charset.name());
aoqi@0 251 }
aoqi@0 252
aoqi@0 253 // skip past the coding error
aoqi@0 254 inbuf.position(inbuf.position() + result.length());
aoqi@0 255
aoqi@0 256 // undo the flip() to prepare the output buffer
aoqi@0 257 // for more translation
aoqi@0 258 dest.position(dest.limit());
aoqi@0 259 dest.limit(dest.capacity());
aoqi@0 260 dest.put((char)0xfffd); // backward compatible
aoqi@0 261 } else {
aoqi@0 262 throw new AssertionError(result);
aoqi@0 263 }
aoqi@0 264 }
aoqi@0 265 // unreached
aoqi@0 266 }
aoqi@0 267
aoqi@0 268 public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
aoqi@0 269 Charset cs = (this.charset == null)
aoqi@0 270 ? Charset.forName(encodingName)
aoqi@0 271 : this.charset;
aoqi@0 272 CharsetDecoder decoder = cs.newDecoder();
aoqi@0 273
aoqi@0 274 CodingErrorAction action;
aoqi@0 275 if (ignoreEncodingErrors)
aoqi@0 276 action = CodingErrorAction.REPLACE;
aoqi@0 277 else
aoqi@0 278 action = CodingErrorAction.REPORT;
aoqi@0 279
aoqi@0 280 return decoder
aoqi@0 281 .onMalformedInput(action)
aoqi@0 282 .onUnmappableCharacter(action);
aoqi@0 283 }
aoqi@0 284 // </editor-fold>
aoqi@0 285
aoqi@0 286 // <editor-fold defaultstate="collapsed" desc="ByteBuffers">
aoqi@0 287 /**
aoqi@0 288 * Make a byte buffer from an input stream.
aoqi@0 289 */
aoqi@0 290 public ByteBuffer makeByteBuffer(InputStream in)
aoqi@0 291 throws IOException {
aoqi@0 292 int limit = in.available();
aoqi@0 293 if (limit < 1024) limit = 1024;
aoqi@0 294 ByteBuffer result = byteBufferCache.get(limit);
aoqi@0 295 int position = 0;
aoqi@0 296 while (in.available() != 0) {
aoqi@0 297 if (position >= limit)
aoqi@0 298 // expand buffer
aoqi@0 299 result = ByteBuffer.
aoqi@0 300 allocate(limit <<= 1).
aoqi@0 301 put((ByteBuffer)result.flip());
aoqi@0 302 int count = in.read(result.array(),
aoqi@0 303 position,
aoqi@0 304 limit - position);
aoqi@0 305 if (count < 0) break;
aoqi@0 306 result.position(position += count);
aoqi@0 307 }
aoqi@0 308 return (ByteBuffer)result.flip();
aoqi@0 309 }
aoqi@0 310
aoqi@0 311 public void recycleByteBuffer(ByteBuffer bb) {
aoqi@0 312 byteBufferCache.put(bb);
aoqi@0 313 }
aoqi@0 314
aoqi@0 315 /**
aoqi@0 316 * A single-element cache of direct byte buffers.
aoqi@0 317 */
aoqi@0 318 private static class ByteBufferCache {
aoqi@0 319 private ByteBuffer cached;
aoqi@0 320 ByteBuffer get(int capacity) {
aoqi@0 321 if (capacity < 20480) capacity = 20480;
aoqi@0 322 ByteBuffer result =
aoqi@0 323 (cached != null && cached.capacity() >= capacity)
aoqi@0 324 ? (ByteBuffer)cached.clear()
aoqi@0 325 : ByteBuffer.allocate(capacity + capacity>>1);
aoqi@0 326 cached = null;
aoqi@0 327 return result;
aoqi@0 328 }
aoqi@0 329 void put(ByteBuffer x) {
aoqi@0 330 cached = x;
aoqi@0 331 }
aoqi@0 332 }
aoqi@0 333
aoqi@0 334 private final ByteBufferCache byteBufferCache;
aoqi@0 335 // </editor-fold>
aoqi@0 336
aoqi@0 337 // <editor-fold defaultstate="collapsed" desc="Content cache">
aoqi@0 338 public CharBuffer getCachedContent(JavaFileObject file) {
aoqi@0 339 ContentCacheEntry e = contentCache.get(file);
aoqi@0 340 if (e == null)
aoqi@0 341 return null;
aoqi@0 342
aoqi@0 343 if (!e.isValid(file)) {
aoqi@0 344 contentCache.remove(file);
aoqi@0 345 return null;
aoqi@0 346 }
aoqi@0 347
aoqi@0 348 return e.getValue();
aoqi@0 349 }
aoqi@0 350
aoqi@0 351 public void cache(JavaFileObject file, CharBuffer cb) {
aoqi@0 352 contentCache.put(file, new ContentCacheEntry(file, cb));
aoqi@0 353 }
aoqi@0 354
aoqi@0 355 public void flushCache(JavaFileObject file) {
aoqi@0 356 contentCache.remove(file);
aoqi@0 357 }
aoqi@0 358
aoqi@0 359 protected final Map<JavaFileObject, ContentCacheEntry> contentCache
aoqi@0 360 = new HashMap<JavaFileObject, ContentCacheEntry>();
aoqi@0 361
aoqi@0 362 protected static class ContentCacheEntry {
aoqi@0 363 final long timestamp;
aoqi@0 364 final SoftReference<CharBuffer> ref;
aoqi@0 365
aoqi@0 366 ContentCacheEntry(JavaFileObject file, CharBuffer cb) {
aoqi@0 367 this.timestamp = file.getLastModified();
aoqi@0 368 this.ref = new SoftReference<CharBuffer>(cb);
aoqi@0 369 }
aoqi@0 370
aoqi@0 371 boolean isValid(JavaFileObject file) {
aoqi@0 372 return timestamp == file.getLastModified();
aoqi@0 373 }
aoqi@0 374
aoqi@0 375 CharBuffer getValue() {
aoqi@0 376 return ref.get();
aoqi@0 377 }
aoqi@0 378 }
aoqi@0 379 // </editor-fold>
aoqi@0 380
aoqi@0 381 public static Kind getKind(String name) {
aoqi@0 382 if (name.endsWith(Kind.CLASS.extension))
aoqi@0 383 return Kind.CLASS;
aoqi@0 384 else if (name.endsWith(Kind.SOURCE.extension))
aoqi@0 385 return Kind.SOURCE;
aoqi@0 386 else if (name.endsWith(Kind.HTML.extension))
aoqi@0 387 return Kind.HTML;
aoqi@0 388 else
aoqi@0 389 return Kind.OTHER;
aoqi@0 390 }
aoqi@0 391
aoqi@0 392 protected static <T> T nullCheck(T o) {
aoqi@0 393 o.getClass(); // null check
aoqi@0 394 return o;
aoqi@0 395 }
aoqi@0 396
aoqi@0 397 protected static <T> Collection<T> nullCheck(Collection<T> it) {
aoqi@0 398 for (T t : it)
aoqi@0 399 t.getClass(); // null check
aoqi@0 400 return it;
aoqi@0 401 }
aoqi@0 402 }

mercurial