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

Mon, 10 Dec 2012 16:21:26 +0000

author
vromero
date
Mon, 10 Dec 2012 16:21:26 +0000
changeset 1442
fcf89720ae71
parent 1157
3809292620c9
child 1665
33b6a52f0037
permissions
-rw-r--r--

8003967: detect and remove all mutable implicit static enum fields in langtools
Reviewed-by: jjg

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

mercurial