Wed, 16 Oct 2013 16:33:04 -0400
8026286: Improper locking of annotation queues causes assertion failures.
8026063: Calls to annotate.flush() cause incorrect type annotations to be generated.
Summary: Fix locking in ClassReader.java
Reviewed-by: jfranck
jjg@450 | 1 | /* |
darcy@1665 | 2 | * Copyright (c) 2009, 2013, 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 | |
darcy@1665 | 115 | // Allow the following to specify a closeable classloader |
darcy@1665 | 116 | // other than URLClassLoader. |
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 | return new URLClassLoader(urls, thisClassLoader); |
jjg@450 | 131 | } |
jjg@450 | 132 | |
jjg@450 | 133 | // <editor-fold defaultstate="collapsed" desc="Option handling"> |
jjg@450 | 134 | public boolean handleOption(String current, Iterator<String> remaining) { |
jjg@1157 | 135 | OptionHelper helper = new GrumpyHelper(log) { |
jjg@1157 | 136 | @Override |
jjg@1157 | 137 | public String get(Option option) { |
jjg@1157 | 138 | return options.get(option.getText()); |
jjg@1157 | 139 | } |
jjg@1157 | 140 | |
jjg@1157 | 141 | @Override |
jjg@1157 | 142 | public void put(String name, String value) { |
jjg@1157 | 143 | options.put(name, value); |
jjg@1157 | 144 | } |
jjg@1157 | 145 | |
jjg@1157 | 146 | @Override |
jjg@1157 | 147 | public void remove(String name) { |
jjg@1157 | 148 | options.remove(name); |
jjg@1157 | 149 | } |
jjg@1157 | 150 | }; |
jjg@1157 | 151 | for (Option o: javacFileManagerOptions) { |
jjg@450 | 152 | if (o.matches(current)) { |
jjg@450 | 153 | if (o.hasArg()) { |
jjg@450 | 154 | if (remaining.hasNext()) { |
jjg@1157 | 155 | if (!o.process(helper, current, remaining.next())) |
jjg@450 | 156 | return true; |
jjg@450 | 157 | } |
jjg@450 | 158 | } else { |
jjg@1157 | 159 | if (!o.process(helper, current)) |
jjg@450 | 160 | return true; |
jjg@450 | 161 | } |
jjg@450 | 162 | // operand missing, or process returned false |
jjg@450 | 163 | throw new IllegalArgumentException(current); |
jjg@450 | 164 | } |
jjg@450 | 165 | } |
jjg@450 | 166 | |
jjg@450 | 167 | return false; |
jjg@450 | 168 | } |
jjg@450 | 169 | // where |
vromero@1442 | 170 | private static final Set<Option> javacFileManagerOptions = |
jjg@1157 | 171 | Option.getJavacFileManagerOptions(); |
jjg@450 | 172 | |
jjg@450 | 173 | public int isSupportedOption(String option) { |
jjg@1157 | 174 | for (Option o : javacFileManagerOptions) { |
jjg@450 | 175 | if (o.matches(option)) |
jjg@450 | 176 | return o.hasArg() ? 1 : 0; |
jjg@450 | 177 | } |
jjg@450 | 178 | return -1; |
jjg@450 | 179 | } |
jjg@757 | 180 | |
jjg@757 | 181 | public abstract boolean isDefaultBootClassPath(); |
jjg@757 | 182 | |
jjg@450 | 183 | // </editor-fold> |
jjg@450 | 184 | |
jjg@450 | 185 | // <editor-fold defaultstate="collapsed" desc="Encoding"> |
jjg@450 | 186 | private String defaultEncodingName; |
jjg@450 | 187 | private String getDefaultEncodingName() { |
jjg@450 | 188 | if (defaultEncodingName == null) { |
jjg@450 | 189 | defaultEncodingName = |
jjg@450 | 190 | new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding(); |
jjg@450 | 191 | } |
jjg@450 | 192 | return defaultEncodingName; |
jjg@450 | 193 | } |
jjg@450 | 194 | |
jjg@450 | 195 | public String getEncodingName() { |
jjg@1157 | 196 | String encName = options.get(Option.ENCODING); |
jjg@450 | 197 | if (encName == null) |
jjg@450 | 198 | return getDefaultEncodingName(); |
jjg@450 | 199 | else |
jjg@450 | 200 | return encName; |
jjg@450 | 201 | } |
jjg@450 | 202 | |
jjg@450 | 203 | public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) { |
jjg@450 | 204 | String encodingName = getEncodingName(); |
jjg@450 | 205 | CharsetDecoder decoder; |
jjg@450 | 206 | try { |
jjg@450 | 207 | decoder = getDecoder(encodingName, ignoreEncodingErrors); |
jjg@450 | 208 | } catch (IllegalCharsetNameException e) { |
jjg@450 | 209 | log.error("unsupported.encoding", encodingName); |
jjg@450 | 210 | return (CharBuffer)CharBuffer.allocate(1).flip(); |
jjg@450 | 211 | } catch (UnsupportedCharsetException e) { |
jjg@450 | 212 | log.error("unsupported.encoding", encodingName); |
jjg@450 | 213 | return (CharBuffer)CharBuffer.allocate(1).flip(); |
jjg@450 | 214 | } |
jjg@450 | 215 | |
jjg@450 | 216 | // slightly overestimate the buffer size to avoid reallocation. |
jjg@450 | 217 | float factor = |
jjg@450 | 218 | decoder.averageCharsPerByte() * 0.8f + |
jjg@450 | 219 | decoder.maxCharsPerByte() * 0.2f; |
jjg@450 | 220 | CharBuffer dest = CharBuffer. |
jjg@450 | 221 | allocate(10 + (int)(inbuf.remaining()*factor)); |
jjg@450 | 222 | |
jjg@450 | 223 | while (true) { |
jjg@450 | 224 | CoderResult result = decoder.decode(inbuf, dest, true); |
jjg@450 | 225 | dest.flip(); |
jjg@450 | 226 | |
jjg@450 | 227 | if (result.isUnderflow()) { // done reading |
jjg@450 | 228 | // make sure there is at least one extra character |
jjg@450 | 229 | if (dest.limit() == dest.capacity()) { |
jjg@450 | 230 | dest = CharBuffer.allocate(dest.capacity()+1).put(dest); |
jjg@450 | 231 | dest.flip(); |
jjg@450 | 232 | } |
jjg@450 | 233 | return dest; |
jjg@450 | 234 | } else if (result.isOverflow()) { // buffer too small; expand |
jjg@450 | 235 | int newCapacity = |
jjg@450 | 236 | 10 + dest.capacity() + |
jjg@450 | 237 | (int)(inbuf.remaining()*decoder.maxCharsPerByte()); |
jjg@450 | 238 | dest = CharBuffer.allocate(newCapacity).put(dest); |
jjg@450 | 239 | } else if (result.isMalformed() || result.isUnmappable()) { |
jjg@450 | 240 | // bad character in input |
jjg@450 | 241 | |
jjg@450 | 242 | // report coding error (warn only pre 1.5) |
jjg@450 | 243 | if (!getSource().allowEncodingErrors()) { |
jjg@450 | 244 | log.error(new SimpleDiagnosticPosition(dest.limit()), |
jjg@450 | 245 | "illegal.char.for.encoding", |
jjg@450 | 246 | charset == null ? encodingName : charset.name()); |
jjg@450 | 247 | } else { |
jjg@450 | 248 | log.warning(new SimpleDiagnosticPosition(dest.limit()), |
jjg@450 | 249 | "illegal.char.for.encoding", |
jjg@450 | 250 | charset == null ? encodingName : charset.name()); |
jjg@450 | 251 | } |
jjg@450 | 252 | |
jjg@450 | 253 | // skip past the coding error |
jjg@450 | 254 | inbuf.position(inbuf.position() + result.length()); |
jjg@450 | 255 | |
jjg@450 | 256 | // undo the flip() to prepare the output buffer |
jjg@450 | 257 | // for more translation |
jjg@450 | 258 | dest.position(dest.limit()); |
jjg@450 | 259 | dest.limit(dest.capacity()); |
jjg@450 | 260 | dest.put((char)0xfffd); // backward compatible |
jjg@450 | 261 | } else { |
jjg@450 | 262 | throw new AssertionError(result); |
jjg@450 | 263 | } |
jjg@450 | 264 | } |
jjg@450 | 265 | // unreached |
jjg@450 | 266 | } |
jjg@450 | 267 | |
jjg@450 | 268 | public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) { |
jjg@450 | 269 | Charset cs = (this.charset == null) |
jjg@450 | 270 | ? Charset.forName(encodingName) |
jjg@450 | 271 | : this.charset; |
jjg@450 | 272 | CharsetDecoder decoder = cs.newDecoder(); |
jjg@450 | 273 | |
jjg@450 | 274 | CodingErrorAction action; |
jjg@450 | 275 | if (ignoreEncodingErrors) |
jjg@450 | 276 | action = CodingErrorAction.REPLACE; |
jjg@450 | 277 | else |
jjg@450 | 278 | action = CodingErrorAction.REPORT; |
jjg@450 | 279 | |
jjg@450 | 280 | return decoder |
jjg@450 | 281 | .onMalformedInput(action) |
jjg@450 | 282 | .onUnmappableCharacter(action); |
jjg@450 | 283 | } |
jjg@450 | 284 | // </editor-fold> |
jjg@450 | 285 | |
jjg@450 | 286 | // <editor-fold defaultstate="collapsed" desc="ByteBuffers"> |
jjg@450 | 287 | /** |
jjg@450 | 288 | * Make a byte buffer from an input stream. |
jjg@450 | 289 | */ |
jjg@450 | 290 | public ByteBuffer makeByteBuffer(InputStream in) |
jjg@450 | 291 | throws IOException { |
jjg@450 | 292 | int limit = in.available(); |
jjg@450 | 293 | if (limit < 1024) limit = 1024; |
jjg@450 | 294 | ByteBuffer result = byteBufferCache.get(limit); |
jjg@450 | 295 | int position = 0; |
jjg@450 | 296 | while (in.available() != 0) { |
jjg@450 | 297 | if (position >= limit) |
jjg@450 | 298 | // expand buffer |
jjg@450 | 299 | result = ByteBuffer. |
jjg@450 | 300 | allocate(limit <<= 1). |
jjg@450 | 301 | put((ByteBuffer)result.flip()); |
jjg@450 | 302 | int count = in.read(result.array(), |
jjg@450 | 303 | position, |
jjg@450 | 304 | limit - position); |
jjg@450 | 305 | if (count < 0) break; |
jjg@450 | 306 | result.position(position += count); |
jjg@450 | 307 | } |
jjg@450 | 308 | return (ByteBuffer)result.flip(); |
jjg@450 | 309 | } |
jjg@450 | 310 | |
jjg@450 | 311 | public void recycleByteBuffer(ByteBuffer bb) { |
jjg@450 | 312 | byteBufferCache.put(bb); |
jjg@450 | 313 | } |
jjg@450 | 314 | |
jjg@450 | 315 | /** |
jjg@450 | 316 | * A single-element cache of direct byte buffers. |
jjg@450 | 317 | */ |
jjg@450 | 318 | private static class ByteBufferCache { |
jjg@450 | 319 | private ByteBuffer cached; |
jjg@450 | 320 | ByteBuffer get(int capacity) { |
jjg@450 | 321 | if (capacity < 20480) capacity = 20480; |
jjg@450 | 322 | ByteBuffer result = |
jjg@450 | 323 | (cached != null && cached.capacity() >= capacity) |
jjg@450 | 324 | ? (ByteBuffer)cached.clear() |
jjg@450 | 325 | : ByteBuffer.allocate(capacity + capacity>>1); |
jjg@450 | 326 | cached = null; |
jjg@450 | 327 | return result; |
jjg@450 | 328 | } |
jjg@450 | 329 | void put(ByteBuffer x) { |
jjg@450 | 330 | cached = x; |
jjg@450 | 331 | } |
jjg@450 | 332 | } |
jjg@450 | 333 | |
jjg@450 | 334 | private final ByteBufferCache byteBufferCache; |
jjg@450 | 335 | // </editor-fold> |
jjg@450 | 336 | |
jjg@450 | 337 | // <editor-fold defaultstate="collapsed" desc="Content cache"> |
jjg@450 | 338 | public CharBuffer getCachedContent(JavaFileObject file) { |
jjg@1080 | 339 | ContentCacheEntry e = contentCache.get(file); |
jjg@1080 | 340 | if (e == null) |
jjg@1080 | 341 | return null; |
jjg@1080 | 342 | |
jjg@1080 | 343 | if (!e.isValid(file)) { |
jjg@1080 | 344 | contentCache.remove(file); |
jjg@1080 | 345 | return null; |
jjg@1080 | 346 | } |
jjg@1080 | 347 | |
jjg@1080 | 348 | return e.getValue(); |
jjg@450 | 349 | } |
jjg@450 | 350 | |
jjg@450 | 351 | public void cache(JavaFileObject file, CharBuffer cb) { |
jjg@1080 | 352 | contentCache.put(file, new ContentCacheEntry(file, cb)); |
jjg@450 | 353 | } |
jjg@450 | 354 | |
jjg@1080 | 355 | public void flushCache(JavaFileObject file) { |
jjg@1080 | 356 | contentCache.remove(file); |
jjg@1080 | 357 | } |
jjg@1080 | 358 | |
jjg@1080 | 359 | protected final Map<JavaFileObject, ContentCacheEntry> contentCache |
jjg@1080 | 360 | = new HashMap<JavaFileObject, ContentCacheEntry>(); |
jjg@1080 | 361 | |
jjg@1080 | 362 | protected static class ContentCacheEntry { |
jjg@1080 | 363 | final long timestamp; |
jjg@1080 | 364 | final SoftReference<CharBuffer> ref; |
jjg@1080 | 365 | |
jjg@1080 | 366 | ContentCacheEntry(JavaFileObject file, CharBuffer cb) { |
jjg@1080 | 367 | this.timestamp = file.getLastModified(); |
jjg@1080 | 368 | this.ref = new SoftReference<CharBuffer>(cb); |
jjg@1080 | 369 | } |
jjg@1080 | 370 | |
jjg@1080 | 371 | boolean isValid(JavaFileObject file) { |
jjg@1080 | 372 | return timestamp == file.getLastModified(); |
jjg@1080 | 373 | } |
jjg@1080 | 374 | |
jjg@1080 | 375 | CharBuffer getValue() { |
jjg@1080 | 376 | return ref.get(); |
jjg@1080 | 377 | } |
jjg@1080 | 378 | } |
jjg@450 | 379 | // </editor-fold> |
jjg@450 | 380 | |
jjg@450 | 381 | public static Kind getKind(String name) { |
jjg@450 | 382 | if (name.endsWith(Kind.CLASS.extension)) |
jjg@450 | 383 | return Kind.CLASS; |
jjg@450 | 384 | else if (name.endsWith(Kind.SOURCE.extension)) |
jjg@450 | 385 | return Kind.SOURCE; |
jjg@450 | 386 | else if (name.endsWith(Kind.HTML.extension)) |
jjg@450 | 387 | return Kind.HTML; |
jjg@450 | 388 | else |
jjg@450 | 389 | return Kind.OTHER; |
jjg@450 | 390 | } |
jjg@450 | 391 | |
jjg@450 | 392 | protected static <T> T nullCheck(T o) { |
jjg@450 | 393 | o.getClass(); // null check |
jjg@450 | 394 | return o; |
jjg@450 | 395 | } |
jjg@450 | 396 | |
jjg@450 | 397 | protected static <T> Collection<T> nullCheck(Collection<T> it) { |
jjg@450 | 398 | for (T t : it) |
jjg@450 | 399 | t.getClass(); // null check |
jjg@450 | 400 | return it; |
jjg@450 | 401 | } |
jjg@450 | 402 | } |