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

Mon, 14 Nov 2011 15:11:10 -0800

author
ksrini
date
Mon, 14 Nov 2011 15:11:10 -0800
changeset 1138
7375d4979bd3
parent 1136
ae361e7f435a
child 1157
3809292620c9
permissions
-rw-r--r--

7106166: (javac) re-factor EndPos parser
Reviewed-by: jjg

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

mercurial