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

Thu, 10 Jun 2010 16:08:01 -0700

author
jjg
date
Thu, 10 Jun 2010 16:08:01 -0700
changeset 581
f2fdd52e4e87
parent 554
9d9f26857129
child 757
c44234f680da
permissions
-rw-r--r--

6944312: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy

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

mercurial