1 /* 2 * Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.javac.file; 27 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.lang.ref.SoftReference; 31 import java.lang.reflect.Constructor; 32 import java.net.URL; 33 import java.net.URLClassLoader; 34 import java.nio.ByteBuffer; 35 import java.nio.CharBuffer; 36 import java.nio.charset.Charset; 37 import java.nio.charset.CharsetDecoder; 38 import java.nio.charset.CoderResult; 39 import java.nio.charset.CodingErrorAction; 40 import java.nio.charset.IllegalCharsetNameException; 41 import java.nio.charset.UnsupportedCharsetException; 42 import java.nio.file.NoSuchFileException; 43 import java.nio.file.Path; 44 import java.util.Collection; 45 import java.util.HashMap; 46 import java.util.HashSet; 47 import java.util.Iterator; 48 import java.util.Map; 49 import java.util.Objects; 50 import java.util.Set; 51 52 import javax.tools.JavaFileManager; 53 import javax.tools.JavaFileObject; 54 import javax.tools.JavaFileObject.Kind; 55 56 import com.sun.tools.javac.code.Lint; 57 import com.sun.tools.javac.code.Lint.LintCategory; 58 import com.sun.tools.javac.main.JavaCompiler; 59 import com.sun.tools.javac.main.JavaCompiler.CodeReflectionSupport; 60 import com.sun.tools.javac.main.Option; 61 import com.sun.tools.javac.main.OptionHelper; 62 import com.sun.tools.javac.main.OptionHelper.GrumpyHelper; 63 import com.sun.tools.javac.resources.CompilerProperties.Errors; 64 import com.sun.tools.javac.resources.CompilerProperties.LintWarnings; 65 import com.sun.tools.javac.resources.CompilerProperties.Warnings; 66 import com.sun.tools.javac.util.Context; 67 import com.sun.tools.javac.util.DefinedBy; 68 import com.sun.tools.javac.util.DefinedBy.Api; 69 import com.sun.tools.javac.util.Log; 70 import com.sun.tools.javac.util.Options; 71 72 /** 73 * Utility methods for building a file manager. 74 * There are no references here to file-system specific objects such as 75 * java.io.File or java.nio.file.Path. 76 */ 77 public abstract class BaseFileManager implements JavaFileManager { 78 79 private static final byte[] EMPTY_ARRAY = new byte[0]; 80 81 @SuppressWarnings("this-escape") 82 protected BaseFileManager(Charset charset) { 83 this.charset = charset; 84 locations = createLocations(); 85 } 86 87 /** 88 * Set the context for JavacPathFileManager. 89 * @param context the context containing items to be associated with the file manager 90 */ 91 public void setContext(Context context) { 92 log = Log.instance(context); 93 lint = Lint.instance(context); 94 options = Options.instance(context); 95 96 // Initialize locations 97 locations.update(log, lint, FSInfo.instance(context)); 98 99 // Apply options 100 options.whenReady(this::applyOptions); 101 } 102 103 protected void applyOptions(Options options) { 104 105 // Setting this option is an indication that close() should defer actually closing 106 // the file manager until after a specified period of inactivity. 107 // This is to accommodate clients which save references to Symbols created for use 108 // within doclets or annotation processors, and which then attempt to use those 109 // references after the tool exits, having closed any internally managed file manager. 110 // Ideally, such clients should run the tool via the javax.tools API, providing their 111 // own file manager, which can be closed by the client when all use of that file 112 // manager is complete. 113 // If the option has a numeric value, it will be interpreted as the duration, 114 // in seconds, of the period of inactivity to wait for, before the file manager 115 // is actually closed. 116 // See also deferredClose(). 117 String s = options.get("fileManager.deferClose"); 118 if (s != null) { 119 try { 120 deferredCloseTimeout = (int) (Float.parseFloat(s) * 1000); 121 } catch (NumberFormatException e) { 122 deferredCloseTimeout = 60 * 1000; // default: one minute, in millis 123 } 124 } 125 } 126 127 protected Locations createLocations() { 128 return new Locations(); 129 } 130 131 /** 132 * The log to be used for error reporting. 133 */ 134 public Log log; 135 136 /** 137 * User provided charset (through javax.tools). 138 */ 139 protected Charset charset; 140 141 protected Options options; 142 143 protected Lint lint; 144 145 protected final Locations locations; 146 147 private final HashSet<Path> outputFilesWritten = new HashSet<>(); 148 149 /** 150 * A flag for clients to use to indicate that this file manager should 151 * be closed when it is no longer required. 152 */ 153 public boolean autoClose; 154 155 /** 156 * Wait for a period of inactivity before calling close(). 157 * The length of the period of inactivity is given by {@code deferredCloseTimeout} 158 */ 159 protected void deferredClose() { 160 Thread t = new Thread(getClass().getName() + " DeferredClose") { 161 @Override 162 public void run() { 163 try { 164 synchronized (BaseFileManager.this) { 165 long now = System.currentTimeMillis(); 166 while (now < lastUsedTime + deferredCloseTimeout) { 167 BaseFileManager.this.wait(lastUsedTime + deferredCloseTimeout - now); 168 now = System.currentTimeMillis(); 169 } 170 deferredCloseTimeout = 0; 171 close(); 172 } 173 } catch (InterruptedException e) { 174 } catch (IOException e) { 175 } 176 } 177 }; 178 t.setDaemon(true); 179 t.start(); 180 } 181 182 synchronized void updateLastUsedTime() { 183 if (deferredCloseTimeout > 0) { // avoid updating the time unnecessarily 184 lastUsedTime = System.currentTimeMillis(); 185 } 186 } 187 188 private long lastUsedTime = System.currentTimeMillis(); 189 protected long deferredCloseTimeout = 0; 190 191 public void clear() { 192 new HashSet<>(options.keySet()).forEach(k -> options.remove(k)); 193 } 194 195 protected ClassLoader getClassLoader(URL[] urls) { 196 ClassLoader thisClassLoader = CodeReflectionSupport.CODE_LAYER != null ? 197 CodeReflectionSupport.CODE_LAYER.findLoader("jdk.incubator.code") : 198 getClass().getClassLoader(); 199 200 // Allow the following to specify a closeable classloader 201 // other than URLClassLoader. 202 203 // 1: Allow client to specify the class to use via hidden option 204 String classLoaderClass = options.get("procloader"); 205 if (classLoaderClass != null) { 206 try { 207 Class<? extends ClassLoader> loader = 208 Class.forName(classLoaderClass).asSubclass(ClassLoader.class); 209 Class<?>[] constrArgTypes = { URL[].class, ClassLoader.class }; 210 Constructor<? extends ClassLoader> constr = loader.getConstructor(constrArgTypes); 211 return constr.newInstance(urls, thisClassLoader); 212 } catch (ReflectiveOperationException t) { 213 // ignore errors loading user-provided class loader, fall through 214 } 215 } 216 return new URLClassLoader(urls, thisClassLoader); 217 } 218 219 public boolean isDefaultBootClassPath() { 220 return locations.isDefaultBootClassPath(); 221 } 222 223 public boolean isDefaultSystemModulesPath() { 224 return locations.isDefaultSystemModulesPath(); 225 } 226 227 // <editor-fold defaultstate="collapsed" desc="Option handling"> 228 @Override @DefinedBy(Api.COMPILER) 229 public boolean handleOption(String current, Iterator<String> remaining) { 230 OptionHelper helper = new GrumpyHelper(log) { 231 @Override 232 public String get(Option option) { 233 return options.get(option); 234 } 235 236 @Override 237 public void put(String name, String value) { 238 options.put(name, value); 239 } 240 241 @Override 242 public void remove(String name) { 243 options.remove(name); 244 } 245 246 @Override 247 public boolean handleFileManagerOption(Option option, String value) { 248 return handleOption(option, value); 249 } 250 251 @Override 252 public void initialize() { 253 options.initialize(); 254 } 255 }; 256 257 Option o = Option.lookup(current, javacFileManagerOptions); 258 if (o == null) { 259 return false; 260 } 261 262 try { 263 o.handleOption(helper, current, remaining); 264 } catch (Option.InvalidValueException e) { 265 throw new IllegalArgumentException(e.getMessage(), e); 266 } 267 268 return true; 269 } 270 // where 271 protected static final Set<Option> javacFileManagerOptions = 272 Option.getJavacFileManagerOptions(); 273 274 @Override @DefinedBy(Api.COMPILER) 275 public int isSupportedOption(String option) { 276 Option o = Option.lookup(option, javacFileManagerOptions); 277 return (o == null) ? -1 : o.hasArg() ? 1 : 0; 278 } 279 280 protected String multiReleaseValue; 281 282 /** 283 * Common back end for OptionHelper handleFileManagerOption. 284 * @param option the option whose value to be set 285 * @param value the value for the option 286 * @return true if successful, and false otherwise 287 */ 288 public boolean handleOption(Option option, String value) { 289 switch (option) { 290 case ENCODING: 291 encodingName = value; 292 return true; 293 294 case MULTIRELEASE: 295 multiReleaseValue = value; 296 locations.setMultiReleaseValue(value); 297 return true; 298 299 default: 300 return locations.handleOption(option, value); 301 } 302 } 303 304 /** 305 * Call handleOption for collection of options and corresponding values. 306 * @param map a collection of options and corresponding values 307 * @return true if all the calls are successful 308 */ 309 public boolean handleOptions(Map<Option, String> map) { 310 boolean ok = true; 311 for (Map.Entry<Option, String> e: map.entrySet()) { 312 try { 313 ok = ok & handleOption(e.getKey(), e.getValue()); 314 } catch (IllegalArgumentException ex) { 315 log.error(Errors.IllegalArgumentForOption(e.getKey().getPrimaryName(), ex.getMessage())); 316 ok = false; 317 } 318 } 319 return ok; 320 } 321 322 // </editor-fold> 323 324 // <editor-fold defaultstate="collapsed" desc="Encoding"> 325 private String encodingName; 326 private String defaultEncodingName; 327 private String getDefaultEncodingName() { 328 if (defaultEncodingName == null) { 329 defaultEncodingName = Charset.defaultCharset().name(); 330 } 331 return defaultEncodingName; 332 } 333 334 public String getEncodingName() { 335 return (encodingName != null) ? encodingName : getDefaultEncodingName(); 336 } 337 338 public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) { 339 String encName = getEncodingName(); 340 CharsetDecoder decoder; 341 try { 342 decoder = getDecoder(encName, ignoreEncodingErrors); 343 } catch (IllegalCharsetNameException | UnsupportedCharsetException e) { 344 log.error(Errors.UnsupportedEncoding(encName)); 345 return CharBuffer.allocate(1).flip(); 346 } 347 348 // slightly overestimate the buffer size to avoid reallocation. 349 float factor = 350 decoder.averageCharsPerByte() * 0.8f + 351 decoder.maxCharsPerByte() * 0.2f; 352 CharBuffer dest = CharBuffer. 353 allocate(10 + (int)(inbuf.remaining()*factor)); 354 355 while (true) { 356 CoderResult result = decoder.decode(inbuf, dest, true); 357 dest.flip(); 358 359 if (result.isUnderflow()) { // done reading 360 // make sure there is at least one extra character 361 if (dest.limit() == dest.capacity()) { 362 dest = CharBuffer.allocate(dest.capacity()+1).put(dest); 363 dest.flip(); 364 } 365 return dest; 366 } else if (result.isOverflow()) { // buffer too small; expand 367 int newCapacity = 368 10 + dest.capacity() + 369 (int)(inbuf.remaining()*decoder.maxCharsPerByte()); 370 dest = CharBuffer.allocate(newCapacity).put(dest); 371 } else if (result.isMalformed() || result.isUnmappable()) { 372 // bad character in input 373 StringBuilder unmappable = new StringBuilder(); 374 int len = result.length(); 375 376 for (int i = 0; i < len; i++) { 377 unmappable.append(String.format("%02X", inbuf.get())); 378 } 379 380 String charsetName = charset == null ? encName : charset.name(); 381 382 log.error(dest.limit(), 383 Errors.IllegalCharForEncoding(unmappable.toString(), charsetName)); 384 385 // undo the flip() to prepare the output buffer 386 // for more translation 387 dest.position(dest.limit()); 388 dest.limit(dest.capacity()); 389 dest.put((char)0xfffd); // backward compatible 390 } else { 391 throw new AssertionError(result); 392 } 393 } 394 // unreached 395 } 396 397 public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) { 398 Charset cs = (this.charset == null) 399 ? Charset.forName(encodingName) 400 : this.charset; 401 CharsetDecoder decoder = cs.newDecoder(); 402 403 CodingErrorAction action; 404 if (ignoreEncodingErrors) 405 action = CodingErrorAction.REPLACE; 406 else 407 action = CodingErrorAction.REPORT; 408 409 return decoder 410 .onMalformedInput(action) 411 .onUnmappableCharacter(action); 412 } 413 // </editor-fold> 414 415 // <editor-fold defaultstate="collapsed" desc="ByteBuffers"> 416 /** 417 * Make a {@link ByteBuffer} from an input stream. 418 * @param in the stream 419 * @return a byte buffer containing the contents of the stream 420 * @throws IOException if an error occurred while reading the stream 421 */ 422 public ByteBuffer makeByteBuffer(InputStream in) throws IOException { 423 byte[] array; 424 synchronized (this) { 425 if ((array = byteArrayCache) != null) 426 byteArrayCache = null; 427 else 428 array = EMPTY_ARRAY; 429 } 430 com.sun.tools.javac.util.ByteBuffer buf = new com.sun.tools.javac.util.ByteBuffer(array); 431 buf.appendStream(in); 432 return buf.asByteBuffer(); 433 } 434 435 public void recycleByteBuffer(ByteBuffer buf) { 436 if (buf.hasArray()) { 437 synchronized (this) { 438 byteArrayCache = buf.array(); 439 } 440 } 441 } 442 443 private byte[] byteArrayCache; 444 // </editor-fold> 445 446 // <editor-fold defaultstate="collapsed" desc="Content cache"> 447 public CharBuffer getCachedContent(JavaFileObject file) { 448 ContentCacheEntry e = contentCache.get(file); 449 if (e == null) 450 return null; 451 452 if (!e.isValid(file)) { 453 contentCache.remove(file); 454 return null; 455 } 456 457 return e.getValue(); 458 } 459 460 public void cache(JavaFileObject file, CharBuffer cb) { 461 contentCache.put(file, new ContentCacheEntry(file, cb)); 462 } 463 464 public void flushCache(JavaFileObject file) { 465 contentCache.remove(file); 466 } 467 468 public synchronized void resetOutputFilesWritten() { 469 outputFilesWritten.clear(); 470 } 471 472 protected final Map<JavaFileObject, ContentCacheEntry> contentCache = new HashMap<>(); 473 474 protected static class ContentCacheEntry { 475 final long timestamp; 476 final SoftReference<CharBuffer> ref; 477 478 ContentCacheEntry(JavaFileObject file, CharBuffer cb) { 479 this.timestamp = file.getLastModified(); 480 this.ref = new SoftReference<>(cb); 481 } 482 483 boolean isValid(JavaFileObject file) { 484 return timestamp == file.getLastModified(); 485 } 486 487 CharBuffer getValue() { 488 return ref.get(); 489 } 490 } 491 // </editor-fold> 492 493 public static Kind getKind(Path path) { 494 return getKind(path.getFileName().toString()); 495 } 496 497 public static Kind getKind(String name) { 498 if (name.endsWith(Kind.CLASS.extension)) 499 return Kind.CLASS; 500 else if (name.endsWith(Kind.SOURCE.extension)) 501 return Kind.SOURCE; 502 else if (name.endsWith(Kind.HTML.extension)) 503 return Kind.HTML; 504 else 505 return Kind.OTHER; 506 } 507 508 protected static <T> T nullCheck(T o) { 509 return Objects.requireNonNull(o); 510 } 511 512 protected static <T> Collection<T> nullCheck(Collection<T> it) { 513 for (T t : it) 514 Objects.requireNonNull(t); 515 return it; 516 } 517 518 // Output File Clash Detection 519 520 /** Record the fact that we have started writing to an output file. 521 */ 522 // Note: individual files can be accessed concurrently, so we synchronize here 523 synchronized void newOutputToPath(Path path) throws IOException { 524 525 // Is output file clash detection enabled? 526 if (!lint.isEnabled(LintCategory.OUTPUT_FILE_CLASH)) 527 return; 528 529 // Get the "canonical" version of the file's path; we are assuming 530 // here that two clashing files will resolve to the same real path. 531 Path realPath; 532 try { 533 realPath = path.toRealPath(); 534 } catch (NoSuchFileException e) { 535 return; // should never happen except on broken filesystems 536 } 537 538 // Check whether we've already opened this file for output 539 if (!outputFilesWritten.add(realPath)) 540 log.warning(LintWarnings.OutputFileClash(path)); 541 } 542 }